Python学习笔记

https://www.bilibili.com/video/av75855831?from=search&seid=14589661103677033438

Mosh Hamedani的教程


input:
输入,以及类型转换
birth_year = int(input(‘Birth year: ‘))
input("").lower

strings:
字符串的定位:
course = ‘Python for Beginners’

course[0] # returns the first character

course[1] # returns the second character

course[-1] # returns the first character from the end 
 course[-2] # returns the second character from the end
course[1:5] 连续

字符串的格式化输出
name = ‘Mosh’
message = f’Hi, my name is {name}’ 格式化输出

字符串乘法:
就是复制n份,print("abc"*2), 即输出abcabc

字符串的函数,大小写,查找等
message.upper()
message.lower()
message.title()# to capitalize the first letter of every word
message.find(‘p’) # returns the index of the first occurrence of p 
(or -1 if not found)
message.replace(‘p’, ‘q’)

in,用于检查是否包含于该字符串中:
test="xxabcaa"
print("abc" in test)
>>True

打印
打印多行字符串:
print("""
ssxx
xxxx
xxxx """)

算法
+,-,*,/, **平方
x+=10等效于x=x+10

使用数学函数:
import math
x = math.ceil(2.9), floor,abs,,,,

if语句
if, elif, else, and, or
if is_hot:

print(“hot day”)

elif is_cold:

print(“cold day”)

else: 

print(“beautiful day”)

while语句
while, break, else
i = 1
while i <= 3:
i = i + 1
if xxx:
print ("greate")
break
else:



for语句
可嵌套,
for item in 'Python':
print(item)

for item in range(10): 0~10,
range(5,10,2) 5~10,step=2

不换行打印-- print("",end="")

list
numbers = [1, 2, 3, 4, 5]

numbers[0]
numbers[1]

numbers.append(6) 直接加个
numbers.insert(0, 6) 位置,插入值
numbers.remove(6)
numbers.pop()
numbers.clear() 不需要值
numbers.index(8)
numbers.sort()
numbers.reverse()
numbers.copy()

numbers = [ 2,3,4,5,6 ]
numbers.append(66)
print(numbers) #[2, 3, 4, 5, 6, 66]
numbers2 = numbers.copy()
print(numbers2) #[2, 3, 4, 5, 6, 66]

names = ['Jone','Bob','Mosh','Sara','Mark']
names[0]
names[2:]

numbers = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ],
[ 4, 7, 9 ],
]
print (numburs[1][1]) >>5
for row in numbers:
for item in row:
print(item)

list的unpackting,解压
coordinates = (1,2,3)
x = coordinates[0]
y = coordinates[1]
z = coordinates[2]
a,b,c = coordinates
print(f"{x},{y},{z}")
print(f"{a},{b},{c}")


dictionary
举例:
customers = {
"name" : "Jack",
"age" : 30,
"is_verified" : True
}
print(customers)
print(customers["name"])
print(customers["name"][0])
print(customers["age"])
print(customers.get("bith","default value is here"))
customers["bith"] = "1989"
print(customers.get("bith","default value is here"))
>> {'name': 'Jack', 'age': 30, 'is_verified': True}
>> Jack
>> J
>> 30
>> default value is here
>> 1989

字典的查询使用:
digits = {
"1" : "one",
"2" : "two",
"3" : "three"
}
phone_num = input("what's your phone num? ")
for char in phone_num:
output = digits.get(char,"*")
print(output,end=" ")
>> what's your phone num? 1235782
>> one two three * * * two

messages = input(">")
words=messages.split(' ')
emojis = {
":)" : "😀",
":(" : "😭"
}
for item in words:
print(emojis.get(item,item),end=" ")
>> >good morning :(
>> good morning 😭

异常处理
try:
age = int(input("age: "))
print(f"age: {age}")
except ValueError:
print("Please input valid number")

class
class Point:
def __init__(self, x_input, y_input):
self.x = x_input
self.y = y_input

def move(self):
print("xxxx:")

def draw(self):
print("draw")

point = Point(100, 20)
print(point.x)
point.x = 111
print(point.x)

class Person:
def __init__(self, name_input):
self.name = name_input

def print_name(self):
words = self.name.split(" ")
output = ""
for item in words:
output += item + ","
print(f"Hello, {output}")

person = Person("shi wen ming")
person.print_name()
>>>Hello, shi,wen,ming,




posted @ 2020-02-07 18:12  gougouding  阅读(246)  评论(0)    收藏  举报