关于python:Kangrui‘s-Python-Learning-Dairy-20200721

55次阅读

共计 2409 个字符,预计需要花费 7 分钟才能阅读完成。

内容对于用户输出,while 循环以及函数
明天所学内容局部须要输出,所以不附运行后果,欢送大家本人尝试


以下内容为用户输出和 while 循环


# input()工作原理,实质是输出字符串
message = input("Tell me something, and I will repeat it back to you:")
print(message)

name = input("Please enter your name:")
print("Hello," + name + "!")

# int()工作原理,实质是将输出视为数值
height = input("How tall are you, in inches?")
height = int(height)
if height >= 36:
    print("\nYou're tall enough to ride! ")
else:
    print("\nYou'll be able to ride when you're a little older.")

# 求模运算符 %,省略

# while 循环
current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1

# 应用 break 退出循环
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter'quit'when you are finished.)"
while True:
    city = input(prompt)
    if city == 'quit':
        break
    else:
        print("I'd love to go to "+ city.title() +"!")

# 在循环中应用 continue,满足条件时不再执行接下里的程序,进入下一循环
current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    print(current_number)

以下内容为函数

# 向函数传递信息
def greet_user(username):
    # 形参 username
    print("Hello," + username.title() + "!")

greet_user('jesse')  # 实参 'jesse'

# 传递实参
def describe_pet(animal_type, pet_name):
    print("\nI have a" + animal_type + ".")
    print("My" + animal_type + "'s name is" + pet_name.title() + ".")


describe_pet('hamster', 'harry')  #  地位实参,最根本的是基于实参程序, 不能随便调换
describe_pet(animal_type = 'hamster', pet_name = 'harry')  # 关键字实参,能够换地位


def describe_pet(pet_name, animal_type = 'dog'):  # 蕴含默认值, 且默认值须要在前面
    print("\nI have a" + animal_type + ".")
    print("My" + animal_type + "'s name is" + pet_name.title() + ".")

describe_pet(pet_name = 'willie')

# 返回值
def get_formatted_name(first_name, last_name):
    full_name = first_name + ' ' + last_name
    return full_name.title()

musician = get_formatted_name('jimi', 'hendrix')
print(musician)

# 使参数可选
def get_formatted_name(first_name, middle_name, last_name):
    if middle_name:
        full_name = first_name + '' + middle_name +' ' + last_name
    else:
        full_name = first_name + ' ' + last_name
    return full_name.title()

musician = get_formatted_name('jimi','lee','hendrix')
print(musician)

# 返回字典
def build_person(first_name, last_name):
    person = {'first':first_name, 'last':last_name}
    return person

person = build_person('jimi', 'hendrix')
print(person)


# 传递列表
def greet_users(names):
    for name in names:
        msg = "Hello," + name.title() + "!"
        print(msg)

usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)

# 禁止函数批改列表
function_name(list_name[:])  #用切片表示法 [:] 创立列表的正本

# 传递任意数量的实参
    # 应用形参名 *names, 创立一个空元组,放入所有收到的值
    # 能够和一般实参联合应用

# 将函数存储在模块中,应用 import module_name
# 调用模块中的某个函数,module_name.function_name
# 只导入特定的函数,from module_name import function_1,function 2
# 应用 as 指定别名,from module import function as anothername;import module as anothername
# 导入模块中所有的函数,from module import *

正文完
 0