a python program into six chapters (Key Code) from entry to practice


jupyter:
jupytext:
formats: ipynb,md
text_representation:
extension: .md
format_name: markdown
format_version: ‘1.2’
jupytext_version: 1.3.2
kernelspec:
display_name: Python 3
language: python
name: python3

The first chapter started

print("hello world")

Click Restart kernel kernel restart

Chapter II variables and simple data types

# python 将始终记得变量的最新值
message = "Hello world"
print(message)

message = 'abc'
print(message)
# 关于大小写的操作
name = "ada  lovelace"
print(name.title())

name = "Ada"
print(name.upper())
print(name.lower())
first_name = 'ada'
last_name = 'lovelace'
full_name = first_name+' '+last_name

message = 'Hello, '+full_name.title()+'!'
print(message)
#制表符
print("\tPython")
# 换行符
print("language:\n\tpython\n\tC\n\tjava")
# 删除尾部的空白
language = "   \tpython \t"
print(language)

print(language.rstrip())
language=language.rstrip()
print(language)
# 删除开头的空白
language = "\tpython\t"
print(language)

print(language.lstrip())
# 删除两端的空白
language = "\tpython\t"
print(language)

print(language.strip())
# 删除两端的空白
language = "\tpy  thon\t"
print(language)

print(language.strip())

# 注意小数位
0.2+0.1
age = 34
message = "happy "+str(age)+'th birthday'
print(message)
print(3+5)
print(10-2)
print(2*4)
print(int(16/2))
import this

The third chapter list Introduction

bicycle = ['trek','cannondale','redline','specialized']
print(bicycle)

print(bicycle[0].title())
# 访问列表的倒数几个元素
print(bicycle[-1])
bicycle[0] = 'ducati'
print(bicycle)
bicycle.append('honda')
print(bicycle)
bicycle.insert(0,'abc')
print(bicycle)
# 从列表中删除元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

del motorcycles[0]
print(motorcycles)
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

del motorcycles[1]
print(motorcycles)
# 删除后的尾部元素还能继续使用 pop 方法
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

poppedmo = motorcycles.pop()
print(motorcycles)
print(poppedmo)
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

poppedmo = motorcycles.pop(0)
print(motorcycles)
print(poppedmo)
# 根据值删除元素 remove 方法

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

motorcycles.remove('honda')

print(motorcycles)
# 对列表进行永久排序 sort 方法
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
# 对列表进行永久排序 sort 方法
cars = ['bmw','audi','toyota','subaru']
cars.sort(reverse=True)
print(cars)
# 临时排序 sorted 方法
# 对列表进行永久排序 sort 方法
cars = ['bmw','audi','toyota','subaru']
print(sorted(cars))
print(cars)
# 倒着打印列表
cars = ['bmw','audi','toyota','subaru']
cars.reverse()
print(cars)
# 确定列表长度
cars = ['bmw','audi','toyota','subaru']
len(cars)

Chapter 4 Operation list

magicians = ['Alice','david','carolina']
for magician in magicians:
    print(magician)
# range 函数
for value in range(1,5):
    print(value)
# list 函数将range转换为列表
numbers = list(range(2,11,2))
print(numbers)
squares = []
for value in range(1,11):
    square = value**2
    squares.append(square)

print(squares)
digits = list(range(10))
print(digits)

print(min(digits))
print(max(digits))
print(sum(digits))
# 列表解析
squares = [value**2 for value in range(1,11)]
print(squares)
# 列表切片只包含开头不包含结尾
players = ['charles','martina','michael','florence','eli']
print(players[0:3])
for i in players[:3]:
    print(i.title())
# 复制列表
my_food = ['pizza','falafel','carrot cake']
friend_food = my_food[:]
print(my_food)
print(friend_food)
my_food.append('cannoli')
friend_food.append('ice cream')
print(my_food)
print(friend_food)
# 复制列表(行不通的方法)
my_food = ['pizza','falafel','carrot cake']
friend_food = my_food
my_food.append('cannoli')
friend_food.append('ice cream')
print(my_food)
print(friend_food)
# 元组 值不可变的
dimensions = (200, 5)
print(dimensions[0])
print(dimensions[1])
for i in dimensions:
    print(i)
# 虽然不能修改元组的元素,但是可以给存储元组的变量赋值
dimensions = (200, 5)
for i in dimensions:
    print(i)

dimensions = (1200, 15)
for i in dimensions:
    print(i)

The fifth chapter if statement

cars = ['audi','bmw','subaru','toyota']

for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())
# python 对大小写敏感
car = 'audi'
car == 'Audi'
food = 'mushrooms'

if food != 'pizza':
    print('no')
age_0 = 11
age_1 = 20

age_0 < 1 and age_1 > 5
age_0 = 11
age_1 = 20

age_0 > 1 and age_1 > 5
age_0 = 11
age_1 = 20

age_0 < 1 or age_1 > 5
age_0 = 11
age_1 = 20

age_0 < 1 or age_1 < 5
'a' in 'assxsxsx'
request = ['mushrooms','onions','pineapple']
print('mushrooms' in request)
print('abc' in request)
banned_users = ['andrew','carolina','david']
user = 'marie'

if user not in banned_users:
    print(user.title(),'you can post a response if you wish')

age = 17
if age >= 18:
    print("you are old enough to vote")
    print("Have you registered to vote yet?")
else:
    print('Sorry,you are too young to vote.')
age = 12
if age < 4:
    print('Your admission cost is $0')
elif age<18:
    print('Your admission cost is $5')
else:
    print('Your admission cost is $10')

age = 12
if age < 4:
    price = 0
elif age<18:
    price = 5
else:
    price = 10
print("Your admission cost is $%s"%str(price))
age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
else:
    # 这里用 elif 更好
    price = 5
print("Your admission cost is $%s" % str(price))
# 测试多个条件,不用 else 和 elif

request = ['mushrooms','extra cheese']

if 'mushrooms' in request:
    print('adding mushrooms')
if 'abd' in request:
    print('add abd')
if 'extra cheese' in request:
    print('add cheese')
request = ['mushrooms','extra cheese']

if 'mushrooms' in request:
    print('adding mushrooms')
elif 'abd' in request:
    print('add abd')
elif 'extra cheese' in request:
    print('add cheese')
request = ['mushrooms','extra cheese']
for i in request:
    if i == 'mushrooms':
        print('sorry,we are out of mushrooms ringh now.')
    else:
        print('adding',i)
request = []

if request:
    for i in request:
        print('adding',i)
else:
    print("Are you sure you want a plain pizza")
available = ['mushrooms','olives','green peppers','extra cheese']
request = ['mushrooms','french fires']

for i in request:
    if i in available:
        print('adding',i)
    else:
        print('sorry,we do not have',i)

Chapter 6 Dictionary

alien = {'color':'green'}
del alien['color']
alien
# 遍历键值对
a_num = {1:1,2:2,3:5,4:8}
for i,j in a_num.items():
    print(i*j)
# 遍历键
for i in a_num.keys():
    print(i)
# 遍历值
for i in a_num.values():
    print(i)

Chapter 7 user input and while loops

a1 = input('num')
print(type(a1))
# 标志 active
active = True
while active:
    message = input('repeat')
    
    if message == 'quit':
        active = False
    else:
        print(message)
unconfirmed_users = ['alice','brian','candace']
confirmed_users = []

while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    print('verifying: ' + current_user)
    confirmed_users.append(current_user)
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
    pets.remove('cat')

print(pets)
   
Published an original article · won praise 0 · Views 21

Guess you like

Origin blog.csdn.net/qq_39594033/article/details/104286762