1. 程式人生 > >菜鳥的Python之路--基礎知識

菜鳥的Python之路--基礎知識

python

剛剛開始自學Python,整理一下自己的學習感悟

剛剛開始學習Python,代碼之路才剛剛開始第一個差距就感受到了。Python的標點符號與其他語言的差別,它每句後面都沒有“;”。

變量的命名規則
1. 要具有描述性
2. 變量名只能_,數字,字母組成,不可以是空格或特殊字符(#?<.,¥$*!~)
3. 不能以中文為變量名
4. 不能以數字開頭
5. 保留字符是不能被使用

常量 :不變的量 pie = 3.141592653....
在py裏面所有的變量都是可變的 ,所以用全部大寫的變量名來代表次變量為常量

註釋
單行註釋 用#
多行註釋用三個單引號或三個雙引號 ‘‘‘被註釋的內容‘‘‘

表達式if ...else語句
縮進 IndentationError: expected an indented block ####此錯誤為沒有縮進 ^
IndentationError: unindent does not match any outer indentation level
tab != 4個空格,縮進級別必須保持一致 ###假如使用了tab鍵進行縮進,那麽在本次的縮進必須全部使用tab鍵進行縮進,官方要求使用4個空格。

and 且,並且

只有兩個條件全部為True(正確)的時候, 結果才會為True(正確)

ex:條件1 and 條件2

5>3 and 6<2 True

對於and 如果前面的第一個條件為假,那麽這個and前後兩個條件組成的表達式 的計算結果就一定為假,第二個條件就不會被計算

or 或,或者
只要有一個條件為True,則結果為Ture,
ex:5>3 or 6<2 True

對於or ,如果前面的第一個條件為真,那麽這個or前後兩個條件組成的表達式 的計算結果就一定為真,第二個條件就不會被計算

not 不 取反

循環loop
有限循環 ,次數限制

無限循環=死循環
continue 結束本次循環,繼續下一次循環
break 跳出整個當前的循環

while循環:當條件為真時執行循環裏的語句
while 條件:
print("any")
print("any")

猜年齡的小程序

age = 50
#user_input_age = int(input("Age is :"))
#flag = True
# break
while True:
user_input_age = int(input("Age is :"))
if user_input_age == age:
print("Yes")
break
elif user_input_age > age:
print("Is bigger")
else:
print("Is smaller")
print("End")

for循環小程序

#_author: 五星12138
#date: 2017/8/23
_user = "bcb"
_password = "bai199537"
passed_authtication = False
for i in range(3):
username = input("username:")
password = input("password:")
if username == _user and password == _password:
print("welcome %s login"% _user)
passed_authtication = True
break
else:
print("invalid username or password")
else:
print("登錄次數過多")#只要for循環正常結束完畢就會執行else,即沒有被break
# if passed_authtication == False:
# print("登錄次數過多")
數據運算
數據類型出初識
數字
整數 int(integer)
整型
長整型
in py3 已經不區分整型與長整型,統一都叫整型
in C int age 22 , long age
布爾 只有2種狀態,分別是
真 True
假 False
字符串
salary.isdigit()
計算機中, 一切皆為對象
世界萬物,皆為對象,一切對象皆可分類

字符格式化輸出:通過占位符
占位符 %s s = string
%d d = digit 整數
%f f = float 浮點數,約等於小數

格式化輸出小程序:

#_author: 五星12138
#date: 2017/8/23
name = input("name:")
age = input("age:")
sex = input("sex:")
salary = input("salary:")
if salary.isdigit() :
salary = int(salary)
# else :
# exit("must be digt")
msg = """------info of %s
name:%s
age:%s
sex:%s
salary:%d
"""%(name,name,age,sex,salary)
print(msg)

本文出自 “11316806” 博客,請務必保留此出處http://11326806.blog.51cto.com/11316806/1959114

菜鳥的Python之路--基礎知識