变量和简单的数据类型

一.变量、字符串、print、类型转换、运算符

message = "Hello Python world" #字符串变量
a,b,c = 9,9,3

print(message)
print("apple"+str(4))
print(1.4+int("1"))
print(a%4,b//4,c**2)

0f0b46ef-d269-4a7c-8123-8b889528a7a6

二.语句

  1. while循环
condition = 1
while condition < 10:
  print(condition)
  condition = condition + 1

cf2d26af-ea78-466c-8b3f-49f0aff5364b

  1. for循环
ex_list = [1,2,3,4,5]
for i in ex_list:
    print(i)
    print('inner of for')

3f155dd0-5cf8-4732-8bbb-5e3c011eb5d3

  • 可加参数step
  • range的范围1~9:
for i in range(1,10):
    print(i)

95601696-cc87-4fe0-a699-3f9efba98c8b

  1. if、elif、else
x=5
y=5
z=9
if x<5>4:
    print('x<5>4')
elif (x<5)<4:
    print(f'(x<5)<4:{x<5}')
else:
    print('else')

7b5978e8-8328-4452-b62b-fed7cc2f0211

  • x<y<z被解释为(x<y) and (y<z)
  • f字符串
    print(f'(x<5)<4:{x<5}')
    要在字符串中插入变量的值,可先在左引号前加f,再将变量放入花括号内
posted @ 2025-11-10 02:03  骆驼丶浮云  阅读(3)  评论(0)    收藏  举报