零基礎開始學python

來源:互聯網
上載者:User

標籤:支援   tin   ascii碼   匯入   orm   and   注釋   年齡   特殊字元   

變數規則:在python中變數名不能有特殊字元和數字開頭以及python裡的一些關鍵字,可以使用底線開頭,在python裡,變數是支援使用中文的,但盡量不要使用中文,為什嗎?因為這樣會讓你看起來太low了,像個山炮程式員,使用拼音的也是一樣,但是這對於我來說,這是一種挑戰,在使用sql裡我大量使用了拼音。

大寫的表示是常量。

編碼:最基礎的是ASCII碼,還有中文的GB系列,目前使用的大多數採用UTF-8方案,UTF-8能非常好的解決中英文的相容性,一個英文佔用2個位元組,一個中文佔用3個位元組;

注釋:有兩種

1.當前行前面加上#就表示該行為注釋。

2.加上3個單引號或3個雙引號就表示該段落為注釋,即多行注釋。

在python中單引號和雙引號的作用是一樣的。=為賦值,==為等於。

使用者輸入:input

例:name=input(’name:‘)

列印變數的方法

1.使用%s

name=input(‘name:‘)
age=input(‘age:‘)
job=input(‘job:‘)
salary=input(‘salary:‘)
info=‘‘‘
-----------------info of %s--------------
name=%s
age=%s
job=%s
salary=%s
‘‘‘ %(name,name,age,job,salary)
print(info)

2.格式化輸出:format

例:

name=input(‘name:‘)
age=input(‘age:‘)
job=input(‘job:‘)
salary=input(‘salary:‘)
info=‘‘‘
-----------------info of {_name}--------------
name={_name}
age={_age}
job={_job}
salary={_salary}
‘‘‘ .format(_name=name,
_age=age,
_job=job,
_salary=salary)
print(info)
以上兩種都能實現對變數的輸出列印,第一種操作起來簡單,第二種看起來結構清晰。

匯入庫:import
例:import getpass
#密文庫

條件判斷
if else
_age=60
print(type(_age))
age=int(input("猜年齡:"))
if age>_age:
print(‘猜大了‘)
elif age<_age:
print(‘猜小了‘)
else:
print(‘你猜對了‘)
做這個語句的時候要注意資料類型,看上去的資料類型不一定是真實的資料類型,這是老闆課堂上講的,也是自己在實操中感受到了。

迴圈

while
while True
break
跳出整個迴圈
continue
跳出當前迴圈的剩餘語句
count()統計某個字元出現的次數
range()計數
例:range(10)是指從0至10,不包含10,預設步進1。range(1,10,3)是指從1至10,不含10,步進3。

while迴圈
_password=123
count=0
while count<3:
password = int(input(‘請輸入你的網銀密碼:‘))
if password==_password:
print(‘登陸成功‘)
break
elif password!=_password and count==0:
print(‘輸入的密碼錯誤‘)
elif password!=_password and count==1:
print(‘你己輸錯兩次,再輸錯一次將被鎖定‘)
else:
print(‘你的賬戶己被凍結‘)
count +=1
for迴圈
for i in range(3):
_password = int(input(‘password:‘))
if password==_password:
print(‘登陸成功‘)
break
else:
password!=_password
print(‘密碼輸入錯誤‘)
else:
print(‘密碼己被鎖定‘)

迴圈嵌迴圈
_password=123
count=0
while count<3:
password = int(input(‘請輸入你的網銀密碼:‘))
if password==_password:
print(‘登陸成功‘)
break
elif password!=_password and count<2:
print(‘輸入的密碼錯誤‘)
else:
print(‘你己輸錯三次‘)
count +=1
if count==3:
countne=input(‘你還要繼續輸入嗎?‘)
if countne=‘n‘:
count==0
上周主要學習了這些知識,使用操作中會出現很多文法格式之類的錯誤,需要多加練習才行。


零基礎開始學python

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.