python基礎教程,python語言基礎筆記_Python語言 基礎知識筆記

 2023-10-15 阅读 44 评论 0

摘要:背景知識 Python2 的默認編碼是 ascii,Python3 的默認編碼是 utf-8 輸入輸出 python基礎教程?Python2 提供了 input,raw_input,print 等用于輸入輸出,但在 Python3 中發生了一些改變,raw_input 已經沒有了,input 的用法發生了變化

背景知識

Python2 的默認編碼是 ascii,Python3 的默認編碼是 utf-8

輸入輸出

python基礎教程?Python2 提供了 input,raw_input,print 等用于輸入輸出,但在 Python3 中發生了一些改變,raw_input

已經沒有了,input 的用法發生了變化,print 也從原來的語句變成了一個函數。

name = input('please input your name: ')

使用 input

python語言程序設計?的時候,如果輸入的是字符串,必須使用引號把它們括起來;如果輸入的是數值類型,則返回的也是數值類型;如果輸入的是表達式,會對表達式進行運算。

print('%10.3f' %pi) # 字段寬度 10,精度 3print('%010.3f' % pi) # 用 0

填充空白print('%+f' % pi) # 顯示正負號print('%10.3f' %pi) , # 不換行輸出,逗號,中間填充空格

Python3 中使用 print 必須加括號。

python怎么用,print(i, end='') # 加上一個 end 參數 不換行輸出

數據類型

定義

# 列表[] 值,可以對它進行隨意修改。 numbers = [1, 2, 3, 4, 5, 5, 7, 8]

python3?words = ['hello', 'world', 'you', 'me', 'he']

numbers[1]

words[2]# 元組() 元組是一種不可變序列,不能賦值a = (1, 2, 3)

a[2]

python入門筆記。# 字符串'' 和元組一樣,不可變不能賦值:s = 'hello, 's[3]# 字典{} 鍵值對 d1 = {'name': 'ethan',

'age': 20}

d1['age']# 集合{} 鍵,無值s1 = {'a', 'b', 'c', 'a', 'd', 'b'}

列表

python自學筆記?列表常用方法:

index

count

append

python基礎代碼。extend

insert

pop

remove

qpython,reverse

sort

字符串常用方法:

find

python總結筆記、split

join

strip

replace

translate

lower/upper

字典常用方法

clear

copy

get

setdefault

update

pop

popitem

keys/iterkeys

values/itervalues

items/iteritems

fromkeys

函數

參數組合在使用的時候是有順序的,依次是必選參數、默認參數、可變參數和關鍵字參數。

*args 和 **kwargs 是 Python 的慣用寫法。

def func(x, y, z=0, *args, **kwargs):

print 'x =', x print 'y =', y print 'z =', z print 'args =', args print

'kwargs =', kwargs

語句

if else

num = 5 if num == 3: # 判斷num的值

print 'boss' elif num < 0: # 值小于零時輸出

print 'error'else: print 'roadman' # 條件均不成立時輸出

while

count = 0while (count < 9): print 'The count is:', count

count = count + 1

print "Good bye!"

for

for iterating_var in sequence:

statements(s)for (index,iterating_var) in sequence:

statements(s)# 序列索引fruits = ['banana', 'apple', 'mango']for index in

range(len(fruits)): print '當前水果 :', fruits[index]

print "Good bye!"# 輸出 2 到 100 的質數prime = []for num in range(2,100): # 迭代 2

到 100 之間的數字

for i in range(2,num): # 根據因子迭代

if num%i == 0: # 確定第一個因子

break # 跳出當前循環

else: # 循環的 else 部分

prime.append(num)print prime

try...catch...

try:

Normal execution blockexcept A:

Exception A handleexcept B:

Exception B handleexcept:

Other exception handleelse: if no exception,get herefinally:

print("finally")

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/137956.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息