1. Output:
>>> print (' Hello world! ' )
2. Enter:
>>> user = input (' Enter login ' name: ')
Enter Login Name:oyzhx
>>> User
' Oyzhx '
3 notes:
# starts with the # until the end of the line is commented.
4. Operator: +-*///% * * < <= > >= = = = <> (not equal to) and or not
"//": Used as a floating-point division (rounding the result)
"* *": exponentiation
5. Digital Int,long,bool, float,complex (plural)
Bool:true,false
6. String
Plus (+) for string join operations , asterisk (* ) for string repetition
>>> tmpstr = ' Hello '
>>> tmpstr2= ' World '
>>> tmpstr + TMPSTR2
' HelloWorld '
>>> tmpstr*3
' Hellohellohello '
7. Lists and tuples
List: The list element is wrapped in brackets ( []), the number of elements and the value of the element can be changed. (alist=[1,2,3, ' haha ')
Tuples: Tuple elements are wrapped with parentheses (()) and cannot be changed (Atuple = ("Try"))
8. Dictionaries
Consisting of a key-value (Key-value) pair, the value can be any type of Python object, and the dictionary element is wrapped in curly braces ({}).
>>> adict ={' name ': ' Oy '}
>>> adict[' school ' = ' CSU '
>>> adict
{' name ': ' Oy ', ' School ': ' CSU '}
9. code block and indent alignment
Code blocks use indentation alignment to Express code logic instead of curly braces because the program is more readable without extra characters. And indentation can clearly express exactly what block of code a statement belongs to.
If statement :
If expression1:
If_suite
Elif expression2:
Elif_suite
Else
Else_suite
While loop:
While expression:
While_suite
For loop:
The For loop in Python is not the same as a traditional for loop (counter loop), which is more like a foreach iteration in a shell script. A for in Python accepts an iterative object, such as a sequence or iterator, as its argument, iterating over one of the elements at a time.
>>> for item in[' name ', ' age ', ' school ']:
Print (item)
Name
Age
School
Enumerate ():
>>> foo = ' abc '
>>> for I,ch in Enumerate (foo):
Print (ch, '%d '%i)
A 0
B 1
C 2
10. List Resolution:
You can use a for loop in a row to put all the values into a list
Of
>>> squared = [x * * 2 for X in range (4)]
>>> for i in squared:
... print I
0
1
4
9
11. Files and built-in functions open (), file ()
Handle = Open (file_name, Access_mode = ' R ')
12. Errors and Exceptions try-except
Try
filename = input (' Enter file name: ')
fobj = open (filename, ' R ')
For Eachline in Fobj:
Print (Eachline, fobj.close ())
Except IOError, E:
Print (' File open error: ', e)
13. Functions:
def function_name ([arguments]):
"Optional documentation string"
Function_suite
Python Foundation One