WWW.APPSERVGRID.COM

Phorum about AppServers & Grid Technologies
It is currently Mon Dec 02, 2024 6:17 pm

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Mon Feb 11, 2019 2:56 am 
Offline
Site Admin

Joined: Tue Jan 25, 2011 8:51 pm
Posts: 49
Welcome to Python!

Python is a high-level programming language, with applications in numerous areas, including web programming, scripting, scientific computing, and artificial intelligence.

It is very popular and used by organizations such as Google, NASA, the CIA, and Disney.
Python is processed at runtime by the interpreter. There is no need to compile your program before executing it.
---------------------------------------------
Welcome to Python!

The three major versions of Python are 1.x, 2.x and 3.x. These are subdivided into minor versions, such as 2.7 and 3.3.
Code written for Python 3.x is guaranteed to work in all future versions.
Both Python Version 2.x and 3.x are used currently.
This course covers Python 3.x, but it isn't hard to change from one version to another.

Python has several different implementations, written in various languages.
The version used in this course, CPython, is the most popular by far.
An interpreter is a program that runs scripts written in an interpreted language such as Python.
-----------------------------------------------
Your First Program

Let's start off by creating a short program that displays "Hello world!".
In Python, we use the print statement to output text:
>>> print('Hello world!')
Hello world!

Congratulations! You have written your first program.
Run, save, and share your Python code on our Code Playground without installing any additional software.
When using a computer, you will need to download and install Python from http://www.python.org.
Note the >>> in the code above. They are the prompt symbol of the Python console. Python is an interpreted
language, which means that each line is executed as it is entered. Python
also includes IDLE, the integrated development environment, which includes tools for
writing and debugging entire programs.
-----------------------------------------------
Printing Text

The print statement can also be used to output multiple lines of text.
For Example:
>>> print('Hello world!')
Hello world!
>>> print('Hello world!')
Hello world!
>>> print('Spam and eggs...')
Spam and eggs...

Python code often contains references to the comedy group Monty Python. This is why the words, "spam" and "eggs" are often used as placeholder variables in Python where "foo" and "bar" would be used in other programming languages.
--------------------------------------------------
Simple Operations

Python has the capability of carrying out calculations.
Enter a calculation directly into the Python console, and it will output the answer.
>>> 2 + 2
4
>>> 5 + 4 - 3
6

The spaces around the plus and minus signs here are optional (the code would work without them), but they make it easier to read.
----------------------------------------------------
Simple Operations

Python also carries out multiplication and division, using an asterisk to indicate multiplication and a forward slash to indicate division.

Use parentheses to determine which operations are performed first.
>>> 2 * (3 + 4)
14
>>> 10 / 2
5.0

Using a single slash to divide numbers produces a decimal (or float, as it is called in programming). We'll have more about floats in a later lesson.
-----------------------------------------------------
Simple Operations

The minus sign indicates a negative number.
Operations are performed on negative numbers, just as they are on positive ones.
>>> -7
-7
>>> (-7 + 2) * (-4)
20

The plus signs can also be put in front of numbers, but this has no effect, and is mostly
used to emphasize that a number is positive to increase readability of code.
------------------------------------------------------
Simple Operations

Dividing by zero in Python produces an error, as no answer can be calculated.
>>> 11 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

In Python, the last line of an error message indicates the error's type.
Read error messages carefully, as they often tell you how to fix a program!
-------------------------------------------------------
Floats

Floats are used in Python to represent numbers that aren't integers.
Some examples of numbers that are represented as floats are 0.5 and -7.8237591.
They can be created directly by entering a number with a decimal point, or by using operations such as division on integers.
Extra zeros at the number's end are ignored.
>>> 3/4
0.75
>>> 9.8765000
9.8765

Computers can't store floats perfectly accurately, in the same way that we can't write down the complete decimal
expansion of 1/3 (0.3333333333333333...). Keep this in mind,
because it often leads to infuriating bugs!
--------------------------------------------------------
Floats

As you saw previously, dividing any two integers produces a float.
A float is also produced by running an operation on two floats, or on a float and an integer.
>>> 8 / 2
4.0
>>> 6 * 7.0
42.0
>>> 4 + 1.65
5.65

A float can be added to an integer, because Python silently converts the integer to a float.
However, this implicit conversion is the exception rather the rule in Python - usually you have
to convert values manually if you want to operate on them.
---------------------------------------------------------
Exponentiation

Besides addition, subtraction, multiplication, and division, Python also supports exponentiation,
which is the raising of one number to the power of another. This operation is performed using two asterisks.
>>> 2**5
32
>>> 9 ** (1/2)
3.0
---------------------------------------------------------
Quotient & Remainder

To determine the quotient and remainder of a division, use the floor division and modulo operators, respectively.
Floor division is done using two forward slashes.
The modulo operator is carried out with a percent symbol (%).
These operators can be used with both floats and integers.

This code shows that 6 goes into 20 three times, and the remainder when 1.25 is divided by 0.5 is 0.25.
>>> 20 // 6
3
>>> 1.25 % 0.5
0.25
----------------------------------------------------------
Strings

If you want to use text in Python, you have to use a string.
A string is created by entering text between two single or double quotation marks.

When the Python console displays a string, it generally uses
single quotes. The delimiter used for a string doesn't affect how it behaves in any way.
>>> "Python is fun!"
'Python is fun!'
>>> 'Always look on the bright side of life'
'Always look on the bright side of life'
-------------------------------------------------------------
Strings

Some characters can't be directly included in a string. For instance, double quotes
can't be directly included in a double quote string; this would cause it to end
prematurely.

Characters like these must be escaped by placing a backslash before them.
Other common characters that must be escaped are newlines and backslashes.
Double quotes only need to be escaped in double quote strings, and the same
is true for single quote strings.
>>> 'Brian\'s mother: He\'s not the Messiah. He\'s a very naughty boy!'
'Brian's mother: He's not the Messiah. He's a very naughty boy!'

\n represents a new line.
Backslashes can also be used to escape tabs, arbitrary Unicode characters,
and various other things that can't be reliably printed. These characters
are known as escape characters.
----------------------------------------------------------------
Newlines

Python provides an easy way to avoid manually writing "\n" to escape newlines in a
string. Create a string with three sets of quotes, and newlines that are created by
pressing Enter are automatically escaped for you.
>>> """Customer: Good morning.
Owner: Good morning, Sir. Welcome to the National Cheese Emporium."""

'Customer: Good morning.\nOwner: Good morning, Sir. Welcome to the National Cheese
Emporium.'

As you can see, the \n was automatically put in the output, where we pressed Enter.
-------------------------------------------------------------------

Output

Usually, programs take input and process it to produce output.
In Python, you can use the print function to produce output. This displays a textual representation of something to the screen.
>>> print(1 + 1)
2
>>> print("Hello\nWorld!")
Hello
World!

When a string is printed, the quotes around it are not displayed.
--------------------------------------------------------------------

Input

To get input from the user in Python, you can use the intuitively named input function.
The function prompts the user for input, and returns what they enter as a string (with the contents
automatically escaped).
>>> input("Enter something please: ")
Enter something please: This is what\nthe user enters!

'This is what\\nthe user enters!'

The print and input functions aren't very useful at the Python console, which automatically does
input and output. However, they are very useful in actual programs.
---------------------------------------------------------------------
Concatenation

As with integers and floats, strings in Python can be added, using a process called concatenation,
which can be done on any two strings.
When concatenating strings, it doesn't matter whether they've been created with single or double quotes.
>>> "Spam" + 'eggs'
'Spameggs'

>>> print("First string" + ", " + "second string")
First string, second string
----------------------------------------------------------------------
Concatenation

Even if your strings contain numbers, they are still added as strings rather than integers.
Adding a string to a number produces an error, as even though they might look similar, they are two different entities.
>>> "2" + "2"
'22'
>>> 1 + '2' + 3 + '4'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

In future lessons, only the final line of error messages will be displayed, as it is the only one that gives details
about the type of error that has occurred.
------------------------------------------------------------------------
String Operations

Strings can also be multiplied by integers. This produces a repeated version of the original string.
The order of the string and the integer doesn't matter, but the string usually comes first.

Strings can't be multiplied by other strings. Strings also can't be multiplied by floats,
even if the floats are whole numbers.
>>> print("spam" * 3)
spamspamspam

>>> 4 * '2'
'2222'

>>> '17' * '87'
TypeError: can't multiply sequence by non-int of type 'str'

>>> 'pythonisfun' * 7.0
TypeError: can't multiply sequence by non-int of type 'float'
------------------------------------------------------------------------
Type Conversion

In Python, it's impossible to complete certain operations due to the types involved.
For instance, you can't add two strings containing the numbers 2 and 3 together to
produce the integer 5, as the operation will be performed on strings, making the result '23'.
The solution to this is type conversion.
In that example, you would use the int function.
>>> "2" + "3"
'23'
>>> int("2") + int("3")
5

In Python, the types we have used so far have been integers, floats, and strings.
The functions used to convert to these are int, float and str, respectively.
------------------------------------------------------------------------
Type Conversion

Another example of type conversion is turning user input (which is a string) to numbers
(integers or floats), to allow for the performance of calculations.
>>> float(input("Enter a number: ")) + float(input("Enter another number: "))
Enter a number: 40
Enter another number: 2
42.0
-------------------------------------------------------------------------
Variables

Variables play a very important role in most programming languages, and Python is no
exception. A variable allows you to store a value by assigning it to a name, which
can be used to refer to the value later in the program.

To assign a variable, use one equals sign. Unlike most lines of code we've looked at
so far, it doesn't produce any output at the Python console.
>>> x = 7
>>> print(x)
7
>>> print(x + 3)
10
>>> print(x)
7

You can use variables to perform corresponding operations, just as you did with numbers
and strings. As you can see, the variable stores its value throughout the program.
---------------------------------------------------------------------------
Variables

Variables can be reassigned as many times as you want, in order to change their value.
In Python, variables don't have specific types, so you can assign a string to a variable,
and later assign an integer to the same variable.
>>> x = 123.456
>>> print(x)
123.456
>>> x = "This is a string"
>>> print(x + "!")
This is a string!
-----------------------------------------------------------------------------
Variable Names

Certain restrictions apply in regard to the characters that may be used in Python variable names. The only characters that are allowed are letters, numbers, and underscores. Also, they can't start with numbers.
Not following these rules results in errors.
>>> this_is_a_normal_name = 7

>>> 123abc = 7
SyntaxError: invalid syntax

>>> spaces are not allowed
SyntaxError: invalid syntax

Python is a case sensitive programming language. Thus, Lastname and lastname are
two different variable names in Python.
-------------------------------------------------------------------------------
Variables

Trying to reference a variable you haven't assigned to causes an error.
You can use the del statement to remove a variable, which means the reference
from the name to the value is deleted, and trying to use the variable causes an error.
Deleted variables can be reassigned to later as normal.
>>> foo = "a string"
>>> foo
'a string'
>>> bar
NameError: name 'bar' is not defined
>>> del foo
>>> foo
NameError: name 'foo' is not defined

You can also take the value of the variable from the user input.
>>> foo = input("Enter a number: ")
Enter a number: 7
>>> print(foo)
7

The variables foo and bar are called metasyntactic
variables, meaning that they are used as placeholder names
in example code to demonstrate something.
-------------------------------------------------------------------------------
In-Place Operators

In-place operators allow you to write code like 'x = x + 3' more concisely, as 'x += 3'.
The same thing is possible with other operators such as -, *, / and % as well.
>>> x = 2
>>> print(x)
2
>>> x += 3
>>> print(x)
5
--------------------------------------------------------------------------------
In-Place Operators

These operators can be used on types other than numbers, as well, such as strings.
>>> x = "spam"
>>> print(x)
spam

>>> x += "eggs"
>>> print(x)
spameggs

Many other languages have special operators such as '++' as a shortcut
for 'x += 1'. Python does not have these.
---------------------------------------------------------------------------------
Using an Editor

So far, we've only used Python with the console, entering and running one line of code at a time.
Actual programs are created differently; many lines of code are written in a file, and then executed with the Python interpreter.

In IDLE, this can be done by creating a new file, entering some code, saving the file, and running it. This can
be done either with the menus or with the keyboard shortcuts Ctrl-N, Ctrl-S and F5.

Each line of code in the file is interpreted as though you entered it one line at a time at the console.
x = 7
x = x + 2
print(x)

Python source files have an extension of .py
You can run, save, and share your Python codes on our Code Playground, without installing any additional software.
Reference this lesson if you need to install the software on your computer.
-----------------------------------------------------------------------------------
Booleans

Another type in Python is the Boolean type. There are two Boolean values: True and False.
They can be created by comparing values, for instance by using the equal operator ==.
>>> my_boolean = True
>>> my_boolean
True

>>> 2 == 3
False
>>> "hello" == "hello"
True

Be careful not to confuse assignment (one equals sign) with comparison (two equals signs).
------------------------------------------------------------------------------------
Comparison

Another comparison operator, the not equal operator (!=), evaluates to True if the
items being compared aren't equal, and False if they are.
>>> 1 != 1
False
>>> "eleven" != "seven"
True
>>> 2 != 10
True
-------------------------------------------------------------------------------------
Comparison

Python also has operators that determine whether one number (float or integer) is
greater than or smaller than another. These operators are > and < respectively.
>>> 7 > 5
True
>>> 10 < 10
False
--------------------------------------------------------------------------------------
Comparison

The greater than or equal to, and smaller than or equal to operators are >= and <=.
They are the same as the strict greater than and smaller than operators, except that
they return True when comparing equal numbers.
>>> 7 <= 8
True
>>> 9 >= 9.0
True

Greater than and smaller than operators can also be used to compare strings
lexicographically (the alphabetical order of words is based on the alphabetical order
of their component letters).
----------------------------------------------------------------------------------------
if Statements

You can use if statements to run code if a certain condition holds.
If an expression evaluates to True, some statements are carried out. Otherwise, they aren't carried out.
An if statement looks like this:
if expression:
statements

Python uses indentation (white space at the beginning of a line) to delimit blocks of code.
Other languages, such as C, use curly braces to accomplish this, but in Python indentation is
mandatory; programs won't work without it. As you can see, the statements in the if should be indented.
-------------------------------------------------------------------------------------------
if Statements

Here is an example if statement:
if 10 > 5:
print("10 greater than 5")

print("Program ended")

The expression determines whether 10 is greater than five. Since it is, the indented statement runs,
and "10 greater than 5" is output. Then, the unindented statement, which is not part of the if statement,
is run, and "Program ended" is displayed.

Result:
>>>
10 greater than 5
Program ended
>>>

Notice the colon at the end of the expression in the if statement.
As the program contains multiple lines of code, you should create it as a separate file and run it.
---------------------------------------------------------------------------------------------
if Statements

To perform more complex checks, if statements can be nested, one inside the other.
This means that the inner if statement is the statement part of the outer one. This is one way to
see whether multiple conditions are satisfied.

For example:
num = 12
if num > 5:
print("Bigger than 5")
if num <=47:
print("Between 5 and 47")

Result:
>>>
Bigger than 5
Between 5 and 47
>>>

----------------------------------------------------------------------------------------------
else Statements

An else statement follows an if statement, and contains code that is called when the if statement
evaluates to False.
As with if statements, the code inside the block should be indented.
x = 4
if x == 5:
print("Yes")
else:
print("No")

Result:
>>>
No
>>>
-----------------------------------------------------------------------------------------------
You can chain if and else statements to determine which option in a series of possibilities is true.
For example:
num = 7
if num == 5:
print("Number is 5")
else:
if num == 11:
print("Number is 11")
else:
if num == 7:
print("Number is 7")
else:
print("Number isn't 5, 11 or 7")

Result:
>>>
Number is 7
>>>
----------------------------------------------------------------------------------------------------
elif Statements

The elif (short for else if) statement is a shortcut to use when chaining if and else statements.
A series of if elif statements can have a final else block, which is called if none of the if or elif
expressions is True.
For example:
num = 7
if num == 5:
print("Number is 5")
elif num == 11:
print("Number is 11")
elif num == 7:
print("Number is 7")
else:
print("Number isn't 5, 11 or 7")

Result:
>>>
Number is 7
>>>

In other programming languages, equivalents to the elif statement have varying names,
including else if, elseif or elsif.
-------------------------------------------------------------------------------------------------------
Boolean Logic

Boolean logic is used to make more complicated conditions for if statements that rely on more than one condition.
Python's Boolean operators are and, or, and not.
The and operator takes two arguments, and evaluates as True if, and only if, both of its arguments are True.
Otherwise, it evaluates to False.
>>> 1 == 1 and 2 == 2
True
>>> 1 == 1 and 2 == 3
False
>>> 1 != 1 and 2 == 2
False
>>> 2 < 1 and 3 > 6
False

Python uses words for its Boolean operators, whereas most other languages use symbols such as &&, || and !.
----------------------------------------------------------------------------------------------------------
Boolean Or

The or operator also takes two arguments. It evaluates to True if either (or both) of its arguments are True,
and False if both arguments are False.
>>> 1 == 1 or 2 == 2
True
>>> 1 == 1 or 2 == 3
True
>>> 1 != 1 or 2 == 2
True
>>> 2 < 1 or 3 > 6
False
-----------------------------------------------------------------------------------------------------------
Boolean Not

Unlike other operators we've seen so far, not only takes one argument, and inverts it.
The result of not True is False, and not False goes to True.
>>> not 1 == 1
False
>>> not 1 > 7
True

You can chain multiple conditional statements in an if statement using the Boolean operators.
------------------------------------------------------------------------------------------------------------
Operator Precedence

Operator precedence is a very important concept in programming. It is an extension of the mathematical idea of order of operations (multiplication being performed before addition, etc.) to include other operators, such as those in Boolean logic.

The below code shows that == has a higher precedence than or:
>>> False == False or True
True
>>> False == (False or True)
False
>>> (False == False) or True
True

Python's order of operations is the same as that of normal mathematics: parentheses first, then
exponentiation, then multiplication/division, and then addition/subtraction.
-------------------------------------------------------------------------------------------------------------
while Loops

An if statement is run once if its condition evaluates to True, and never if it evaluates to False.
A while statement is similar, except that it can be run more than once. The statements inside it are repeatedly executed, as long as the condition holds. Once it evaluates to False, the next section of code is executed.
Below is a while loop containing a variable that counts up from 1 to 5, at which point the loop terminates.
i = 1
while i <=5:
print(i)
i = i + 1

print("Finished!")

Result:
>>>
1
2
3
4
5
Finished!
>>>

The code in the body of a while loop is
executed repeatedly. This is called iteration.
---------------------------------------------------------------------------------------------------------------
while Loops

The infinite loop is a special kind of while loop; it never stops running. Its condition always remains True.
An example of an infinite loop:
while 1==1:
print("In the loop")

This program would indefinitely print "In the loop".
You can stop the program's execution by using the Ctrl-C shortcut or by closing the program.
----------------------------------------------------------------------------------------------------------------
break

To end a while loop prematurely, the break statement can be used.
When encountered inside a loop, the break statement causes the loop to finish immediately.
i = 0
while 1==1:
print(i)
i = i + 1
if i >= 5:
print("Breaking")
break

print("Finished")

Result:
>>>
0
1
2
3
4
Breaking
Finished
>>>

Using the break statement outside of a loop causes an error.
-------------------------------------------------------------------------------------------------------------
continue

Another statement that can be used within loops is continue.
Unlike break, continue jumps back to the top of the loop, rather than stopping it.
i = 0
while True:
i = i +1
if i == 2:
print("Skipping 2")
continue
if i == 5:
print("Breaking")
break
print(i)

print("Finished")

Result:
>>>
1
Skipping 2
3
4
Breaking
Finished
>>>

Basically, the continue statement stops the current iteration and continues with the next one.
Using the continue statement outside of a loop causes an error.
--------------------------------------------------------------------------------------------------------------
Lists

Lists are another type of object in Python. They are used to store an indexed list of items.
A list is created using square brackets with commas separating items.
The certain item in the list can be accessed by using its index in square brackets.
For example:
words = ["Hello", "world", "!"]
print(words[0])
print(words[1])
print(words[2])

Result:
>>>
Hello
world
!
>>>

The first list item's index is 0, rather than 1, as might be expected.
---------------------------------------------------------------------------------------------------------------
Lists

An empty list is created with an empty pair of square brackets.
empty_list = []
print(empty_list)

Result:
>>>
[]
>>>

Most of the time, a comma won't follow the last item in a list. However, it is perfectly valid to place
one there, and it is encouraged in some cases.
-----------------------------------------------------------------------------------------------------------------
Lists

Typically, a list will contain items of a single item type, but it is also possible to include several different types.
Lists can also be nested within other lists.
number = 3
things = ["string", 0, [1, 2, number], 4.56]
print(things[1])
print(things[2])
print(things[2][2])

Result:
>>>
0
[1, 2, 3]
3
>>>

Lists of lists are often used to represent 2D grids, as Python lacks
the multidimensional arrays that would be used for this in other languages.
--------------------------------------------------------------------------------------------------------------------
Lists

Indexing out of the bounds of possible list values causes an IndexError.
Some types, such as strings, can be indexed like lists. Indexing strings behaves as though you are indexing a list containing each character in the string.
For other types, such as integers, indexing them isn't possible, and it causes a TypeError.
str = "Hello world!"
print(str[6])

Result:
>>>
w
>>>
---------------------------------------------------------------------------------------------------------------------
List Operations

The item at a certain index in a list can be reassigned.
For example:
nums = [7, 7, 7, 7, 7]
nums[2] = 5
print(nums)

Result:
>>>
[7, 7, 5, 7, 7]
>>>
----------------------------------------------------------------------------------------------------------------------
List Operations

Lists can be added and multiplied in the same way as strings.
For example:
nums = [1, 2, 3]
print(nums + [4, 5, 6])
print(nums * 3)

Result:
>>>
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>>

Lists and strings are similar in many ways - strings can be thought of as lists of characters that can't be changed.
------------------------------------------------------------------------------------------------------------------------
List Operations

To check if an item is in a list, the in operator can be used. It returns True if the item occurs
one or more times in the list, and False if it doesn't.
words = ["spam", "egg", "spam", "sausage"]
print("spam" in words)
print("egg" in words)
print("tomato" in words)

Result:
>>>
True
True
False
>>>

The in operator is also used to determine whether or not a string is a substring of another string.
-----------------------------------------------------------------------------------------------
List Operations

To check if an item is not in a list, you can use the not operator in one of the following ways:
nums = [1, 2, 3]
print(not 4 in nums)
print(4 not in nums)
print(not 3 in nums)
print(3 not in nums)

Result:
>>>
True
True
False
False
>>>
-------------------------------------------------------------------------------------------------
List Functions

Another way of altering lists is using the append method. This adds an item to the end of an existing list.
nums = [1, 2, 3]
nums.append(4)
print(nums)

Result:
>>>
[1, 2, 3, 4]
>>>

The dot before append is there because it is a method of the list class. Methods will be
explained in a later lesson.
----------------------------------------------------------------------------------------------------
List Functions

To get the number of items in a list, you can use the len function.
nums = [1, 3, 5, 2, 4]
print(len(nums))

Result:
>>>
5
>>>

Unlike append, len is a normal function, rather than a method. This means it
is written before the list it is being called on, without a dot.
-----------------------------------------------------------------------------------------------------
List Functions

The insert method is similar to append, except that it allows you to insert a new item at any
position in the list, as opposed to just at the end.
words = ["Python", "fun"]
index = 1
words.insert(index, "is")
print(words)

Result:
>>>
['Python', 'is', 'fun']
>>>
-------------------------------------------------------------------------------------------------------
List Functions

The index method finds the first occurrence of a list item and returns its index.
If the item isn't in the list, it raises a ValueError.
letters = ['p', 'q', 'r', 's', 'p', 'u']
print(letters.index('r'))
print(letters.index('p'))
print(letters.index('z'))

Result:
>>>
2
0
ValueError: 'z' is not in list
>>>

There are a few more useful functions and methods for lists.
max(list): Returns the list item with the maximum value
min(list): Returns the list item with minimum value
list.count(obj): Returns a count of how many times an item occurs in a list
list.remove(obj): Removes an object from a list
list.reverse(): Reverses objects in a list
---------------------------------------------------------------------------------------------------------
Range

The range function creates a sequential list of numbers.
The code below generates a list containing all of the integers, up to 10.
numbers = list(range(10))
print(numbers)

Result:
>>>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

The call to list is necessary because range by itself creates a range object, and this
must be converted to a list if you want to use it as one.
-----------------------------------------------------------------------------------------------------------
Range

If range is called with one argument, it produces an object with values from 0 to that argument.
If it is called with two arguments, it produces values from the first to the second.
For example:
numbers = list(range(3, 8))
print(numbers)

print(range(20) == range(0, 20))

Result:
>>>
[3, 4, 5, 6, 7]

True
>>>
------------------------------------------------------------------------------------------------
Range

range can have a third argument, which determines the interval of the sequence produced.
This third argument must be an integer.
numbers = list(range(5, 20, 2))
print(numbers)

Result:
>>>
[5, 7, 9, 11, 13, 15, 17, 19]
>>>
--------------------------------------------------------------------------------------------------
Loops

Sometimes, you need to perform code on each item in a list. This is called iteration, and
it can be accomplished with a while loop and a counter variable.
For example:
words = ["hello", "world", "spam", "eggs"]
counter = 0
max_index = len(words) - 1

while counter <= max_index:
word = words[counter]
print(word + "!")
counter = counter + 1

Result:
>>>
hello!
world!
spam!
eggs!
>>>

The example above iterates through all items in the list, accesses them using their indices,
and prints them with exclamation marks.
-----------------------------------------------------------------------------------------------------
for Loop

Iterating through a list using a while loop requires quite a lot of code, so Python provides
the for loop as a shortcut that accomplishes the same thing.
The same code from the previous example can be written with a for loop, as follows:
words = ["hello", "world", "spam", "eggs"]
for word in words:
print(word + "!")

Result:
>>>
hello!
world!
spam!
eggs!
>>>

The for loop in Python is like the foreach loop in other languages.
---------------------------------------------------------------------------------------------------------
for Loops

The for loop is commonly used to repeat some code a certain number of times. This is done
by combining for loops with range objects.
for i in range(5):
print("hello!")

Result:
>>>
hello!
hello!
hello!
hello!
hello!
>>>

You don't need to call list on the range object when it is used in a for loop, because
it isn't being indexed, so a list isn't required.
------------------------------------------------------------------------------------------------------------
Creating a Calculator

This lesson is about an example Python project: a simple calculator.
Each part explains a different section of the program.
The first section is the overall menu. This keeps on accepting user input until the user enters "quit",
so a while loop is used.

while True:
print("Options:")
print("Enter 'add' to add two numbers")
print("Enter 'subtract' to subtract two numbers")
print("Enter 'multiply' to multiply two numbers")
print("Enter 'divide' to divide two numbers")
print("Enter 'quit' to end the program")
user_input = input(": ")

if user_input == "quit":
break
elif user_input == "add":
...
elif user_input == "subtract":
...
elif user_input == "multiply":
...
elif user_input == "divide":
...
else:
print("Unknown input")

The code above is the starting point for our program. It accepts user input, and compares it to
the options in the if/elif statements.
The break statement is used to stop the while loop, in case the user inputs "quit".
-----------------------------------------------------------------------------------------------------------
Creating a Calculator

The next part of the program is getting the numbers the user wants to do something with.
The code below shows this for the addition section of the calculator. Similar code would have to
be written for the other sections.
elif user_input == "add":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))

Now, when the user inputs "add", the program prompts to enter two numbers, and stores them
in the corresponding variables.
As it is, this code crashes if the user enters a non-numeric input when prompted to
enter a number. We will look at fixing problems like this in a later module.

--------------------------------------------------------------------------------------------------------------
Creating a Calculator

The final part of the program processes user input and displays it.
The code for the addition part is shown here.
elif user_input == "add":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
result = str(num1 + num2)
print("The answer is " + result)

We now have a working program that prompts for user input, and then calculates and prints the sum of the input.
Similar code would have to be written for the other branches (for subtraction, multiplication and division).
The output line could be put outside the if statements to omit repetition of code.
Similar code would have to be written for the other branches (for subtraction, multiplication and division).
The output line could be put outside the if statements to omit repetition of code.
----------------------------------------------------------------------------------------------------------------

Reusing Code

Code reuse is a very important part of programming in any language. Increasing code size makes it harder to maintain.
For a large programming project to be successful, it is essential to abide by the Don't Repeat Yourself, or DRY,
principle. We've already looked at one way of doing this: by using loops. In this module, we will explore two more:
functions and modules.
Bad, repetitive code is said to abide by the WET principle, which stands for Write Everything Twice, or We Enjoy Typing.
------------------------------------------------------------------------------------------------------------------

Functions

You've already used functions in previous lessons.
Any statement that consists of a word followed by information in parentheses is a function call.
Here are some examples that you've already seen:
print("Hello world!")
range(2, 20)
str(12)
range(10, 20, 3)

The words in front of the parentheses are function names, and the comma-separated values inside the parentheses
are function arguments.
---------------------------------------------------------------------------------------------------------------------
Functions

In addition to using pre-defined functions, you can create your own functions by using the def statement.
Here is an example of a function named my_func. It takes no arguments, and prints "spam" three times. It is
defined, and then called. The statements in the function are executed only when the function is called.
def my_func():
print("spam")
print("spam")
print("spam")

my_func()

Result:
>>>
spam
spam
spam
>>>

The code block within every function starts with a colon (:) and is indented.
----------------------------------------------------------------------------------------------------------------------
Functions

You must define functions before they are called, in the same way that you must assign variables before using them.
hello()

def hello():
print("Hello world!")

Result:
>>>
NameError: name 'hello' is not defined
>>>
------------------------------------------------------------------------------------------------------------------------
Arguments

All the function definitions we've looked at so far have been functions of zero arguments, which are called with empty parentheses.
However, most functions take arguments.
The example below defines a function that takes one argument:
def print_with_exclamation(word):
print(word + "!")

print_with_exclamation("spam")
print_with_exclamation("eggs")
print_with_exclamation("python")

Result:
>>>
spam!
eggs!
python!
>>>

As you can see, the argument is defined inside the parentheses.
-------------------------------------------------------------------------------------------------------------------------
Arguments

You can also define functions with more than one argument; separate them with commas.
def print_sum_twice(x, y):
print(x + y)
print(x + y)

print_sum_twice(5, 8)

Result:
>>>
13
13
>>>
---------------------------------------------------------------------------------------------------------------------------
Arguments

Function arguments can be used as variables inside the function definition. However, they cannot be referenced
outside of the function's definition. This also applies to other variables created inside a function.
def function(variable):
variable += 1
print(variable)

function(7)
print(variable)

Result:
>>>
8

NameError: name 'variable' is not defined
>>>

Technically, parameters are the variables in a function definition, and arguments are the values put
into parameters when functions are called.
-----------------------------------------------------------------------------------------------------------------------------
Fill in the blanks to define a function that prints "Yes", if its parameter is an even number, and "No" otherwise.

def
even(x):
if x%2 == 0:
print
("Yes")
else:
print("No")
------------------------------------------------------------------------------------------------------------------------------

Returning from Functions

Certain functions, such as int or str, return a value that can be used later.
To do this for your defined functions, you can use the return statement.

For example:
def max(x, y):
if x >=y:
return x
else:
return y

print(max(4, 7))
z = max(8, 5)
print(z)

Result:
>>>
7
8
>>>

The return statement cannot be used outside of a function definition.
-------------------------------------------------------------------------------------------------------------------------------

Fill in the blanks to define a function that compares the lengths of its arguments and returns the shortest one.
def shortest_string(x, y):
if len(x) <= len (y):
return x
else:
return y
-------------------------------------------------------------------------------------------------------------------------------

Returning from Functions

Once you return a value from a function, it immediately stops being executed. Any code after the return statement will never happen.
For example:
def add_numbers(x, y):
total = x + y
return total
print("This won't be printed")

print(add_numbers(4, 5))

Result:
>>>
9
>>>
---------------------------------------------------------------------------------------------------------------------------------

Comments

Comments are annotations to code used to make it easier to understand. They don't affect how code is run.
In Python, a comment is created by inserting an octothorpe (otherwise known as a number sign or hash symbol: #). All text after it on that line is ignored.
For example:
x = 365
y = 7
# this is a comment

print(x % y) # find the remainder
# print (x // y)
# another comment

Result:
>>>
1
>>>

Python doesn't have general purpose multiline comments, as do programming languages such as C.
----------------------------------------------------------------------------------------------------------------------------------


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron