Chapter 2 Variables and Simple Data Types

2-1 Store a message into a variable and print it out.
The file name is simple_message.py

message = "下午好"
print(message)

2-2 Store a message in a variable and print it out; then modify the value of the variable to a new message and print it out.
The file name is simple_messages.py

message = "下午好"
print(message)
message = "吃饭了吗"
print(message)

2-3 Personalized message: Store the user's name in a variable and display a message to the user. The displayed message should be very simple, like "Hello Eric, would you like to learn some Python today?"

name = "Eric"
print(f"Hello {
      
      name}, would you like to learn some Python today?")

2-4 Adjust the case of the name: store a person's name in a variable, and then display the person's name in lowercase, uppercase and initial capitalization.

name = "  li jiahao  "
print(name)
print(name.title())#将字符串中的每个单词的首字母改为大写
print(name.upper())#将字符串全部改为大写
print(name.lower())#将字符串全部改为小写
print(name.rstrip())#删除字符串前的空格
print(name.lstrip())#删除字符串后的空格
print(name.strip())#删除字符串前后的空格

2-8. Write four expressions that use addition, subtraction, multiplication, and division respectively, but all result in the number 8. In order to use the print statement to display the results, be sure to enclose these expressions in parentheses, that is, you should write code similar to the following:

""" 
姓名:lijiahao
时间:2023.4.21
"""
print(2+2+4)
print(10-2)
print(2*4)
print(16/2)
8
8
8
8.0

2-9. Store your favorite number in a variable, then use this variable to create a message, point out your favorite number, and print the message.

favorite_number = 5
print( "我最喜欢的数字是:"+ str(favorite_number))
我最喜欢的数字是:5

Guess you like

Origin blog.csdn.net/lijiahao1212/article/details/130213174