精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
python3.5|小白入門使用頻率較高及較實用基礎語法與簡單實例
小智雅匯 2018-05-01 10:41:44

先了解下python解釋器的工作方式:

1 默認方式是按行處理,遇到換行時就認為語句(或表達式)結束,就會去執行這個語句(或求值表達式);

2 如果在讀入一行的最后遇到反斜杠,解釋器就丟掉反斜杠及隨后的換行符,把下一行的內容看作是本行內容的繼續(續行);

3 如果被讀入行中存在尚未配對的括號,同樣認為下一行是本行的繼續;

4 以換行結尾的一系列字符構成一個物理行,解釋器根據上面規則把連續的幾個物理行拼接成一個邏輯的程序行(邏輯行),一次處理一個邏輯行。

5 如果讀到的行是一個復雜結構的頭部,解釋器就會繼續讀完這個結構(根據代碼的退格縮進形式),而后一次完成這個結構的處理。如def、if、for、while等。

附原碼:

# 列表list和元組tuple中可以包含python中的任何對象

import math

all_in_list = [

1, # 整數integer

1.0, # 浮點數float

'a word', # 字符串string

print(1), # 函數function

True, # 布爾值bool

math.sin(1), # 數學函數

math.e, # 數學常數

[0,1], # 列表list

(1,2), # 元組tuple

{'key':'value'},# 字典dict

{3,4} # 集合set

]

print(all_in_list)

# (下面的print輸出全部用'''注釋表示)

'''[1, 1.0, 'a word', None, True, 0.8414709848078965,

2.718281828459045, [0, 1], (1, 2), {'key': 'value'}, {3, 4}]'''

# 升序合并兩個list并去除重復元素

list1 = [2,3,8,4,9,5,6]

list2 = [5,6,7,17,11,2]

list3 = list(set(list1 + list2))

print(list3)

'''[2, 3, 4, 5, 6, 7, 8, 9, 11, 17]'''

# list倒置

list6 = []

for iter in reversed(list3): # 返回的是迭代器,而不是序列

list6.append(iter)

list7 = sorted(list3,reverse=True)

list8 = list3.reverse

print(list6,'',list7,'',list8)

'''

[2, 3, 4, 5, 6, 7, 8, 9, 11, 17]

[17, 11, 9, 8, 7, 6, 5, 4, 3, 2]

[17, 11, 9, 8, 7, 6, 5, 4, 3, 2]

'''

#兩個重要的數學常數

import math

print(math.e, math.pi)

'''2.718281828459045 3.141592653589793'''

#三角函數使用的是弧度值

print(math.sin(1)) # 求正弦

print(math.degrees(math.pi/2)) # 弧度值→角度值

print(math.degrees(1))

print(math.radians(90)) # 角度值→弧度值

'''

0.8414709848078965

90.0

57.29577951308232

1.5707963267948966

'''

# 常見數學計算與函數

print(2**10) # 求2^10

print(math.pow(2,10)) # 求2^10

print(math.sqrt(2)) # 求2的平方根

print(math.exp(10)) # 求e^10

print(math.log(1024,2)) # 求以2為底,1024的對數

'''

1024

1024.0

1.4142135623730951

22026.465794806718

10.0

'''

#操作符in、not in判斷字符串是否包含指定字符串

str1 = 'href'

str2 = '<a

if(str1 in str2):

print(str2,'包含有',str1)

str1 = 'https'

if(str1 not in str2):

print(str2,'不包含有',str1)

'''

<a > 包含有 href

<a > 不包含有 https

'''

# 字符串str.format()函數,類似于C語言的printf()函數

str3 = '{} {}'.format('hello','python')

str4 = '{0},{2}'.format('hi','first','python')

str5 = '{1} {0} {1}'.format('wwu','hi')

str6 = '姓名:{name},年齡:{age}'.format(name = 'zwu',age = 10)

dic = {'name':'jwu','age':2}

str7 = '姓名:{name},年齡:{age}'.format(**dic)

list9 = ['fwu',35]

str8 = '姓名:{0[0]},年齡:{0[1]}'.format(list9)

print(str3,'',str4,'',str5,'',str6,'',str7,'',str8,'')

'''

hello python

hi,python

hi wwu hi

姓名:zwu,年齡:10

姓名:jwu,年齡:2

姓名:fwu,年齡:35

'''

# 進制轉換

print('{:b}'.format(16)) # 二進制

print('{:d}'.format(16)) # 十進制

print('{:o}'.format(16)) # 八進制

print('{:x}'.format(16)) # 十六進制

print('{:#x}'.format(16)) # 小寫十六進制

print('{:#X}'.format(16)) # 大寫十六進制

'''

10000

16

20

10

0x10

0X10

'''

print(0b10000,16,0o20,0x10)

'''16 16 16 16'''

# 字符串分割1(字符串與元組)

s1 = 'http://www.baidu.com'.partition('://')

print(s1)

'''('http', '://', 'www.baidu.com')'''

# 以上返回一個3元的元組,分別是分隔字符串本身,及其前、后內容

# 字符串分割2,使用split()方法(字符串與列表)

stri4 = 'hello,hell,hello,world'

list4 = stri4.split('ll',2)

print(list4)

'''['he', 'o,he', ',hello,world']'''

'''

# 字符串切片3,使用splitlines()方法

#

str.splitlines([keepends])

# 按照行('','','')分隔,返回一個包含各行作為元素的列表

# 如果參數keepends為True,則保留換行符

'''

# 表list元素連接成字符串

listn1 = ['hi','wwu','how are you!']

strn1 = ','.join(listn1)

print(strn1)

'''hi,wwu,how are you!'''

# 字符計數

s2 = 'hello,hell,hello,world'

s3 = 'ell'

print(s2.count(s3,0,len(s2)))

print(s2.count(s3))

'''

3

3'''

# 字符串center()方法

# 返回一個原字符串居中,并使用空格或指定字符填充至指定長度的新字符串

s4 = 'wwu'

s5 = s4.center(10)

s6 = s4.center(10,'-')

print(s5,s6)

''' wwu ---wwu----'''

# 左(右)對齊并填充ljust()、rjust()

s6 = s4.rjust(10,'.')

print(s6)

'''.......wwu'''

#字符串替換replace()方法

# str.replace(old,new[,n])

# 字符串str中的字符串old,用字符串new替換,替換n次

stri1 = 'hello,hell,hello,world'

stri2 = stri1.replace('hell','good',2)

print(stri2)

'''goodo,good,hello,world'''

# lambda表達式,用于描述小的匿名函數

num = (lambda a,b,c: b**2 -4*a*c)(3,4,5)

print(num)

'''-44'''

# a=1, b=2,不用中間變量交換a和b的值

# I 直接交換

a = 3

b = 5

a,b = b, a

print(a,b)

'''5 3'''

# II 加法實現交換

a = a + b

b = a - b

a = a - b

print(a,b)

'''3 5'''

# III 異或實現交換

# 如果a、b兩個值不相同,則異或結果為1。如果a、b兩個值相同,異或結果為0。

a = a ^ b

b = a ^ b

a = a ^ b

print(a,b)

'''5 3'''

'''

a = 3 | 0 0 1 1

b = 5 | 1 0 0 1

a ^ 1 0 1 0 = 6

b ^ 0 0 1 1 = 3

a ^ 1 0 0 1 = 5

'''

#九九乘法表

for i in range(1,10):

for j in range(i,10):

print('{}*{}={}'.format(i,j,i*j),end = ' ')

print('')

# 四則運算

oplist = ('+','-','*','/')

while True:

line = input("input the expression to calculate,i.e. 3 * 4:")

if line == "quit":

break

exp = line.split()

if len(exp) !=3:

print("Exp should be 'x op y"

"Try again, or 'quit' to exit")

contine

op = exp[1]

if op not in oplist:

print("Operator is not corect")

continue

x = float(exp[0])

y = float(exp[2])

if op == '+':

z = x + y

elif op == '-':

z = x - y

elif op == '*':

z = x * y

elif op == '/':

if y==0:

print("Error:divider cann't be 0")

continue

z = x / y

print("Result:",z)

'''

input the expression to calculate,i.e. 3 * 4:

4 * 9

Result: 36.0

'''

-End-

本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
【Python】入門了解
python3 列表轉化為字符串
跟老齊學Python之print詳解
python教程 --.Python 列表Lis
Python字符串中replace,split,join方法
python列表轉換為字符串
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 黄山市| 延川县| 临泽县| 台中县| 高淳县| 精河县| 开江县| 玉山县| 屏边| 封开县| 抚州市| 太湖县| 中卫市| 廉江市| 洪泽县| 玉门市| 象山县| 乌鲁木齐县| 洛川县| 乐亭县| 连州市| 吉木乃县| 延寿县| 大悟县| 太谷县| 泌阳县| 镇坪县| 文安县| 沭阳县| 醴陵市| 宁陕县| 凤冈县| 哈密市| 马龙县| 沙田区| 盐亭县| 禄丰县| 平罗县| 门头沟区| 张家口市| 教育|