Variable names in Python
The letter ' name ', ' age ',
--Digital ' name1 ', ' age1 '
--Underline ' name_1 ', ' age_1 '
Note: You cannot start with a number:1name_, cannot be a keyword in python, it is best not to duplicate something built into python, such as: ' and ', ' as ', ' Break ', ' class ' ....
Add: When writing variable names, try to make the variable name meaningful, such as user ID should write user_id more appropriate.
Data type String type
The content in the quotation marks in Python is the string.
Such as:
' Samoyed ' " Sydney ", " Medium Dog " "" " Sled Dog "" "
Single quotes, double quotes, and three quotation marks can be used to indicate that the quotation marks are symmetrical on both sides.
The operation of the string
Addition of strings:
Name = name1 +' samoyed Sydney '
Multiplication of strings:
Name = name2' Sydney Sydney Sydney Pear Sydney Sydney Sydney Pear Sydney Sydney
Indicates that the string recurs 10 times.
Note: String operations are only additive and multiplicative.
Number Type
Numeric types have no quotes, write numbers only
Age = 3
Arithmetic of the arithmetic addition of numbers
A = 5= 3= a += 8
Arithmetic of subtraction
c = A-= 3
Arithmetic of multiplication
c = A *= 15
The arithmetic of exponentiation in multiplication
D = 2**2= 4
Here the * * represents the second party, 2 of the 2-time side. If you want to represent 4 of the 8 parties, you should write 4**8.
The operation of division
A = += 8= A/= 2.375
Divisible, Fetch
c = A//= 2
Take surplus
c = a%= 3
The remainder is the remaining number after a divided by B, after learning to take the remainder, we can use the method to determine whether a number is odd or even
if c = = 0 :print(' even 'Else : Print (' odd ')
Conditional Statement Base statement
If + condition:
Internal code block
Internal code block
Else
。。。
Note that the inner block of code here is indented before the indentation in Python is the TAB key, and the IF statement is indented after the inner code block.
Pseudo code: If 1 equals 1, then output ' welcome login ', otherwise the output ' login failed '.
Realize:
if 1 = =1 :print(' Welcome login ')else: Print (' Login failed ')
Nesting
Pseudo code: If 1 equals 1 o'clock, 2 equals 2, the output ' welcome VIP User Login ', if 1 equals 1 o'clock, 2 is not equal to 2, output ' welcome ordinary user login ', otherwise output ' login failed '.
Realize:
if 1 = =1 :if 2 = = = 2: print(' welcome VIP User Login ') else: print(' Welcome to normal User login ' Else: print(' Login failed ')
Multi-Conditional judgment
Grade = input ('Please enter User level')ifGrade = ='Platinum Users':Print('Welcome, Platinum User')elifGrade = ='Diamond Users':Print('Welcome, Diamond Users')elifGrade = ='VIP Users':Print('welcome you, VIP user')Else:Print('Welcome, ordinary users')
Usage of Supplemental Pass
When you do not want to manipulate anything inside your if statement, you can write.
if 1==1: passelse: print(' Welcome ')
If it is
If 1==1:
Else
Print (' Welcome ')
Is wrong and is not allowed in Python, so you should use pass
Cyclic dead loop
While 1 = = 1 :print('ok')
The conditions are always set and executed forever.
Whlie Cycle
Count = 0 while Count <=: print (count)= Count +1
Summary Exercises
1, use while loop output 1,2,3,4,5,6,7,9,10.
Count = 1 while Count <=: if count = = 8: pass Else : Print(count) = Count +1
2. Ask for 1-100 and.
n = 1= 0 while n <=: = s + n Print (s) = n + 1
3, ask 1-100 all the odd
n = 1 while n <=: = n%2 if temp = = 0: pass else< /c9>: print(n) = n + 1
4. Ask for 1-100 all even
n = 1 while n <=: = n%2 if temp = = 0: Print (n) Else : Pass = n + 1
5, 1-2+3-4+5-6+7...99 all the numbers and
n = 1= 0 while n < := n 2 if temp = = 0: = s-
n
else
: = s +
n = n +1
print(s)
6. User Login (three chance retry)
Uesr_name = input ('Please enter user name') user_passwd= Input ('Please enter your password')ifUesr_name = ='Root': ifUSER_PASSWD = ='123456': Print('Welcome to login') Else: Print('Password input error, please re-enter') user_passwd= Input ('Please enter your password for the second time') ifUSER_PASSWD = ='123456': Print('Welcome to login') Else: Print('Password input error, please re-enter') user_passwd= Input ('Please enter the password in the third') ifUSER_PASSWD = ='123456': Print('Welcome to login') Else: Print('too many errors, login failed')Else: Print('user name does not exist')
Python variable names, data types and simple operations, conditional statements, loops and exercises