python 编程从入门到实践一

"""
备注:
"""
#修改字符串的大小写
#首字母大写
'''
name = "ada lovelace"
print(name.title())
print(name.upper())
print(name.lower())

first_name = 'ada'
last_name = 'lovelace'
full_name = first_name +" " + last_name
print(full_name)
print("Hello," + full_name.title() + "!")

print("\tPython")
print("Languages:\nPython\nC\nJavaScript")

favorite_language = 'python '
print(favorite_language)
print(favorite_language.rstrip())

>>> 3/2
1.5
>>> 3/2.0
1.5
>>> 3%2
1
>>> 3**2
9
>>> 2+3*4
14
>>> 0.1 + 0.1
0.2
>>> 3 + 1.2
4.2
>>> 0.2 + 0.1
0.30000000000000004
>>> 3*0.1
0.30000000000000004
>>> 2*0.1
0.2
>>> 2*0.2
0.4


age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)

#使用pop方法删除元素 与del 区别,del删除后不使用元素,pop删除后仍使用删除的元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

motorcycles = ['honda','yamaha','suzuki']
last_owned = motorcycles.pop()
print('The last motorcycle I owned was a ' + last_owned.title() + '.')
print(motorcycles.pop(0))
print(motorcycles)
#根据指定的值删除,如果有多个重复的只要删除第一个元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.append("ducati")
print(motorcycles)
motorcycles.remove("yamaha")
print(motorcycles)

#组织列表,使用方法sort()对列表进行永久性排序
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)

cars = ['bmw','audi','toyota','subaru']
cars.sort(reverse = True)
print(cars)

cars = ['bmw','audi','toyota','subaru']
print(sorted(cars))
print(cars)

cars = ['bmw','audi','toyota','subaru']
cars.reverse()
print(cars)

print(len(cars))
print(cars[-1])

#使用for循环遍历整个列表
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
print("Thank you !")


for value in range(1,5):
print(value)

numbers = list(range(1,6))
print(numbers)

print(list(range(0,11,2)))

#squares = []
#for value in range(1,11):
# square = value**2
# squares.append(square)
#
#print(squares)

squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)

#列表解析
squares = [value**2 for value in range(1,11)]
print(squares)

#切片,使用列表的一部分
players = ['charles','martina','michael','florence','eli']
#获取1-3的子列表
print(players[0:3])
#获取2-4的子列表
print(players[1:4])
#返回离列表结尾距离相应的元素
print(players[-3])

players = ['charles','martina','michael','florence','eli']
for player in players[:3]:
print(player.title())
#使用切片复制列表得到列表的另一个副本,修改一个列表并不影响另一个列表
#把一个列表赋值给另一个列表,两个列表指向同一个列表,修改原列表会影响副本
list_01 = [1,2,3]
list_02 = list_01[:]
print(list_01)
list_01.append(4)
print(list_01)
print(list_02)

list_03 = [4,5,6]
list_04 = list_03
list_03.remove(5)
print(list_04)
#元组不可修改,元组变量可以修改
t = (1,2,3)
print(t[0])
print(t[1])

t = [2,5]
print(t)

cars = ['audi','bmw','subaru','toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
>>> car = 'Audi'
>>> car
'Audi'
>>> car == 'audi'
False
>>> car.lower() == 'audi'
True

>>> car = 'Audi'
>>> car
'Audi'
>>> car == 'audi'
False
>>> car.lower() == 'audi'
True
>>> 6 != 5
True
>>> age = 18
>>> age == 18
True
>>> age = 19
>>> age < 21
True
>>> age <= 21
True
>>> age >= 21
False
>>> age_0 = 22
>>> age_1 = 18
>>> age_0 >= 21 and age_1 >= 21
False
>>> age_1 =22
>>> age_0 >=21 and age_1>=21
True
>>> (age_0>=21) and (age_1>=21)
True
>>> age_0=22
>>> age_1=18
>>> age_0>=21 or age_1>=21
True
>>> age_0 = 18
>>> age_0 >= 21 or age_1 >= 21
False
# in not in 检查值是包含在列表中
>>> li = [1,2,3]
>>> 1 in li
True
>>> 2 in li
True
>>> 5 in li
Falise
>>> 1 not in li
Falise
>>> 5 not in li
True

#游乐场收费标准:4岁以下免费,4到18岁5元,18岁(含)以上10元
def show_fee(age):
if age < 4:
return "免费"
#elif age >= 4 and age < 18:
elif age < 18:
return "5元"
else:
return "10元"

if __name__ == "__main__":
print(show_fee(3))
print(show_fee(4))
print(show_fee(6))
print(show_fee(18))

age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print("your admission cost is ¥" + str(price) + ".")
#当知道输入条件可以不用else,避免引入未知危害条件
age = 65
if age < 4:
price = 0
elif age < 18:
price = 5
elif age >= 18:
price = 10
print("your admission cost is ¥" + str(price) + ".")
#如果只想执行一个代码块使用if-elif-else 如果要执行多个代码块就要运行一系统独立的if条件
li = ['reganmian','banhuanggua','huashengmi']
if 'reganmian' in li:
print("add reganmian")
if 'banhuanggua' in li:
print("add banhuanggua")
if 'nirou' in li:
print('add niurou')
#优化
li = ['reganmian','banhuanggua','huashengmi']
for i in li:
print("adding " + i + '.')

print("Done!")

li = ['reganmian','banhuanggua','huashengmi']
for i in li:
if i == 'huashengmi':
print("sory,we are out of huashengmi ")
else:
print("adding " + i + '.')

print("Done!")

li = ['reganmian','banhuanggua','huashengmi']
for i in li:
if i == 'huashengmi':
print("sory,we are out of huashengmi ")
else:
print("adding " + i + '.')

print("Done!")
#判断列表是否为空
li = []
li.append(1)
if li:
for i in li:
print(i)
else:
print("列表是空的")


#使用多个列表
li_0 = [1,2,3,4,5]
li_1 = [5,8,9]
for i in li_1:
if i in li_0:
print("adding " + str(i) + '.')
else:
print("sorry,we don't find " + str(i) + '.')
print("Done!")

#字典的创建 添加值
student = {'name':'liming','age':22}
print(student['name'])
print(student['age'])
print('you are ' + str(student['age']) + " years old")
student['address']='江苏省'
student['ID']= '0250001'
print(student)
del student['address']
print(student)

dt = {}
dt['color'] = 'green'
dt['point'] = 5
print(dt)
dt['color'] = 'yellow'
print(dt)


favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}

print("Sarah's favorite language is " +
favorite_languages['sarah'].title() +
'.')

for k,v in favorite_languages.items():
print("\nKey: " + k)
print("Value: " + v)

for name in favorite_languages:
print(name.title())

for name in favorite_languages.keys():
print(name.title())
if 'erin' not in favorite_languages.keys():
print("Erin,please take our your poll!")
for name in sorted(favorite_languages.keys()):
print(name.title())

for language in favorite_languages.values():
print(language)

#遍历字典的值,去重打印
for language in set(favorite_languages.values()):
print(language)

#嵌套 字典列表
li_0 = {'c':'green','p':5}
li_1 = {'c':'yellow','p':10}
li_2 = {'c':'red','p':15}
li = [li_0,li_1,li_2]
print(li)
for i in li:
print(i)

aliens = []
for alien_number in range(30):
new_a = {'c':'green','p':5,'speed':'slow'}
aliens.append(new_a)
print(len(aliens))
for alien in aliens[:5]:
if alien['c'] == 'green':
alien['c'] = 'yellow'
alien['p'] = 10
alien['speed'] = 'medium'
print(alien)
print("...")


pop_lang = {
'a':['python','ruby'],
'b':['c'],
'c':['ruby','go'],
'd':['python','java']
}
print(pop_lang)
for i in pop_lang['a']:
print(i)

for k,v in pop_lang.items():
print("%s=%s" %(k,v))
for name,languages in pop_lang.items():
print("\n" + name.title() + "'s favorite languages are:")
for language in languages:
print("\t" + language.title())
#input 与 while
mesg = input("请输入你的姓名:")
print("Hello, " + mesg + "!")

prompt = "If you tell us who you are,we can personalize the messaages you see."
prompt += '\nWhat is your first name?'
name = input(prompt)
print("\nHello, " + name + "!")

#使用int来获取数值输入
age = input("How old are you? ")
print(age)
age = int(age)
if age >= 18:
print("you have vote")

num = input("请输入一个整数,我告诉你是奇数还是偶数:")
num = int(num)

if num%2 == 0:
print("您输入的数" + str(num) + "是偶数")
else:
print("您输入的数" + str(num) + "是奇数")

#while循环
i = 1
while i < 6:
print(i)
i +=1
mesg = "输入'quit'循环退出,否则循环一个运行"
m = ""
while m != 'quit':
m = input(mesg)
print(m)

mesg = "输入'quit'循环退出,否则循环一个运行:"
m = ""
while m != 'quit':
m = input(mesg)

if m != 'quit':
print(m)

mesg = "输入'quit'循环退出,否则循环一个运行:"
flag = True
while flag:
m = input(mesg)

if m == 'quit':
flag = False
else:
print(m)

mesg = "输入'quit'循环退出,否则循环一个运行:"
while True:
m = input(mesg)

if m == 'quit':
break
else:
print(m)

i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue

print(i)


#for 循环遍历时不能修改修改,while可以
#在列表之间移动元素

unconfirmed = ['alice','brian','candace']
confirmed = []
while unconfirmed:
cur_user = unconfirmed.pop()
print("已验证用户:" + cur_user.title())
confirmed.append(cur_user)
#显示所有已验证用户
print("已经验证的用户:")
for c in confirmed:
print(c.title())

#删除包含特定值的元素

p = ['a','b','c','d','a']
print(p)
while 'a' in p:
p.remove('a')
print(p)

#使用用户输入来填充字典
questions = {}
flag = True
while flag:
name = input("\nwhat's your name? ")
question = input("Which mountain would you like? ")

questions[name] = question
repeat = input("还有人参加调查么?(yes/no) ")
if repeat == 'no':
flag = False
print("\n调查结果:")
for name,question in questions.items():
print(name + " would like to climb " + question + '.')
'''

def greet_user():
print("Hello")

greet_user()

posted on 2020-06-02 07:55  sunny_2016  阅读(128)  评论(0编辑  收藏  举报

导航