一、變數和簡單資料型別
1.1運行hello world
print("Hello python world!")
1.2變數
添加一個變數
massage = "Hello python world!"
print(massage)
1.2.1變數的命名和使用
變數名只能包含字母、數字和下劃線,
- 變數可以命名為message_1,但不能是1_message
- 變數名不能包含空格 greeting_message 可以 greeting message 不行
1.3字串
可以用單引號也可以用雙引號
"This is a string."
'This is also a string.'
1.3.1使用方法修改字串的大小寫
name = "ada lovelace"
print(name.title())
| 方法 | 功能 |
|---|---|
| title() | 首字母大寫 |
| upper() | 全部大寫 |
| lower() | 全部小寫 |
1.3.2合并字串
python用+來合并字串
first_name ="ada"
last_name="lovelace"
full_name=first name+" "+last name
print(full_name)
print("Hello, "+full_name.title()+"!")
1.3.3使用制表符或換行符來添加空白
print("\tpython")
python
\n換行
洗掉空白
| 方法 | 功能 |
|---|---|
| rstrip() | 剔除字串末尾空白 |
| lstrip() | 剔除字串開頭空白 |
| strip() | 剔除字串首尾空白 |
favorite_language=' python '
favorite_language.rstrip()
' python'
1.4數字
1.4.1整數
2+3=5
2*3=6
2**3=8 //**表示乘方運算
1.4.2浮點數
0.1+0.2=0.3
1.4.3使用函式str()避免型別錯誤
age = 23
message = "Happy "+str(age)+"rd Birthday!"
print(message)
如果不用str的話,那么輸出就會報錯,因為python不清楚到底是23還是2和3
1.4.4 python之禪
在Python終端執行import this

python代碼的制導原則
二、串列簡介
2.1串列
串列由一系列按特定順序排列的元素組成,用[]表示串列,用逗號分隔其中的元素,
bicycles=['trek','cannondale','redline']
print(bicycles)
結果為
['trek','cannodale','redline']
2.1.1訪問串列元素
bicycles=['trek','cannondale','redline']
print(bicycles[0])
結果為
trek
記住:索引是從0而不是從1開始的
三、操作串列
3.1遍歷整個串列
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
結果為
alice
david
carolina
3.1.1深入研究回圈
for cat in cats:
for dog in dogs:
3.1.2在for回圈中執行更多的操作
magicians = ['alice','david','carolina']
for magician in magicians: //這里的冒號一定不要遺忘哦
print(magician.title()+",that was a great trick!")
只要print縮進了,因此它們都將針對串列的每位魔術師執行一次,
第一二三次迭代的抬頭分別為 Alice David Carolina
3.1.3在for回圈結束后執行一些操作
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician.title()+",that was a great trick!")
print("Thank you.")
那么Thank you只會在最后出現一次
3.2創建數值串列
3.2.1使用函式range()
for value in range(1,5):
print(value)
結果為:
1
2
3
4
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
結果為
[1,4,9,16,25,36,49,64,81,100]
3.5元組
不可變的串列稱為元組
第四章 if陳述句
4.1 if陳述句
if conditional_test:
do something
age = 19
if age>=18:
print("You are old enough to vote!")
4.2if else 陳述句
age = 17
if age>=18:
print("you are old enough to vote.")
else:
print("sorry.")
4.3 if-elif-else結構
age=12
if age<4:
print("your admission cost is $0.")
elif age<18:
print("your admission cost is $5.")
else:
print("your admission cost is $10.")
4.4 省略else代碼塊
age =12
if age<4:
price = 0
elif age<18:
price = 5
elif age<65:
price=10
elif age >=65:
price=5
print(Your admission cost is $"+str(price)+".”)
4.5測驗多個條件
if-elif-else結構功能強大,但僅適合用于只有一個條件滿足的情況,遇到通過了的測驗后,Python就會跳過余下的測驗,當必須檢查所有條件時,應用一系列不包含elif和else代碼塊的簡單if陳述句,
requested_toopings=['mushrooms','extra cheese']
if'mushirooms' in requested_toppings:
print("adding mushrooms.")
if"pepperoni' in requested_toppings:
print("adding pepperoni.")
if'cheese' in requested_toppings:
print("adding cheese.)
adding mushrooms.
adding pepperoni.
addomg cheese.
4.6檢查特殊元素
requested_toppings=['mushrooms','green peppers','cheese']
for requested_topping in requested_toppings:
print("adding "+requested_topping+".")
結果為:
adding mushrooms.
adding green peppers.
adding cheese
第五章 字典
5.1一個簡單的字典
aline_0 = {'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
結果為
green
5
第六章 用戶輸入和while回圈
6.1函式Input()的作業原理
函式input()讓程式暫停運行,等待用戶輸入一些文本,獲取用戶輸入后,python將其存盤在一個變數中,以方便使用,
name = input("please enter your name: ")
print("hello, "+name+"!")
運行區:

6.1.1使用int()來獲取數值輸入
在Python解釋器里

如果不加
age = int(age)這一行 就會報錯 因為系統不知道20是一個數字還是2和0
6.2while回圈簡介
6.2.1使用while回圈
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
6.2.1讓用戶選擇何時退出
prompt = "n\Tell me something ,and i will repeat it back to you:"
prompt+= "n\Enter 'quit' to end the program."
message = ""
while message != 'quit':
message = input(prompt)
print(message)
當輸入quit時,回圈才結束
6.2.2使用標志
在前一個示例中,我們讓程式在滿足指定條件時就執行特定任務,但在更復雜的程式中,很多不同的事件都會導致程式停止運行,
在要求很多條件都滿足才繼續運行的程式中,可定義一個變數,用于判斷整個程式是否處于活動狀態,這個變數被稱為標志,
prompt = "n\Tell me something ,and i will repeat it back to you:"
prompt+= "n\Enter 'quit' to end the program."
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
第七章 函式
7.1定義函式
def greet_user():
print("Hello!")
greet_user() //呼叫函式
7.1.1向函式傳遞資訊
def greet_user(username):
print("Hello, "+username.title()+"!")
greet_user('萌噠噠的CXY')
結果:
Hello, 萌噠噠的Cxy!
7.1.2實參和形參
前面定義函式greet_user時,要求給變數username指定一個值,呼叫這個函式并提供這種資訊(人名)
username 是一個形參
萌噠噠的CXY是實參
7.2傳遞實參
向函式傳遞實參的方式很多,可使用位置實參,這要求實參的順序與形參的順序相同,也可使用關鍵字實參,
7.2.1位置實參
def describe_pet(animal_type,pet_name):
print("\nI hava a "+animal_type+".")
print("Its name is "+pet_name+".")
describe_pet('cat','coco')
結果為

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/265399.html
標籤:python
