- 學習前儀式
print("hello word!")
ps: 熟練各種語言的hello world書寫, 嘻嘻 - 注釋
單行注視:# 被注釋內(nèi)容
多行注釋:""" 被注釋內(nèi)容 """
或者'''被注釋內(nèi)容'''
- 變量
變量定義的規(guī)則:
變量名只能是 字母、數(shù)字或下劃線的任意組合
變量名的第一個字符不能是數(shù)字
以下關(guān)鍵字不能聲明為變量名
['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']
代碼
# Author:Scott She
name = "Scott She"
name2 = name
print("My name is ", name, ", again, my name is ", name2)
name = "馬云"
print(name, name2)
# 單引號和雙引號基本一樣, 三個單(或雙)引號還能打印多行, 也可以注釋多行
lines = """
name = "馬云"
print(name, name2)
"""
print(lines)
打印
- 用戶輸入及字符串格式化
代碼
# Author:Scott She
name = input("name ")
age = int(input("age "))
job = input("job ")
salary = float(input("salary "))
print(type(name), type(age), type(job), type(salary))
info1 = """
---------------- info of %s ------------------
Name: %s
Age: %d
Job: %s
Salary: %.1f
""" % (name, name, age, job, salary)
print(info1)
info2 = '''
---------------- info of {_name} ------------------
Name: {_name}
Age: {_age}
Job: {_job}
Salary: {_salary}
'''.format(_name=name,
_age=age,
_job=job,
_salary=salary)
print(info2)
info3 = '''
---------------- info of {0} ------------------
Name: {0}
Age: {1}
Job: {2}
Salary: {3}
'''.format(name,
age,
job,
salary)
print(info3)
# 以上三種字符串格式化, 一般都會用第二種
打印
- if 語句及密文
代碼
# Author:Scott She
import getpass # 模塊
_username = "scott"
_password = "666666"
username = input("username ")
password = getpass.getpass("password ") # 注意, 該方法在PyCharm中不能運行,可以用終端測試
if _username == username and _password == password:
print("Wellcom {name} login...".format(name=username))
else:
print("Invalid username or password!")
終端測試
- 循環(huán)語句
# Author:Scott She
age_of_scott = 25
# while循環(huán)
'''
count = 0
while count < 3:
guess_age = int(input("age "))
if guess_age == age_of_scott:
print("yes, you got it")
break
elif guess_age > age_of_scott:
print("think smaller...")
else:
print("think bigger...")
count += 1
else:
print("you have tried too many... fuck off")
'''
# for循環(huán)
'''
for i in range(3):
guess_age = int(input("age "))
if guess_age == age_of_scott:
print("yes, you got it")
break
elif guess_age > age_of_scott:
print("think smaller...")
else:
print("think bigger...")
else:
print("you have tried too many... fuck off")
'''
# for循環(huán)詳解
'''
類似于其他語言中的
for (int i = 0; i < 10; i+=2) {
print(i)
}
'''
for i in range(0, 10, 2):
print(i)
count = 0
while count < 3:
guess_age = int(input("age "))
if guess_age == age_of_scott:
print("yes, you got it")
break
elif guess_age > age_of_scott:
print("think smaller...")
else:
print("think bigger...")
count += 1
if count == 3:
print("Do you want to keep guessing... n or N for no, others for yes: ")
continue_confirm = input()
if continue_confirm != "n" and continue_confirm != "N":
count = 0
打印
- 三元運算
result = 值1 if 條件 else 值2
如果條件為真:result = 值1
如果條件為假:result = 值2
由于簡書格式原因, 代碼均沒有空行, 可以去這里下載我的練習代碼