Python基础1之变量、判断语句、循环语句等

一、变量

  • 变量名只能是 字母、数字或下划线的任意组合
  • 变量名的第一个字符不能是数字
  • 以下关键字不能声明为变量名

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

  • 变量名应该有意义,且命名规则有下划线法和驼峰法

二、注释

  • 单行注释  #后均为注释内容,且为了保证可读性,#后添加一个空格,直接在代码后注释的时候,#前空两个格
  • 多行注释 连续的三对引号(单引号双引号都可以),多行注释可以当多行字符串
  • # -*-coding utf-8-*- 放在第一行,声明使用的是utf8字符编码

三、格式化输出

第一种,利用+连接字符串

name = "hui"
str = '''
hello
my name is '''+name+''',hahahh'''

print(str)

第二种,利用占位符

name = "huihui"
str = '''
my name is %s,hahahh
'''%(name)

print(str)

第三种,利用{}

name = "huihui"
str = '''
my name is {_name},hahahh
'''.format(_name=name)

print(str)

四、循环语句

while循环

count = 0
while true:
    print("hello world")
    count +=1
    if(count == 10)
        break
    

for循环

for i in range(10):
    print("hello world")

五、判断语句

if……else

count = 0

if count < 10 :
    print("hello world")
    count +=1
else :
    print("xxxxxxx")

if……elif……else

count age = 10

num = int(input("age:"))

if num == age :
    print("num = age")
elif num > age :
    print("num > age ")
else :
    print("num < age")

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值