Python基礎-01git
print("Hello World!")#打印hello worldapp
注:命名規則:oop
1. 字母數字下劃線組成spa
2. 不能以數字開頭,不能含有特殊字符和空格排序
3. 不能以保留字命名索引
4. 不能以中文命名rem
5. 定義的變量名應該有意義get
6. 駝峯式命、 下劃線分割單詞input
7. 變量名區分大小寫string
(1)註釋
單行註釋 用#,多行註釋用三個單引號或三個雙引號 '''被註釋的內容'''
# 單行註釋
'''多行註釋'''
""" 多行註釋 """
(2)用戶輸入 input()
(3)程序縮進 tab不等於四個空格 縮進級別必須保持一致
print("Hello World!")
print("Alex")
print("JinXing")
name = "Alex Li"
x = 3
y = 4
z=x*y
print("3乘以4=",z)#3乘以4
z=5*8
print(z)
if 條件1:
自拍
elif 條件2:
蹦
else:
跳舞
例程以下:
score = int(input("score:"))
if score > 90:
print("A")
elif score > 80:
print("B")
elif score > 70:
print("C")
elif score > 60:
print("D")
else:
print("滾")
while 條件:
print("any")
print("any")
例程(求1-10的和)
num = 1
while num <=10:
num+=1
print(num)
else:
print("This is else statement")
#打印 高爲h 寬爲 w的#圖形
height = int(input("Height:"))
width = int(input("width:"))
num_height = 1
while num_height <= height:
num_width = 1
while num_width <= width:
print("#",end="")
num_width +=1
print()#空一行
num_height +=1
#打印 高爲h 寬爲 w的#圖形(改進版)
height = int(input("Height:"))#用戶輸入一個寬度
width = int(input("width:"))#用戶輸入一個高度
num2 = height#第一步賦值
while num2 > 0:#第二部:num2 == 2
num1 = width#第三部:賦值
while num1>0:#第四部:num1==2 #第七部:num1 = 1
print("#", end="")#第五步:不換行 打印一個# 第八步:不換行 打印一個#
num1 -= 1 #第六步:num1 = 1 第九部:num1 = 0
print() #第十步:num2=1
num2 -= 1
6. for循環以及continue和break
7. #_author:"ai_XZP_master"
#date: 2018/1/3
'''continue 結束本次循環,繼續下一次循環
# break 跳出整個當前的循環
'''
exit_flag = False
for i in range(10):
if i<5:
continue#結束本次循環,跳向下次循環
print(i)
for j in range(10):
print("layer2",j)
if j == 6:
exit_flag = True#設置標誌位,跟隨break 兒子要跳樓了
break
if exit_flag:#他也跳
break
8. 數據類型
9. #_author:"ai_XZP_master"
#date: 2018/1/3
# 索引(下標) ,都是從0開始
# 切片
# .count 查某個元素的出現次數
# .index 根據內容找其對應的位置
# "haidilao ge" in a
# 增長
# a.append() 追加
# a.insert(index, "內容")
# a.extend 擴展
#
# 修改
# a[index] = "新的值"
# a[start:end] = [a,b,c]
#
# 刪除
# remove("內容")
# pop(index)
# del a, del a[index]
# a.clear() 清空
#
# 排序
# sort ()
# reverse()
# 身份判斷
# >>> type(a) is list
# True
# >>>
name='wuchao'
name1='jinxin'
name2='xiaohu'
name3='sanpang'
name4='ligang'
names='wuchao jinxing xiaohu sanpang ligang'
# a=['wuchao','jinxing','xiaohu','sanpang','ligang']
#增刪改查
#增 切片
# print(a[1:])#取到最後
# print(a[1:-1])#取到倒數第二值
# print(a[1:-1:2])#從左到右一個一個去取
# print(a[1::2])#從左往右隔一個去取
# print(a[3::-2])
# b=a[3::-1]
#添加 append insert
# a.append('xuepeng')#默認插到最後一個位置
# print(a)
# a.insert(1,'xuepeng')#將數據插入到任意位置
# print(a)
#修改
# a[1]='haidilao'
# print(a)
# a[1:3]=['a','b']
# print(a)
#刪除 remove pop del
# a.remove(a[0])
# print(a)
# a.pop(1)
# print(a)
# del a
# print(a)
# #count:計算某元素出現次數
# t=['to','be','or','not','to','be'].count('to')
# print(t)
#extend
# a = [1,2,3]
# b = [4,5,6]
# a.extend(b)
# print(a)
# print(b)
#index
# a=['wuchao','jinxing','ligang','xiaohu','sanpang','ligang',['wuchao','ligang','jinxing','xiaohu','sanpang','ligang']]
# # print(a.index('jinxing'))
#
# first_lg_index = a.index("ligang")#get the first ligang
#
# little_list = a[first_lg_index+1:]#切取小列表
#
# second_lg_index = little_list.index("ligang")#去第二個李剛在小列表裏的位置
# print("second_lg_index:",second_lg_index)
#
# second_lg_index_in_big_list = first_lg_index + second_lg_index +1#經過大李剛和小李剛的位置來計算 小李剛在大李剛的位置
#
# print("second_lg_index_in_big_list:",second_lg_index_in_big_list)
# print("second lg:",a[second_lg_index_in_big_list])
#reverse
# a=['wuchao','jinxing','xiaohu','sanpang','ligang']
# a.reverse()
# print(a)
# x = [4,6,2,1,7,9]
# x.sort()
# print(x)
a=['wuchao','jinxing','xiaohu','sanpang','ligang']
a.sort()
print(a)
1、登錄界面(for循環實現)
#_author:"ai_XZP_master"
#date: 2018/1/3
'''
循環loop
有限循環
無限循環
'''
#1、有限循環
#打印1-100因此奇數
# for i in range(1,101,2):#i是循環這組數據 #2是步長
# # if i%2 == 1:
# print("loop:",i)
# for i in range(100):
# if i<50 and i>70:
# print(i)
_user = "alex"
_passwd = "abc123"
passed_authentication =False #假,不成立 #標誌位(flag)
for i in range(3):
username = input("Username:")
password = input("Password:")
if username == _user and password == _passwd :
print("Welcome %s login..." %_user)
break #跳出,中斷 #break for事後,就不會執行最後面的else語句
else:
print("Invalid username or [password!")
else: #只要上面的for循環正常執行完畢,中間沒有被打斷,就會執行else語句
print("要不要臉,臭流氓,小虎。")
# passed_authentication =False #假,不成立 #標誌位(flag)
#
# for i in range(3):
# username = input("Username:")
# password = input("Password:")
#
# if username == _user and password == _passwd :
# print("Welcome %s login..." %_user)
# passed_authentication = True
# break #跳出,中斷
# else:
# print("Invalid username or [password!")
# if not passed_authentication:
# print("要不要臉,臭流氓,小虎。"
2、登錄界面(while實現)
#_author:"ai_XZP_master"
#date: 2018/1/3
_user = "alex"
_passwd = "abc123"
counter = 0
while counter < 3:
username = input("Username:")#當while後面的條件成立(True),纔會執行它下面的代碼
password = input("Password:")
if username == _user and password == _passwd:
print("Welcome %s login..." % _user)
break # 跳出,中斷 #break for事後,就不會執行最後面的else語句
else:
print("Invalid username or [password!")
counter += 1
if counter == 3:
keep_going_choice = input("還想玩嗎?[y/n]")
if keep_going_choice == "y":
counter = 0
else: #只要上面的for循環正常執行完畢,中間沒有被打斷,就會執行else語句
print("要不要臉,臭流氓,小虎。")
3.格式化輸出
#_author:"ai_XZP_master"
#date: 2018/1/3
'''
佔位符 %s s = string
%d d = digit 整數
%f f = float 浮點數,約等於小數
'''
name = input("Name:")
age = int(input("Age:"))
job = input("Job:")
salary = input("Salary:")
if salary.isdigit():#長的像不像數字,好比200d,‘200’
salary = int (salary)
# else:
# #print("must input digit")
# exit("must input digit")#退出程序
#print(name,age,job,salary)
msg = ''' -----------info of %s------------ Name:%s Age :%s Job :%s Salary :%d You will be retired in %s vears ----------- end ------------ ''' %(name,name,age,job,salary,65-age) print(msg)