Python day 1, python get the first day of this week
The first line of var1.py code:
1 #! Usr/bin/env python2 #-*-coding: UTF-8-*-3 # Author calmyan4 print ('Hello World ') 5 name = 'yan' 6 name2 = name7 print ('My name: ', name, name2) 8 name = 'utem '9 print ('My name :', name, name2)
Compared with C, C is more clean and easy to understand!
1 #! Usr/bin/env python 2 # Author calmyan 3 import getpass 4 name = "calmyan" 5 password = "123abc" 6 7 username = input ("nsername :") 8 userpassword = getpass. getpass ("password:") 9 # userpassword = input ("password:") 10 if name = username and password = userpassword: 11 print ("Welcome to {name} login ". format (name = username) 12 else: 13 print ("incorrect user name or password ")
If you use pyCharm to run getpass on Windows 10, you can run it normally with cmd!
Three methods for multi-line character output (int type requires forced conversion ):
1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 name = input ("name :") 5 age = int (input ("age:") # int forced conversion 6 job = input ("job:") 7 salary = input ("salary :") 8 9 # info = '''10 # --------- info of ''' + name + ''' --------- 11 # Name: ''' + name + ''' 12 # Age: ''' + age + ''' 13 # Job: ''' + job14 15 # info = ''' 16 # ------ info of % s -------- 17 # Name: % s18 # Age: % d19 # Job: % s20 # salary: % s21 # ''' % (name, name, age, job, salary) 22 23 info2 = ''' 24 ------ info of {_ name} -------- 25 Name: {_ name} 26 Age: {_ age} 27 Job: {_ job} 28 salary: {_ salary} 29 '''. format (_ name = name, 30 _ age = age, 31 _ job = job, 32 _ salary = salary) 33 info3 = ''' 34 ------ info of {0} -------- 35 Name: {0} 36 Age: {1} 37 Job: {2} 38 salary: {3} 39 '''. format (name, 40 age, 41 job, 42 salary) 43 print (info2) 44 print (info3)
Comparison of while and for loops:
1 #! Usr/bin/env python 2 # Author calmyan 3 4 5 age_name = 45 6 7 count = 0 8 while count <= 3: 9 guess_age = int (input ("age: ") 10 if guess_age = age_name: 11 print (" yes, you got it ") 12 break13 elif guess_age> age_name: 14 print (" think smaller .... ") 15 else: 16 print (" think bigger... ") 17 count = count + 118 else: 19 print (" You tried too many times! ") 20 21 for I in range (3): 22 guess_age = int (input (" age: ") 23 if guess_age = age_name: 24 print (" yes, you got it ") 25 break26 elif guess_age> age_name: 27 print (" think smaller .... ") 28 else: 29 print (" think bigger... ") 30 else: 31 print (" You tried too many times! ")
Optimization of loops can be more humane:
1 #! Usr/bin/env python 2 # Author calmyan 3 4 5 age_name = 45 6 count = 0 7 while count <= 3: 8 guess_age = int (input ("age :")) 9 if guess_age = age_name: 10 print ("yes, you got it") 11 break12 elif guess_age> age_name: 13 print ("think smaller .... ") 14 else: 15 print (" think bigger... ") 16 count = count + 117 if count = :18 countine_confirm = input (" do you want to keep guessing ..? ") # Continue! 19 if countine_confirm! = 'N': 20 count = 021 else: 22 print ("You tried too many times! ")
The first day I learned the basic syntax, which is relatively easy to understand!