Python beginners Summary 1. python beginners Summary
Python code runtime environment: PyCharm 2017.1.3
Age = 1.7 print (type (age) output is <class 'int'>
Age = 1.7 print (type (age) output is <class 'float'>
Age = input ("age:") Even if the number is 17, print (type (age) Outputs <class 'str'>
Therefore, age = int (input ("age:") must be used to convert it to the int type.
The following are three types of codes with the same output:
Name = input ("name = ")
Age = int (input ("age = "))
Job = input ("job = ")
Info1 = '''
---------- Info1 of % s -----
Name: % s
Age: % d
Job: % s
''' % (Name, name, age, job)
Info2 = '''
---------- Info2 of {_ name }-----
Name: {_ name}
Age: {_ age}
Job: {_ job}
'''. Format (_ name = name,
_ Age = age,
_ Job = job)
Info3 = '''
---------- Info3 of {0 }-----
Name: {0}
Age: {1}
Job: {2}
'''. Format (name, age, job) # format
Print (info1)
print(info2)
print(info3)
Print (type (info) # <class 'str'>
Partial output:
Name = mumu
Age = 0
Job = st
---------- Info1 of mumu -----
Name: mumu
Age: 0
Job: st
In python, you can use for... else and while... else, for example:
age= 17
for i in range (3):
guess_age = int(input("guess age:"))
if guess_age == age:
print("yes,you got it!")
break
elif guess_age > age:
print("Think smaller...")
else:
print("Think bigger...")
else:
print("you have trid too many times")
range:
For I in range (, 2) indicates that I starts from 0, increments by 2 each time, ends by 10, and does not take 10. If print (I), the result is: 0 2 4 6 8