• python编程复习系列——week1(Input & Output)



    前言

    主题描述
    🎈本课程使用现代编程语言介绍
    介绍程序设计的基本概念,如过程编程、变量、数据类型、数组、递归函数、条件表达式、选择语句、重复指令等。

    🎈本主题还使用现代编程语言来描述
    数据结构和算法的基本概念,如堆栈、链表、队列、deque、排序、搜索、二叉树。

    🎈随着适当的抽象数据类型和算法的发展,
    这门课提高了学生在设计和实现结构良好的算法以解决广泛的现实问题方面的技能。


    0、我们的第一个Python程序

    # My first Python program
    print("PPP Y Y TTTTT H H OO N N")
    print("P P Y Y T H H O O NN N")
    print("PPP Y T HHHH O O N N N")
    print("P Y T H H O O N NN")
    print("P Y T H H OO N N")
    # print blank lines
    print()
    print()
    # print greetings
    print("Welcome to Python!")
    
    # print hello and greeting
    print("Hello World!")
    print('Welcome to Python!')
    # print hello and greeting and silly stuff :-)
    print("Hello World!", end="frog")
    print("Welcome to Python!", end="cat")
    print("How are you?")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    一、变量和数据类型

    1.变量是用来存储值的保留存储位置

    ● str:字符串表示一系列字符。我们使用双引号或单引号来创建字符串。

    始终使用具有有意义的名称和正确数据类型的变量

     first_name = "John"
     last_name = "Smith"
     age = 20
    
    • 1
    • 2
    • 3

    永远不要使用像a、b、c、x、y、z或诸如此类的变量。

    2.变量以特定的数据类型存储值。常见数据类型:

    🧨str:字符串表示一系列字符。我们使用双引号或单引号来创建字符串。

    first_name = "John"
     state = 'New South Wales'
    
    • 1
    • 2

    🧨int:整数

     age = 20
    
    • 1

    🧨float:十进制数

    interest_rate = 5.2
    
    • 1

    🧨bool:布尔值为True或False

    scan_completed = True
     virus_found = False
    
    • 1
    • 2

    每个变量都有一个数据类型。检查数据类型:type(变量名)
    字符串:使用双引号或单引号

    #字符串型
    first_name = "John"
    last_name = 'Smith'
    print(type(first_name))
    print(type(last_name))
    #整型
    age = 20
    temperature = -5
    credit_point = 6
    print(type(age))
    print(type(temperature))
    print(type(credit_point))
    #浮点型
    price = 30.5
    interest_rate = 3.18
    print(type(price))
    print(type(interest_rate)) <class 'float'>
    Some important math constants
    import math
    pi = math.pi
    e = math.e
    tau = math.tau
    print(pi)
    print(e)
    print(tau)
    
    #Boolean: True or False
    virus_scan_completed = True
    virus_found = False
    print(type(virus_scan_completed))
    print(type(virus_found))
    
    #Boolean Example:
    temperature = -5
    temperature_negative = (temperature < 0)
    print(temperature_negative)
    temperature_positive = (temperature > 0)
    print(temperature_positive)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    🧨日期数据类型:包括年、月、日(非时间)

    import datetime
    today_date = datetime.date.today()
    us_election_2020 = datetime.date(2020, 11, 3)
    print(type(today_date))
    print(type(us_election_2020))
    
    #Date-time data type: including year, month, day, hour, minute, second, ...
    import datetime
    current_date_time = datetime.datetime.now()
    christmas_2020 = datetime.datetime(2020, 12, 25)
    random_date_time = datetime.datetime(2000, 12, 20, 14, 20, 39, 555)
    print(type(current_date_time))
    print(type(christmas_2020))
    print(type(random_date_time))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ✨变量仅包含数据信息

    3.字符串添加(连接)

    # name details
    first_name = "John"
    last_name = "Smith"
    # use string addition to formulate the full name
    full_name = first_name + " " + last_name
    # display the full name
    print("My name is " + full_name + ".")
    #My name is John Smith.
    
    # name details
    first_name = "John"
    last_name = "Smith"
    # use string addition to formulate the full name
    full_name = first_name + " " + last_name
    # display the full name
    print("My name is " + full_name + ".")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4.字符串乘法(带数字)!

    # display some silly strings
    silly1 = "frog" * 7
    silly2 = 5 * "I am Sam "
    print(silly1)
    print(silly2)
    
    #结果
    #frogfrogfrogfrogfrogfrogfrog
    #I am Sam I am Sam I am Sam I am Sam I am Sam
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5.从用户处获取输入

    # ask the user to enter first name and last name
    first_name = input("Enter your first name: ")
    last_name = input("Enter your last name: ")
    # use string addition to formulate the full name
    full_name = first_name + " " + last_name
    # display the full name
    print("My name is " + full_name + ".")
    #结果
    #Enter your first name: Mary
    #Enter your last name: Wilson
    #My name is Mary Wilson.
    
    # Ask the user to enter 3 subjects
    print("You must choose 3 subjects.")
    print()
    subject1 = input("Enter the 1st subject: ")
    subject2 = input("Enter the 2nd subject: ")
    subject3 = input("Enter the 3rd subject: ")
    # Display subjects
    print()
    print("You have chosen: " + subject1 + ", " + subject2 + ", " + subject3 + "." )
    #You must choose 3 subjects.
    #Enter the 1st subject: ISIT111
    #Enter the 2nd subject: MATH101
    #Enter the 3rd subject: ACCY113
    #You have chosen: ISIT111, MATH101, ACCY113.
    
    #重写代码以使其更清晰。当我们有很多字符串添加时,用这种方式写它,使代码更清晰!
    # Ask the user to enter 3 subjects
    print("You must choose 3 subjects.")
    print()
    subject1 = input("Enter the 1st subject: ")
    subject2 = input("Enter the 2nd subject: ")
    subject3 = input("Enter the 3rd subject: ")
    # Display subjects
    print()
    print("You have chosen: " 
     + subject1 + ", " 
     + subject2 + ", " 
     + subject3 + "."
    )
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    二、在数据类型之间转换

    1.将数字转换为字符串

    fav_number = 7
    # display favorite number
    print("My favorite number is " + fav_number)
    #编写这个python代码并运行它。您将看到该代码无法运行,因为有一个错误。这个代码有什么问题?
    
    
    #正确应该是:
    # favorite number
    fav_number = 7
    # display favorite number
    print("My favorite number is " + str(fav_number))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.将字符串转换为数字

    # Ask the user to enter 2 integers and display the sum
    user_input1 = input("Enter the 1st integer: ")
    number1 = int(user_input1)
    user_input2 = input("Enter the 2nd integer: ")
    number2 = int(user_input2)
    # calculate the sum
    number_sum = number1 + number2
    # display the sum
    print("The sum is " + str(number_sum))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.将字符串转换为十进制数字

    # Ask the user to enter 2 decimal numbers and display the sum
    user_input = input("Enter the 1st number: ")
    number1 = float(user_input)
    user_input = input("Enter the 2nd number: ")
    number2 = float(user_input)
    # calculate the sum
    number_sum = number1 + number2
    # display the sum
    print("The sum of " 
     + str(number1)
     + " and " 
     + str(number2)
     + " is "
     + str(number_sum)
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.在字符串和日期之间的转换

    import datetime
    # ask the user enter dob in DD/MM/YYYY format
    user_input = input("Enter your dob (DD/MM/YYYY): ")
    # convert string type to date type
    date_format = '%d/%m/%Y' 
    dob = datetime.datetime.strptime(user_input, date_format).date()
    # convert date to string
    print("Your dob is " + dob.strftime("%d/%b/%Y"))
    print("Your dob is " + dob.strftime("%d-%m-%Y"))
    #Enter your dob (DD/MM/YYYY): 26/03/2000
    #Your dob is 26/Mar/2000
    #Your dob is 26-03-2000
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    三、输入和输出(前面已经结束完毕)

    1.print function(打印函数)

    print
    
    • 1

    2. input function(输入函数)

    input
    
    • 1

    四、关键字

    以下列表显示了Python关键字。这些都是保留词,我们不能使用它们作为常量或变量或任何其他标识符名称。

  • 相关阅读:
    C/C++获取文件大小
    市场调研实业怎样使用自动化程序自动识别信息
    [附源码]计算机毕业设计springboot高校流浪动物领养网站
    【云原生之k8s】Yaml文件详解
    Lactoferrin-PEG-alginate 乳铁蛋白-聚乙二醇-海藻酸钠
    目标检测系列——Fast R-CNN
    java计算机毕业设计考试编排管理系统源码+mysql数据库+系统+lw文档+部署
    四、T100固定资产之固定资产折旧计提
    成功的性能测试方法的 3 个阶段
    【Java】main方法的深入理解
  • 原文地址:https://blog.csdn.net/qq_43499381/article/details/134319537