天天看點

pycharm,python 常見問題

 1. pycharm裡面快速将代碼格式規整的快捷鍵

    1) 快捷鍵: Ctrl + Alt + L

    2) 頁眉 :Code ->  Reformat Code

2. pycharm 設定自動換行顯示

    1) 隻對目前檔案有效的操作

          菜單欄 -> View -> Active Editor -> Use Soft Wraps;

    2)如果想對所有檔案都生效,需要在setting 裡設定

          File -> Setting -> Editor -> General -> Use soft wraps in editor。

3. python3 中判斷變量是否為空

判斷變量是否為空:

        方式一:
                 if not x:


        方式二:
                 if x is None:
         


同理: 判斷變量有值:
        
        方式一:
                if x:

        方式二:
                if not x is None:    

推薦使用:方式二

多個變量時:
# a = 1
# b = 2
# c = None
# if a and b:
#     print('有值')
#
# if a and c:
#     print('a and c 有值')
# else:
#     print('ac 不全有值')
           

4.  Python中日期、時間的運用

      轉載: https://www.cnblogs.com/mainstream/p/11143341.html

5.  Python format 格式化函數

     1)Python2.6 開始,新增了一種格式化字元串的函數 str.format(),它增強了字元串格式化的功能。

           基本文法是通過 {} 和 : 來代替以前的 % 。

           format 函數可以接受不限個參數,位置可以不按順序。

>>>"{} {}".format("hello", "world")    # 不設定指定位置,按預設順序
'hello world'
 
>>> "{0} {1}".format("hello", "world")  # 設定指定位置
'hello world'
 
>>> "{1} {0} {1}".format("hello", "world")  # 設定指定位置
'world hello world'

----------------------------------------------------------------------------------

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
print("網站名:{name}, 位址 {url}".format(name="菜鳥教程", url="www.runoob.com"))
 
# 通過字典設定參數
site = {"name": "菜鳥教程", "url": "www.runoob.com"}
print("網站名:{name}, 位址 {url}".format(**site))
 
# 通過清單索引設定參數
my_list = ['菜鳥教程', 'www.runoob.com']
print("網站名:{0[0]}, 位址 {0[1]}".format(my_list))  # "0" 是必須的
           

      2) 數字格式化

            >>> print("{:.2f}".format(3.1415926));

            3.14

         ## 更多執行個體: https://www.runoob.com/python/att-string-format.html

6.  Python3 中的清單推導式

      所謂的清單推導式,就是指的輕量級循環建立清單

python range() 函數可建立一個整數清單,一般用在 for 循環中。
  文法: range(start, stop[, step])  
               
參數說明:
    start: 計數從 start 開始。預設是從 0 開始。例如range(5)等價于range(0, 5);
    stop: 計數到 stop 結束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5
    step:步長,預設為1。例如:range(0, 5) 等價于 range(0, 5, 1)

# a = range(5)
# print(a)         # range(0, 5)
# print(type(a))   # <class 'range'>  輸出的是一個類.需要通過周遊取值


# 清單推導式  所謂的清單推導式,就是指的輕量級循環建立清單

a = [i for i in range(5)]
print(a)

b = [x for x in range(3, 10) if x % 2 == 0]
print(b)

# ex:請寫出一段 Python 代碼實作分組一個 list 裡面的元素,比如 [1,2,3,...100]變成 [[1,2,3],[4,5,6]....]
c = [[x for x in range(y - 2, y + 1)] for y in range(1, 101) if y % 3 == 0]
print(c)

# 方式二:
a = [x for x in range(1, 101)]
b = [a[x:x + 3] for x in range(0, len(a), 3)]
print(a[0:3])   # 通過切片去取值
print(b)
           

7.  Python 中字典的幾種定義

##  使用最多的為前兩種方式
# 方式一
d = dict(name='Bob', age=20, score=88)
print(d, type(d))  # {'name': 'Bob', 'age': 20, 'score': 88} <class 'dict'>

# 方式二
d = {"name": 'Bob', "age": 20, "score": 88}
print(d, type(d))  # {'name': 'Bob', 'age': 20, 'score': 88} <class 'dict'>

# 方式三
d = dict([["name", "zh"], ["age", "18"]])
print(d, type(d))  # {'name': 'zh', 'age': '18'} <class 'dict'>

# 方式四
d = dict([("name", "zh"), ("age", "18")])
print(d, type(d))  # {'name': 'zh', 'age': '18'} <class 'dict'>
           

8.  json.dumps()和json.loads() 

      json.dumps()用于将字典形式的資料轉化為字元串,json.loads()用于将字元串形式的資料轉化為字典

     https://www.cnblogs.com/ConnorShip/p/9744223.html

9. 實時重新整理日志指令:   tail -f request.log  

10. pycharm 頁面背景顔色調整

     位址:https://blog.csdn.net/weixin_42336579/article/details/81005575

11.pycharm 設定:快速輸入自定義"代碼片段"

    位址:https://blog.csdn.net/Franticquanshi/article/details/82761897