1. 程式人生 > >自學Python快速入門

自學Python快速入門

標識 sel 存儲 創建方式 基本語法 變量名 t_sql 負數 www

1 helloworld
#基本語法
print("hello")

#換行
print(‘1221312\
12312312\
2312312321312\
21312312‘)


##表示註釋

‘‘‘
多行註釋
‘‘‘

print(123+423)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2 變量和標識符
#py中南使用變量,不需要聲明,直接為變量賦值即刻
# a = 10

#不能使用對沒有定義的變量
# b = 3
# print(b)


#python動態類型語言,可以為變量賦任意類型的值,也可以任意修改任意類
# my = ‘aaa‘
# print(my)

#標識符
# 在py中所有可以自主命名的內容都屬於標識符
# 比如 變量名,函數名,類名

# b_1 = 20
# print(b_1)


#數值
#可以表示任意大小的數據 下劃線做分隔符
#數字不能以0開頭 d = 0123
c = 999999_999999_999999999999
print(c)
#二進制 0b開頭
#八進制 0o開頭
#十六進制 0x開頭
#只要是數據,都是十進制顯示

5 類型轉換
#類型轉換
#四個函數 int() float() str() bool()
a = ‘hello‘
b = 123
print(a + str(b))
1
2
3
4
5
6 類型檢查
#類型檢查 type(v) 函數
a = 123
b = ‘123‘
print("a:",a,type(a))
print("b:",b,type(b))
1
2
3
4
5
7 運算符
#算術運算符
# +
# -
# *
# /
# a = 10 + 5
# print(a)
#
# a = 10 * 3
# print(a)

#賦值運算符
# = 可以將等號右側的值賦值給等號左側的變量
# +=
# -=
# *=
# **=
# /=
# //=
# %=

# a = 4
# a *= 5
# print(a)

#關系運算符
# 比較兩個值之間的關系,總會返回一個布爾值
# >
# >=
# <
# <=
# ==
# !=

# r = 10 > 20
# print(r)


#邏輯運算符
# not 邏輯非
# and 邏輯與
# or 邏輯或

# a = True
# b = not a
# print(a)

# True and True 返回原值
#如果第一個值是False,則返回第一給值,否則返回第二個值
# re = 1 and 2
# # True and False 返回原值
# re = 1 and 0
# print(re)


#條件運算符
print(‘你好‘) if False else print(‘hello‘)

a = 10
b = 20
print(‘a big!‘) if a > b else print(‘b big!‘)

#a 和 b 誰大就打印誰
max = a if a > b else b
print(max)#切片是從現有的列表,獲取一個子列表

student = [‘cxl‘,‘ywk‘,‘gql‘]

#print(student[-1]) # -1表示倒數第一個

#通過切片來獲取指定元素
#列表[起始:結束]
print(student[0:2])
print(student[:2]) #如果第一個是第一個值,可以不用寫

#步長 列表[起始:結束:步長] 默認是1,間隔,不能是0,可以是負數(倒過來)
print(student[0:3:2])
r = range(5) #生成由自然數的的序列,起始位置,結束位置,步長
print(list(r))


a = range(3,100,3)
print(list(a))


#配合for循環使用
for i in range(10):
print(f"打印{i}次")

元祖
#元組 tuple
# 不可變的序列
# 操作方式基本上和列表是一致
# 當做不可變的列表就行了
# 希望我們數據不會改變的時候,使用元組
# 創建方式 ()
e = (1,2,3,4,5,6)
print(e)

字典
#數據結構 稱為映射()
# K/V方式存儲

# 創建{}
d = {1:"我是1",2:"我是2"}
print(d,type(d))
print(d.get(1))
print(type(d.keys()))

for i in d.keys():
print(d[i])


s = d.pop(2)
print("--------",s)

for i in d.keys():
print(d[i]

13 列表常用方法
student = [‘cxl‘,‘ywk‘,‘gql‘,‘ywk‘]
number = [1,2,3]

# + and *
# mylist1 = [1,2,3] + [4,5,6]
# print(mylist1)
#
# mylist2 = [1,2,3] * 10
# print(mylist2)


# in and not in
# in 是否存在列表中南

# print(‘cxl‘ in student) #True
# print(‘cxl‘ not in student) #False


#min() and max() 獲取最大值和最小值
# print(min(number))
# print(max(number))


#index() and count()
#獲取第一次出現的位置(‘str‘,s,e)
# s:表示查找的起始位置
# e:表示查找的結束位置
print(student.index(‘cxl‘))

print(student.count(‘ywk‘)) #數量
#用python完成計算器

print("歡迎使用計算器")
print("1.加法 2.減法 3.乘法 4.除法")

choose = input("請輸入要進行的操作:")

flag = True

while flag:
if choose==‘1‘ :
add1 = int(input("請輸入加數1:"))
add2 = int(input("請輸入加數2:"))
print("結果為:",add1+add2)
flagStr = input("是否繼續y/n")
if flagStr == ‘y‘:
flag = True
print("1.加法 2.減法 3.乘法 4.除法")
choose = input("請輸入要進行的操作:")
else:
flag = False
elif choose==‘2‘ :
j1 = int(input("請輸入減數1:"))
j2 = int(input("請輸入減數2:"))
print("結果為:",j1-j2)
flagStr = input("是否繼續y/n")
if flagStr == ‘y‘:
flag = True
print("1.加法 2.減法 3.乘法 4.除法")
choose = input("請輸入要進行的操作:")
else:
flag = False
elif choose==‘3‘ :
c1 = int(input("請輸入因數1:"))
c2 = int(input("請輸入因數2:"))
print("結果為:",c1*c2)
flagStr = input("是否繼續y/n")
if flagStr == ‘y‘:
flag = True
print("1.加法 2.減法 3.乘法 4.除法")
choose = input("請輸入要進行的操作:")
else:
flag = False
elif choose==‘4‘ :
f1 = int(input("請出入除數1:"))
f2 = int(input("請出入除數1:"))
print("結果為:",f1/f2)
flagStr = input("是否繼續y/n")
if flagStr == ‘y‘:
flag = True
print("1.加法 2.減法 3.乘法 4.除法")
choose = input("請輸入要進行的操作:")
else:
flag = Fals

import pymysql

connection = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘,
password=‘root‘, db=‘test‘,charset=‘utf8‘,
cursorclass=pymysql.cursors.DictCursor)
cur = connection.cursor()

while True :
print("==============================================")
print("1.列表 2.修改 3.添加 4.刪除 5.建表 6.關閉連接")
print("7.九九乘法表 8.計算器")
choose = input("請輸入您的選擇")
if choose == "1":
print("用戶列表")
sql = "SELECT * FROM user_py"
# 執行SQL語句
cur.execute(sql)
# 獲取所有記錄列表
results = cur.fetchall()
print(results)
elif choose == "2":
print("====修改====")
id = input("修改ID:")
sql = "SELECT * FROM user_py WHERE id = "+id
# 執行SQL語句
cur.execute(sql)
# 獲取所有記錄列表
results = cur.fetchall()
print("你要修改這條數據 > ",results)
userName = input("input UserName > :")
userAge = input("input UserAge > :")
update_sql = f"""UPDATE user_py SET USER_NAME =‘{userName}‘, USER_AGE={userAge} WHERE `ID`= {id}"""
cur.execute(update_sql)
print("更新成功!")
elif choose == "3":
print("====插入====")
id = input("input id > :")
userName = input("input UserName > :")
userAge = input("input UserAge www.dasheng178.com> :")
insert_sql = f"""INSERT INTO USER_PY(`ID`,`USER_NAME`,`USER_AGE`)values({id},"{userName}",{userAge})"""
print(insert_sql)
cur.execute(insert_sql)
print("插入成功")
elif choose == "4":
print("====刪除====")
id = input(" input delete id > :")
delete_sql ="DELETE FROM USER_PY WHERE ID = "+ id
cur.execute(delete_sql)
print("刪除成功")
elif choose == "5":
print("開始建表")
sql = """CREATE www.michenggw.com TABLE User_py (
ID INT NOT NULL,
USER_NAME CHAR(20) NOT NULL,
USER_AGE INT)"""
cur.execute(sql)
print("建表成功")
elif choose == "6":
print("開始關閉連接")
connection.close()
print("關閉連接成功!")
elif choose == "7":
print("正在輸出九九乘法表")
i = 0
while i < 9:
i += 1
j = 0
while j < i:
j += 1
print(f‘{j}*{i}={i * j} ‘, end="")
print()
elif choose == "8":
# 用python完成計算器
print("歡迎使用計算器")
print("1.加法 2.減法 3.乘法 4.除法")
choose www.meiwanyule.cn= input("請輸入要進行的操作:")
flag = True
while flag:
if choose == ‘1‘:
add1 = int(input("請輸入加數1:"))
add2 = int(input("請輸入加數2:"))
print("結果為:", add1 + add2)
flagStr = input("是否繼續y/n")
if flagStr == ‘y‘:
flag = True
print("1.加法 2.減法 3.乘法 4.除法")
choose = input("請輸入要進行的操作:")
else:
flag = False
elif choose == ‘2‘:
j1 = int(input("請輸入減數1:"))
j2 = int(input("請輸入減數2:"))
print("結果為:", j1 - j2)
flagStr = input("是否繼續y/n")
if flagStr == ‘y‘:
flag = True
print("1.加法 2.減法 3.乘法 4.除法")
choose = input("請輸入要進行的操作:")
else:
flag = False
elif choose == ‘3‘:
c1 = int(input("請輸入因數1:"))
c2 = int(input("請輸入因數2:"))
print("結果為:", c1 * c2)
flagStr = input("是否繼續y/n")
if flagStr =www.mhylpt.com= ‘y‘:
flag = True
print("1.加法 2.減法 3.乘法 4.除法")
choose = input("請輸入要進行的操作:")
else:
flag = False
elif choose == ‘4‘:
f1 = int(input("請出入除數1:"))
f2 = int(input("請出入除數1:"))
print("結果為:", f1 / f2)
flagStr www.mhylpt.com= input("是否繼續y/n")
if flagStr == ‘y‘:
flag = True
print("1.加法 2.減法 3.乘法 4.除法")
choose = input("請輸入要進行的操作:")
else:
flag = False

# for row in results:
# id = row[0]
# userName = row[1]
# userAge = row[2]
# # 打印結果
# print(id, userName, userAge)

自學Python快速入門