[Development] Pyhthon: acquaintance Python

A variable

Declare variables

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:21
# example20.py
# PyCharm

name = "caidongsheng"

Variables defined rules

  • Variable names can be any combination of letters, numbers or underscore
  • The first character variable names can not be digital
  • The following keywords can not be declared as a variable name. [ 'And', 'as',' assert ',' break ',' class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', ' finally ',' for ',' from ',' global ',' if ',' import ',' in ',' is', 'lambda', 'not', 'or', 'pass',' print ' , 'raise', 'return', 'try', 'while', 'with', 'yield']

Assignment of variables

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:21
# example20.py
# PyCharm

name = "caidongsheng"
name2 = name
print(name,name2) ## caidongsheng caidongsheng
name = "lixue"
print("name2 is ",name2) ##name2 is caidongsheng

Second, the process control

# ! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7     
# - UTF-8 - *: - - * Coding      
# caidongsheng 
# 2019-09-08 21:21 
# example20.py 
# PyCharm 


# prompted for a user name and password 
# to authenticate the user name and password 
#      If an error, the output user name or password is incorrect 
#      If successful, the output welcome, XXX! 


Import getpass 

name = the iNPUT ( ' Please enter your user name: ' )      # #INPUT input acquisition the values are a string! ! ! 
= getpass.getpass pwd ( ' Enter password: ' ) 

IF name == " caidongsheng"  And pwd == " cmd " :
     Print ( " Welcome, caidongsheng " )
 the else :
     Print ( " user name and password error " )

Three, while circulation

Syntax Rules

while conditions: 

# loop 
# If the condition is true, then the body of the loop is executed 
# If the condition is false, the loop is not executed

break

  break is used to exit all cycles

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:37
# example21.py
# PyCharm

while True:
    print("123")
    break
    print("456")


输出结果:
123

continue

  continue to exit the current cycle, the next cycle continues

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:37
# example21.py
# PyCharm

while True:
    print("123")
    continue
    print("456")

输出结果:
123 123 ...

 

Fourth, exercises

1, using input while loop 1234568910

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm

num = 1
while num <= 10:
    if num == 7:
        pass
    else:
        print(num)
    num += 1

2, find all the numbers from 1 to 100 and

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm


sum = 0

i = 1
while  i <= 100:
    sum = sum + i
    i = i + 1

print(sum)

3, the output of all the odd 1-100

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm


sum01 = 0
j = 1
while  j <= 100:
    if j % 2 == 1:
        sum01 = sum01 + j
    else:
        pass
    j = j + 1

print("1到100,奇数和是: ",sum01)

4, the output of all the even-numbered 1-100

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm


sum02 = 0
j1 = 1
while  j1 <= 100:
    if j1 % 2 == 0:
        sum02 = sum02 + j1
    else:
        pass
    j1 = j1 + 1

print("1到100,偶数和是: ",sum02)

5, find all the numbers and 1-2 + 3-4 + 5 ... 99

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm


sum03 = 0
sum04 = 0

j2 = 1

while  j2 <= 99:
    if j2 % 2 == 0:
        sum03 = sum03 - j2
    else:
        sum04 = sum04 + j2
    j2 = j2 + 1

print("和是: ",sum04+sum03)

6, user login (three chances to retry)

 

# ! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7     
# - * - Coding: UTF-8 - * -      
# caidongsheng 
# 2019-09-08 10:07 
# example19.py 
# PyCharm 
user_name the iNPUT = ( " Please enter your user name: " )
. 1 = Loop 
the while Loop <=. 3:
USER_NAME = INPUT ( "Please enter your name:")
user_pass = INPUT ( "Enter password:")
IF USER_NAME == "caidongsheng" and user_pass == "123456":
Print ( " Welcome !! ")
BREAK
the else:
Print (" user or password is wrong !! ")
Loop Loop + 1 =
 

 

Guess you like

Origin www.cnblogs.com/caidongsheng94/p/11488745.html