Python learning notes and python learning notes
Variable
Names are separated by commas;
You can modify the name of a constant in uppercase;
# Author:Csicsi
print("hello world!")
name = "Csicsi"
name2 = name
print("My name is", name, name2)
name = "L"
print(name,name2)
friend_of_Csicsi = "S"
FriendOfCsicsi = "S"
HOME = "M"
print(FriendOfCsicsi, "lives in", HOME, "with", name, "and", name2)
Differences and introduction of character encoding
Each character occupies one ASCII code byte. Each character occupies, 1 bytes Unicode, and each character occupies two bytes. UTF-8 can be a variable-length Unicode character. English characters are saved in ascii code, chinese characters in three bytes python2 cannot be directly written into Chinese python3 can be directly written into Chinese
User interaction program
All input values are strings (raw_input in py2 and input in py2 is missing) % s string % d integer % f Floating Point info = ''' ---------------- info ------------------ Name: % s; age: % s; job: % s; Salary: % s; ''' % (name, age, job, salary) print (info) info2 = ''' ---------------- info2 ------------------ Name: {_ name} Age: {_ age} job: {_ job} Salary: {_ salary }'''. format (_ name = name, _ age = age, _ job = job, _ salary = salary) info3 = ''' ------------------- info3 ----------------- Name: {0} Age: {1} job: {2} Salary: {3 }'''. format (name, age, job, salary)
% S indicates string, % d indicates that only numbers can be entered, to help detect Data Types
# Author:Csicsi
username = input("username:")
password = input("password:")
print(username,password)
name = input("Name:")
age = int(input("Age:"))
job = input("Job:")
salary = input("Salary:")
info = '''
------- info of ''' + name + ''' ---------
Age:''' + str(age) + '''
Job:''' + job + '''
Salary:''' + salary + '''
'''
print(info)
info2 = '''
------- info2 of %s ---------
Age:%d
Job:%s
Salary:%s
''' %(name,age,job,salary)
print(info2)
info3 = '''
------- info3 of {_name} ---------
Age:{_age}
Job:{_job}
Salary:{_salary}
'''.format(_name=name,
_age=age,
_job=job,
_salary=salary)
print(info3)
info4 = '''
------- info4 of {0} ---------
Age:{1}
Job:{2}
Salary:{3}
'''.format(name,age,job,salary)
print(info4)
If else process judgment
"=" Indicates a value assignment, and "=" indicates that getpass cannot be implemented in Pycharm. In CMD, You can execute [Forced indent of python]-in python, force indent is used to represent the conclusion. -Indentation error indicates an indent ERROR-all codes must be written in the top level if they are the top level.
# Author: csicsi
import getpass
_username = 'csicsi'
_password = '1234'
username = input("username=")
#password = getpass.getpass("password")
password = input("password=")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
else:
print("Invalid username or password!")
# Author: csicsi
age_of_csicsi = 23
count = 0
while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_csicsi:
print("You are a genius! csicsi is {age}.".format(age=guess_age))
break
elif guess_age > age_of_csicsi:
print("try younger...")
else:
print ("try older!")
count += 1
if count == 3:
continue_confirmation = input("Do you want to keep trying?")
if continue_confirmation != 'n':
count = 0
#else:
# print("You've tried too many times... Bye...")
For Loop and job requirements
Continue jumps out of this loop and enters the next loop. break ends the entire loop.
# Author: csicsi
for i in range(10):
print('-----------',i)
for j in range(10):
print(j)
if j > 5:
break
# Author: csicsi
age_of_csicsi = 23
for i in range(3):
guess_age = int(input("guess age of csicsi:"))
if guess_age == age_of_csicsi:
print("You are a genius! csicsi is {age}.".format(age=guess_age))
break
elif guess_age > age_of_suzy:
print("try younger...")
else:
print("try older!")
else:
print("You've tried too many times... Bye...")