python學習筆記基礎一


1,數據類型簡單認識 ,遇到不認識的數據類型,就用type去測試一下,查看一下。

如:

print(type(1234))
<class 'int'>
[Finished in 0.1s]

  1,整數型(int)

  數字,1,2,3,4,5。如:

print(100)

  可加減乘除取余數(+-*/%)

print(100+2)
print(100-2)
print(100*2)
print(100/2)
print(100%2)
102
98
200
50.0
0
[Finished in 0.3s]

  2,字符串:str 凡是用引號引起來的,都是字符串。但是要注意,如果外邊是用雙引號包括的,里邊就不能有雙引號,如果需要輸出雙引號,得加上轉移字符\

  如:

print("'可以正常輸出'")
print('"可以正常輸出"')
print("\"可以正常輸出\"")
print('\'可以正常輸出\'')
'可以正常輸出'
"可以正常輸出"
"可以正常輸出"
'可以正常輸出'
[Finished in 0.2s]

  可相加

#-*- encoding:utf-8 -*-
a = "Dmail"
b = "加油"
print(a+b)
print("Dmail"+"加油")

  可與數字相乘

print("加油"*8)

  字符串轉換成字符串:int(str) 條件:str必須是數字

a = '123'
b = int(a)
print(a,type(b))
123 <class 'int'>

  數字轉換成字符串:str(int),任何的數字都可以轉換成字符串。

  3,bool:布爾值

print(True,type(True))
True <class 'bool'>
[Finished in 0.2s]

 2,用戶交互,input

  1,等待輸入

  2,將你輸入的內容復制給了前面的變量。

  3,input出來的數據類型全部是str

簡單例子:

#-*- encoding:utf-8 -*-
name = input("請輸入您的名字:")
age = input("請輸入您的年齡:")
print("我的名字是"+name,"我的年齡"+age+"歲")
print(type(age))
請輸入您的名字:Dmail
請輸入您的年齡:24
我的名字是Dmail 我的年齡24歲
<class 'str'>

 3,流程控制語句

  1,if。

  >>if 條件 :

  >>  結果

if True:
    print(666)
666

  2,if else

  >>if 條件 :

  >>  結果1

  >>else :

  >>  結果2

if False:
    print(666)
else:
    print(999)
999

  3,if elif elif ... else 

  >>if 條件:

  >>  結果1

  >>elif 條件:

  >>  結果2

  >>elif 條件:

  >>  結果3

  >>else:

  >>  結果4

 

#-*- encoding:utf-8 -*-
num = input("請輸入數字:")
if num == "1":
    print("一起唱歌")
elif num == "2":
    print("一起跳舞")
elif num == "3":
    print("一起吃飯")
else:
    print("你猜錯了,去選下一家吧")
PS C:\Users\Dmail> & C:/Users/Dmail/AppData/Local/Programs/Python/Python37/python.exe c:/Users/Dmail/Desktop/test.py
請輸入數字:1
一起唱歌
PS C:\Users\Dmail> & C:/Users/Dmail/AppData/Local/Programs/Python/Python37/python.exe c:/Users/Dmail/Desktop/test.py
請輸入數字:2
一起跳舞
PS C:\Users\Dmail> & C:/Users/Dmail/AppData/Local/Programs/Python/Python37/python.exe c:/Users/Dmail/Desktop/test.py
請輸入數字:3
一起吃飯
PS C:\Users\Dmail> & C:/Users/Dmail/AppData/Local/Programs/Python/Python37/python.exe c:/Users/Dmail/Desktop/test.py
請輸入數字:4
你猜錯了,去選下一家吧
PS C:\Users\Dmail> & C:/Users/Dmail/AppData/Local/Programs/Python/Python37/python.exe c:/Users/Dmail/Desktop/test.py
請輸入數字:5
你猜錯了,去選下一家吧
PS C:\Users\Dmail>

  例題:匹配成績的小程序,成績有ABCDE5個等級,與分數對應關系如下:

  A  90-100

  B  80-89

  C  60-79

  D  40-59

  E  0-39

score = int(input("請輸入成績:"))
if score >100:
    print('我擦,總分才100')
elif score >=90:
    print('A')
elif score >= 80:
    print('B')
elif score >= 60:
    print('C')
elif score >= 40:
    print('D')
elif score >=0:
    print('E')
else:
    print('成績有誤!')

  這里如果輸入成績92,也只會輸出A,下面的條件雖然滿足,但是也不會執行的。

  4,if嵌套

  >>if 條件:

  >>  if 條件:

  >>    結果1

  >>  else:

  >>    結果2

#-*- encoding:utf-8 -*-
name = input('請輸入姓名:')
age = input('請輸入年齡:')
if name == 'Dmail':
    if age == '18':
        print('666')
    else:
        print('233')
else:
    print('哈哈哈')
請輸入姓名:Dmail
請輸入年齡:24
233

  5,while 循環

      循環體

      無限循環

      continue:遇到continue,終止此次循環,進入下一次循環。

      終止循環:1,改變條件,使其不成立

              2,遇到break

  >>while 條件:

  >>  執行語句1

  >>  執行語句2

例1:無限循環

#while
print('111')
while True:
    print('水手')
    print('綠色')
    print('我們不一樣')
print('222')

  注意:這里由於while的條件一直是True,所以這個循環是無限循環的,永遠都不會輸出'222'

例2:終止循環:1,改變條件,使其不成立

count = 1
flag = True
while flag:
    print(count)
    count = count + 1
    if count > 100:
        flag = False

count = 1

while count <=100:
    print(count)
    count = count + 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

例3:終止循環:1,遇到break

while True:
    print('222')
    print('3333')
    break
    print('4444')
print('exit')
222
3333
exit

例4:conntinue,輸出出[1-10]中哪些數是偶數,即可以理解為除以2的商是整數而沒有余數。

count = 0
while count <= 10:
    count = count + 1
    if count % 2 !=0:
        continue
    print(count)
2
4
6
8
10

  while補充

  >>while 條件

  >>  循環體

  >>else:

  >>  循環正常結束:沒有遇到break強行終止循環時候,會執行該地方的代碼

  例子,2019年9月20日進行補充的例子。

  

 1 #while 補充
 2 count = 0
 3 while count <= 3:
 4     count += 1
 5     if count == 2 :
 6         break
 7     else:
 8         print('loop%d' % count)
 9 else:
10     print('測試1')
11 
12 count = 0
13 while count <= 3:
14     count += 1
15     if count == 2 :
16         pass
17     else:
18         print('loop%d' % count)
19 else:
20     print('測試2')
loop1
loop1
loop3
loop4
測試2

Process finished with exit code 0
查看運行結果

  6,for循環,有限循環

  >>for 元素變量 in 元素:

  >>  循環體 

例子:

#for循環例子
l =[2,1,4,5]
for index in l:
    print(index)

運行結果:

2
1
4
5

   其他補充,in的場景應用:用於檢查輸入的字符串是否含有非法字符串。

#-*- encoding:utf-8 -*-
s = 'asdasdas豬頭dsdsds'
if '豬頭' in s:
    print('您的評論有敏感詞')

運行結果:

您的評論有敏感詞

 

 

相關習題

1、使用while循環輸入1 2 3 4 5 6    8 9 10
2、求1-100的所有數的和
3、輸出1-100內的所有奇數
4、輸出1-100內的所有偶數
5、求1-2+3-4+5 ... 99的所有數的和
6、用戶登陸(三次機會重試)

 

其他筆記:tab鍵和4個空格不要混用,會報錯。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM