1. 程式人生 > 實用技巧 >python 01篇

python 01篇

一、Pycharm 使用小tips

1.1pycharm建立專案時,選擇Python環境,不使用預設的虛擬環境

1.2如何在pycharm中檢視python版本

  路徑File-Settings-Project Interpreter

1.3 修改pycharm中的字型

  路徑:File-Settings-Editor--Front

1.4 配置pycharm可使用Ctrl+滑鼠滾輪切換字型大小

  路徑:File-Settings-General 勾選Change front size (Zoom) with Ctrl+Mouse Wheel

1.5 pycharm配置git

路徑:VCS-Get from Version Control ,輸入git倉庫地址

1.6 pycharm快捷鍵

 編輯類:

  Ctrl + D 複製選定的區域或行
  Ctrl + Y 刪除選定的行
  Ctrl + Alt + L 程式碼格式化
  Ctrl + Alt + O 優化匯入(去掉用不到的包匯入)
  Ctrl + 滑鼠 簡介/進入程式碼定義
  Ctrl + / 行註釋 、取消註釋

  Ctrl + 左方括號 快速跳到程式碼開頭
  Ctrl + 右方括號 快速跳到程式碼末尾
  Shift + F10 執行
Shift + F9 除錯

查詢/替換類:

Ctrl + F 當前檔案查詢

Ctrl + R 當前檔案替換
Ctrl + Shift + F 全域性查詢
Ctrl + Shift + R 全域性替換

執行類:

Shift + F10 執行
Shift + F9 除錯
Alt + Shift + F10 執行模式配置
Alt + Shift + F9 除錯模式配置

除錯類:

F8 單步除錯(一行一行走)
F7 進入內部
Shift + F8 退出
Ctrl + F8 在當前行加上斷點/斷點開關
Ctrl + Shift + F8 檢視所有斷點

導航類:

Ctrl + N 快速查詢類(也可查詢當前工程中的檔案,以檔名查詢)
Double Shift 任意位置查詢

二、Python知識點

2.1定義變數

name = "
小黑" # 字串 str age = 18 # 整數型別 int score = 97.5 # 浮點型 float words = " let'go " words2 = ' tom very "shy" ' # words3 = ''' let'go,tom very "shy" ''' print(words) print(words2) print(words3) # bool 布林型別 True 真 False 假 l = [1, 2, 3, 4] print(1 > 2) print(4 in l) print(1 not in l) print(len(l) > 3)

2.2條件判斷

'''
判斷條件為真時,執行該部分程式碼邏輯
使用 if或if else或if elif else或 巢狀if else等
判斷中用的比較運算,> < >= <= != ==
'''
score = input('請輸入你的分數:')  # input接受到的全都是str
score = int(score)
# print('score的型別',type(score)) #檢視型別函式type(val)
if score >= 90:
    print('優秀')
elif score < 90 and  score >= 75:
    print('良好')
elif score >= 60 and score < 75:
    print('及格了')
else:
    print('不及格')

2.3迴圈

迴圈包括for和while,一般迭代和遍歷會用到。

break:立即結束迴圈,不管你剩餘多少次沒有迴圈
continue:結束本次迴圈,繼續進行下一次迴圈

迴圈對應的else:
  ①迴圈正常結束迴圈後,會執行else裡面的程式碼
  ②else不是必須寫的

while迴圈必須指定結束條件,否則會死迴圈

count = 0 # 計數器
rate = 140 # 心率
while count < 10:
    if rate > 160:
        break  #break   迴圈裡遇到break迴圈立即結束
    
    print('跑圈',count)
    count = count + 1
    rate += 5  # rate = rate + 5
count = 0 # 計數器
while count < 10:
    print('迴圈', count)
    count = count + 1
else:
    print('當迴圈%d次時,執行else語句' % count)

for 迴圈:不需要計數器,也不會死迴圈

rate = 130
for count in range(8):
    print('跑圈', count)
    if rate > 160:
        print('你的心跳太快了,強制休息')
        break
    rate += 5
else:  # 只要for迴圈不是break提前結束的,都會執行else
    print('for對應的else')

迴圈練習

#寫一個小遊戲
#猜數字有戲
# 1.最多輸入7次,如果猜的不對,提示它大了還是小了,猜對了遊戲結束,如果次數用盡了還沒有猜對,提示已經達到次數

import random
number = random.randint(1, 100) #生成一個1~100內的隨機整數
for i in range(7):
    guess = int(input('輸入您猜的資料:'))
    if guess == number:
        print('恭喜你,猜對了')
        break
    elif guess > number:
        print('猜的大了')
        continue
    else:
        print('猜的小了')
        continue
else:
    print('您的次數用盡了,請下次再參與遊戲')

2.4 字串格式化

方式1:直接 + 不推薦使用

方式2:佔位符 %s %d %f

  %s  佔位str  %d  佔位int  %f  佔位float  其中%.2f  代表保留2位小數

方式3:{}佔位 適用於引數比較多的時候,可以對應

import datetime
name = 'marry'
welcome = 'marry,welcome'
today = datetime.datetime.today()
#字串格式化:
#1.簡單粗暴直接 +
print(name + ',歡迎登陸。今天的日期是'+ str(today))  #不推薦
#2.佔位符,%s  %d  %f     %s  佔位str  %d  佔位int  %f  佔位float  其中%.2f  代表保留2位小數
welcome = '%s,歡迎登陸' % name
welcome1 = '%s,歡迎登陸,今天的日期是 %s' % (name,today) #有多個佔位符時,要加()
print(welcome)
age = 18
age_str = '你的年齡是%d' % age  #這裡也可以用%f,不過會有小數
score = 85.789
info = '你的年齡是%d,你的成績是%.2f' % (age, score)
print(age_str)
print(info)

#3.大括號的方式    適用於引數比較多的時候,可以對應
welcome2 = '{name},歡迎登陸.今天的日期是{today}'.format(today=today, name=name)  #如果{}中加了名字,可以使用名字對應,不應按照順序傳參
welcome3 = '{},歡迎登陸.今天的日期是{}'.format(name, today)
welcome4 = '{},歡迎登陸.今天的日期是{}'.format(today, name)  #如果{}中沒有加名字,就是%s一樣的效果,必須按照順序傳參
print(welcome2)
print(welcome3)
print(welcome4)

2.5 列表 list

#陣列/列表  list
student_new = ['張三', '李四', 'wangwu']  #定義一個列表
stu_list = []  # 定義一個空列表

# 取值   索引/下標/角標,從list中取出來   list_name[index]
print(student_new[-1]) #取最後一個元素
print(student_new[0])   #取第一個元素
print(student_new[1])

#往list中增元素
student_new.append('張明')  #在列表的末尾新增元素
student_new.insert(0, 'yangyang')   #在列表的指定位置新增元素

#修改   找到指定元素後修改
student_new[2] = '黃麗'
#刪除
student_new.pop(3)  #傳入指定的下標
student_new.remove('yangyang')  #使用remove方法可以傳元素的值
del student_new[0]
student_new.clear()   #清空列表   此方法無返回值

#其他方法
hy_count = student_new.count('張三')   #統計這個list裡面某個元素出現的次數(有返回值),常用於判斷元素是否存在
print(hy_count)
hy_index = student_new.index('張三') #找到list中某個元素的下標,如果同一元素有多個,返回第一次出現時的索引
print('hy_index=',hy_index)
users = ['張三', '李珊']
student_new.extend(users)  #把一個列表裡面的元素加入另一個列表裡面
print(student_new)

numbers = [1, 25, 3, 95, 102, 4]
numbers.sort()    #預設升序排序
print(numbers)
numbers.sort(reverse=True)   # 降序排序
print(numbers)
#多維陣列   陣列套陣列
student_info = [
    [1, 'lhy', '北京'],
    [2, 'hzy', 'shanghai'],
    [3, 'lhh', '天津']
]
#二維陣列
student_info2 = [
    [1, 'lhy', '北京', ['bmw', 'benz', 'aodi']],
    [2, 'hzy', 'shanghai'],
    [3, 'lhh', '天津']
]
#一層層去找要操作的資料即可
student_info[0][2] = '山東'
student_info2[0][-1] .append('tesla')

print(student_info)
print(student_info2)