Python3-- Python basic syntax of python basis
1. The first sentence python
- - suffix can be any? (When the module is introduced, is not applicable)
- - file extensions are .py
2. State of implementation
python interpreter py file path
pyhton real-time input into the interpreter and read results
Interpreter path
#! / Usr / bin / env python python2 the beginning of a comment
coding
# -*- coding:utf8 -*-
ascil 00000000 eight digits
unicode 0000000000000000+ digits to represent at least 16
How many expressed utf-8 can represent how much to use
5. perform a complicated operation
To remind the user to enter: Username Password
Obtain a user name and password, detection: username root password root
Log on success
Error and fails
a.input usage always wait until the user enters the value assigned to the variable input
6. The variable name
Variables only by
letter
digital
Underline
Exception: can not start with a number
e.g. python keyword '' class '', etc.
7. Conditional statements
(1) if conditions:
Internal block
Internal block
Internal block
else:
...
#TAB four keys on behalf of that position
(2) if else can support nesting
Multi-conditional (3) if elif of
inp = input ( 'Please enter your membership level' )
IF Inpu == "senior member" :
Print ( 'beauty' )
elif InP == "Platinum Member" : Print ( 'modol' ) elif InP == "star" : Print ( 'urban management')
Print ( 'start serving it ....')
Why (4) Supplementary psss not perform
IF 1 == 1:
Pass
the else:
command statement
8. The basic data types
String (in quotes), for example: name = "alex" or name = 'alex'
N1 addition string = 'alex' n2 = 'sb' n3 = n1 + n2
Multiplication string n1 = 'alex' n2 = n1 * 10
Digital I arithmetic th (4 th 4) a4 = 39% 8 a3 = 4 ** 4 (39 remainder acquired by dividing 8)
a5 = 39 // 8 (two diagonal lines taken supplier
9. circulation
Infinite loop:
the while. 1. 1 ==:
Print ( 'OK')
a conditional loop
COUNT = 0
Import Time
the while COUNT <10:
Print (COUNT, the time.time)
COUNT = COUNT +. 1
Print (123)
10. Exercises
(1) while loop input 1234568910
count = 1 while count != 7 and count < = 10: print(count) count = count + 1
The second solution
n = 1 while n < 11: if n == 7: pass else: print(n) n =n + 1 print('------end----')
(2) find all the numbers from 1 to 100 and
num = 1 count = 1 while count <= 100: count = count + 1 num = num +count
In all odd (3) Output 1-100
count = 0 while count < 100: a1 = count % 2 if a1 == 0: pass else: print(a1) count = count +1
In all even (4) Output 1-100
count = 0 while count < 100: a1 = count% 2 if a1 == 0: print(a1) count = count +1 else: pass
(5) Find the number of + 1-2 + 3-4 and all 5 ... 99
. 1 = n- S = 0 # S is the sum before all the while n-<100 : TEMP = n-2% IF TEMP == 0: S = S - n- n- = n-+. 1 the else S = S + n- n- = n-+ . 1 Print (S)