Syntax
- The structure of language.
- In python the first line of every python program must be
#!/usr/bin/python
. - This executes your program.
/usr/bin/
is where the program resides.- This tells the system where the python program is located.
VARIABLES
- A place to store a number or text.
- Variables have types associated with them.
- int: An integer.
- float: Decimal numbers.
- string: Contains letters, punctuation, numbers.
- In python you don’t declare variables.
- In python all text is stored in a string.
- In python numbers can be stored as a string when in quotations
- You cannot concatenate numbers with text strings
The print
statement displays the contents of the variable to the screen, ie:
Arithmatic Operators
- Overloading: An operator that has multiple functions.
- Concatenation: The act of adding two strings together; combining them.
- addition
+
- subtraction
-
- multiplication
*
- division
/
- You can also store character strings in a variable. A character string is a collection of letters, punctuation, numbers, or blank space, ie:
str = "Fred"
str2 = "betty23?"
A Simple Python Program
- Anything on the right side of an assignment can go into a print statement.
- The addition operator
+
works with numbers and strings (overloading). - You cannot mix numbers and strings w/ the
+
symbol.
Running The Program
If the program is saved in a file named al.py there are two steps to running a python program:
Before you can run the program, you need to tell the computer that the file is a program by issuing chmod u+x
al.py (you only need to use chmod once).
To run the program type ./followed by the program name.
Branching
Making a choice between several actions. Use the if statement. To make a choice we need to test a variable, ie using ==. Without indentation the program creates an IndentationError. The colon must be placed at the end of the if and else lines.
Example 1
Example 2
Nesting
Putting one or more if statements inside another.
Example 1
Example 2
Looping
Repeating operations. Eliminates repetition in the code. ie. printing “fred” 100 times would be annoying. Since in the above example we need to count from 1 to 100; the end condition is when count ==100. Loops repeat operations until a condition is met.
The While Loop
The while loop repeats a block (indented) of code until a test is false.
Example: Count from 0 to 99
Out:
Fred 0
Fred 1
...
Fred 99
The For Loop and Range() Function
Used to count a number or iterations. Easier to count with than while loop. Range(5) returns the set of values 0,1,2,3,4. The for loops sets i equal to the next value from the set created by range(). Range() can have a starting value where it will begin the count; ie. range(3,6). With no starting value it will begin with 0. When the set is empty, the loop ends. The set does not need to be created using range(); range is for counting. Strings can create a set in a for loop.
Example: Iterate from 0 to 4
Out:
0
1
2
3
4
Example: Loop through a String
In:
Out:
F
r
e
d