Python基礎知識
今天給大家分享一些Python的基礎知識,想要蓋好大房子,不把地基打扎實打牢怎么行呢?所以,今天咱們就來學習基礎知識,
這樣后期學習Python的時候才能更容易掌握,更輕松的學會Python的使用,別跟我說你都學過了,看完再告訴我…

一、編程基礎
1.基本的輸入輸出:
print("Hello World"); Name = input('請輸入您的姓名:'); print(Name); D:\作業空間\Python\venv\Scripts\python.exe D:/作業空間/Python/main.py Hello World 請輸入您的姓名:Alice Alice 行程已結束,退出代碼0
2.變數:
Python學習交流Q群:906715085### print("-------------輸出陳述句-------------"); message="Hello Python world"; print(message); print("-------------首字母大寫-------------"); name="ada lovelace"; print(name.title()); print("-------------大小寫-------------"); print(name.upper()); print(name.lower()); print("-------------拼接字串-------------"); first_name = "ada" last_name = "lovelace" full_name = first_name + " " + last_name print(full_name); print("-------------添加空白-------------"); print("\tPython"); print("Languages:\nPython\nC\nJavaScript"); print("-------------洗掉空白-------------"); print("Hello ".rstrip()); print("-------------運算-------------"); print(2+3); print(3-2); print(2*3); print(3/2); print(3**2); print(3**3); print(10**6); print(0.1+0.1); print(0.2+0.2); print("------------注釋-------------"); # 測驗注釋

Python學習交流Q群:906715085### -------------輸出陳述句------------- Hello Python world -------------首字母大寫------------- Ada Lovelace -------------大小寫------------- ADA LOVELACE ada lovelace -------------拼接字串------------- ada lovelace -------------添加空白------------- Python Languages: Python C JavaScript -------------洗掉空白------------- Hello -------------運算------------- 5 1 6 1.5 9 27 1000000 0.2 0.4 ------------注釋------------- Process finished with exit code 0

3.基本運算子
print("-----------------算數運算子-----------------"); #+ 加,兩個物件相加 #- 減,得到負數或是一個數減去另一個數 #* 乘,兩個數相乘或是回傳一個被重復若干次的字串 #x/y 除,x 除以 y #% 取模,回傳除法的余數 #// 取整除,回傳商的整數部分 #x**y 冪 print(12+13); print(12-13); print(12*13); print(12/13); print(12/13); print(12%13); print(12//13); print(12**13); print("-----------------比較運算子-----------------"); #== 等于,比較物件是否相等 (a == b)回傳 False #!= 不等于,比較兩個物件是否不相等 (a != b)回傳 True #<> 不等于,比較兩個物件是否不相等 (a <> b)回傳 True,這個運算子類似 != #x > y 大于,回傳 x 是否大于 y (a > b)回傳 False #x < y小于,回傳 x 是否小于 y,所有比較運算子回傳 1表示真,回傳 0 表示假,這分別與特殊的變數 True 和 False 等價,注意這些變數名的大小寫(a < b)回傳 True #x >= y 大于等于,回傳 x 是否大于等于 y (a >= b)回傳 False #x <= y 小于等于,回傳 x 是否小于等于 y (a <= b)回傳 True print(12>13); print(12>=13); print(12<13); print(12<=13); print(12!=13); print(12==13); print("-----------------賦值運算子-----------------"); #= 簡單的賦值運算子 c = a + b 將 a + b 的運算結果賦值為 c #+= 加法賦值運算子 c += a 等效于 c = c + a #-= 減法賦值運算子 c-= a 等效于 c = c-a #*= 乘法賦值運算子 c *= a 等效于 c = c * a #/= 除法賦值運算子 c /= a 等效于 c = c / a #%= 取模賦值運算子 c %= a 等效于 c = c % a #**= 冪賦值運算子 c **= a 等效于 c = c ** a #//= 取整除賦值運算子 c //= a 等效于 c = c // a a=21; b=10; c=0; c+=a; print(c); c*=b; print(c); c/=a; print(c); c-=b; print(c); c=2; c%=a; print(c); c**=a; print(c); c//=a; print(c); print("-----------------位運算子-----------------"); #& 按位與運算子 (a & b)輸出結果 12,二進制解釋:0000 1100 #| 按位或運算子 (a | b)輸出結果 61,二進制解釋:0011 1101 #^ 按位異或運算子 (a ^ b)輸出結果 49,二進制解釋:0011 0001 #~ 按位取反運算子 #(~a)輸出結果?61,二進制解釋:1100 0011, #在一個有符號二進制數的補碼形式 #<< 左移動運算子 a << 2 輸出結果 240,二進制解釋:1111 0000 #>> 右移動運算子 a >> 2 輸出結果 15,二進制解釋:0000 1111 a=60; b=13; c=0; c=a&b; print(c); c=a|b; print(c); c=a^b; print(c); c=~a; print(c); c=a<<2; print(c); c=a>>2; print(c); print("-----------------邏輯運算子-----------------"); #x and y 布爾“與”,如果 x 為 False,x and y 回傳 False,否則它回傳 y 的計算值 #x or y 布爾“或”,如果 x 是 True,它回傳 True,否則它回傳 y 的計算值 #x not y 布爾“非”,如果 x 為 True,回傳 False,如果 x 為 False,它回傳 True a=10; b=20; print(a and b); a=0; print(a and b);

D:\作業空間\Python\venv\Scripts\python.exe D:/作業空間/Python/main.py -----------------算數運算子----------------- 25 -1 156 0.9230769230769231 0.9230769230769231 12 0 106993205379072 -----------------比較運算子----------------- False False True True True False -----------------賦值運算子----------------- 21 210 10.0 0.0 2 2097152 99864 -----------------位運算子----------------- 12 61 49 -61 240 15 行程已結束,退出代碼0

二、控制流程
1.選擇結構
print("-------------檢查是否相等-------------"); car='bmw'; print(car=='bmw'); print(car=='audi'); print(car=='BMW'); print(car.upper()=='BMW'); age=18; print(age==18); print(age>=18); print(age<=18); print(age>18); print(age<18); age_0 = 22; age_1 = 18; print(age_0 >= 21 and age_1 >= 21); print(age_0 >= 21 or age_1 >= 21); print("-------------if陳述句-------------"); age = 19 if age >= 18: print("You are old enough to vote!"); age=17 if age>=18: print("You are old enough to vote!"); else: print("Sorry you are too young"); 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.") 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) + ".") -------------檢查是否相等------------- True False False True True True True False False False True -------------if陳述句------------- You are old enough to vote! Sorry you are too young Your admission cost is $5. Your admission cost is $5. Process finished with exit code 0


2.回圈結構
Python學習交流Q群:906715085#### print("-------------函式input()的作業原理-------------"); message = input("Tell me something, and I will repeat it back to you: ") print(message) print("-------------撰寫清晰的程式-------------"); name=input("Please enter your name:"); print("Hello,"+name+"!"); print("-------------求模運算子-------------"); print(4%3); print("-------------while回圈-------------"); current_number = 1 while current_number <= 5: print(current_number) current_number += 1 print("-------------讓用戶選擇何時退出-------------"); prompt = "\nTell me something, and I will repeat it back to you:" prompt += "\nEnter 'quit' to end the program. " message = "" while message != 'quit': message = input(prompt) print(message) print("-------------break陳述句-------------"); prompt = "\nPlease enter the name of a city you have visited:" prompt += "\n(Enter 'quit' when you are finished.) " while True: city = input(prompt) if city == 'quit': break else: print("I'd love to go to " + city.title() + "!") -------------函式input()的作業原理------------- Tell me something, and I will repeat it back to you: Hello World Hello World -------------撰寫清晰的程式------------- Please enter your name:Alice Hello,Alice! -------------求模運算子------------- 1 -------------while回圈------------- 1 2 3 4 5 -------------讓用戶選擇何時退出------------- Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. Hello World Hello World Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. quit quit -------------break陳述句------------- Please enter the name of a city you have visited: (Enter 'quit' when you are finished.) ShangHai I'd love to go to Shanghai! Please enter the name of a city you have visited: (Enter 'quit' when you are finished.) quit Process finished with exit code 0


三、資料型別
1.字串
print("-------------字串操作-------------"); #coding=utf-8 str = 'Hello World!' print(str) # 輸出完整字串 print(str[0]) # 輸出字串中的第一個字符 print(str[2:5]) # 輸出字串中第三個至第五個之間的字串 print(str[2:]) # 輸出從第三個字符開始的字串 print(str * 2) # 輸出字串兩次 print(str + "TEST") # 輸出連接的字串 print("-------------格式化輸出-------------"); x="歡迎您,%s,當前第%d 次訪問! " y=x%("小明",1) #y=("歡迎您,%s,當前第%d 次訪問! "%("小明",1)),以上兩行可以合并為這一行, print(y) print("-------------三引號-------------"); hi = '''hi there''' print(hi) # str() D:\作業空間\Python\venv\Scripts\python.exe D:/作業空間/Python/main.py -------------字串操作------------- Hello World! H llo llo World! Hello World!Hello World! Hello World!TEST -------------格式化輸出------------- 歡迎您,小明,當前第1 次訪問! -------------三引號------------- hi there 行程已結束,退出代碼0

最后

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