python學習筆記01

來源:互聯網
上載者:User

標籤:使用方法   變數   類型   拼接   linu   utf-8   continue   div   特性   

一.Python的注釋方法

python使用#和‘‘‘進行注釋,其中#用於一行注釋,‘‘‘用於多行注釋。需要注意的是‘‘‘內容賦值給一個變數時可以用來列印多行。

1 # Author:sun2 3 ‘‘‘Author4 sun‘‘‘
name=input("please input your name:")age=int(input("please input your age:"))sex=input("please input your sex:")info=‘‘‘name:%s \nage:%d  \nsex:%s‘‘‘%(name,age,sex)

 

二.Python的輸入輸出方法。

python使用print("text")進行輸出,使用 paramater=input("text")從鍵盤輸入。

print("Hello World")name=input("please input your name:")print("your name is ",name)

 

三.python在Linux下運行

如果想用python3.6之類非Linux內建python要事先對預設python進行配置。第一是環境變數,第二是字元格式設定。

在Linux下Python檔案可以直接以./***運行。但是需要進行一些設定,要在開頭插入如下代碼:

#! user/bin/env python# -*- coding:utf-8 -*-# Author:***

四.Python中字串的拼接

Python中字串拼接的方法有三種:

1.以+號進行拼接:可以以+號直接拼接字串

name=input("please input your name:")age=input("please input your age:")sex=input("please input your sex:")print("name:"+ name +"\nage: "+ age + "\nsex:"+ sex)

其中/n是換行逸出字元。

2.以%進行拼接

name=input("please input your name:")age=int(input("please input your age:"))sex=input("please input your sex:")info="name:%s \nage:%d  \nsex:%s"%(name,age,sex)print(info)

需要注意的是%d時需要轉換類型

3.使用.format().

name=input("please input your name:")age=int(input("please input your age:"))sex=input("please input your sex:")info=‘‘‘name:{_name} \nage:{_age}  \nsex:{_sex}‘‘‘.format(_name=name,_age=age,_sex=sex)print(info)

也可以不用指定參數。用{0}{1}{2}等。

name=input("please input your name:")age=int(input("please input your age:"))sex=input("please input your sex:")info=‘‘‘name:{0} \nage:{1}  \nsex:{2}‘‘‘.format(name,age,sex)print(info)

要注意這三種之中+號的效率最為低下。

五.Python中的迴圈語句

Python中有while迴圈與for迴圈,其運行原理與C++略有不同。python中while後可以直接跟判斷語句,不像C++需要加括弧。while還可以與else進行組合。形式為

while 判斷條件:    執行語句……
else:
count = 3while count != 0:    print("hello",count)    count=count-1else:    print("exit")

而for語句則類似C++新特性範圍for的使用。for迴圈的文法格式如下:

for iterating_var in sequence:   statements(s)
for letter in ‘Python‘:        print ‘當前字母 :‘, letter

或者

for i in range(10):    if i <10        print("hello",i)

python中的range()函數使用方法

>>> range(1,5) #代表從1到5(不包含5)[1, 2, 3, 4]>>> range(1,5,2) #代表從1到5,間隔2(不包含5)[1, 3]>>> range(5) #代表從0到5(不包含5)[0, 1, 2, 3, 4]

 

六.continue與break

continue與break與C++中的一樣使用。

 

python學習筆記01

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.