python循环定义变量_Python 变量循环

Python 变量-循环

一、变量

不管什么编程语言或脚本语言 在定义变量时都有一定的规则。Python变量定义规则如下:

变量名只能是字母、数字或下划线的任意组合

变量名的第一个字符不能是数字

关键字不能当变量名,如下:

'and'、'as'、'assert'、'break'、'class'、'continue'、'def'、'del'、'elif'、'else'、'except'、'exce'、'finally'、'for'、'from'、'if'、'is'、'in'、'global'、'import'、'lambda'、'not'、'or'、'pass'、'print'、'raise'、'retuen'、'try'、'while'、'with'、'yield'

声明变量和赋值

字符串变量声明:字符串必须加引号. 单引、双引都可以。主要含有字母都必须加引号

Name = "Yuhl1"

数字类型变量声明:数字类型可不加引号

Age = 27

Name 是变量名,Yuhl1是变量值。下边Age同等

注释

单行注释

# 被注是的内容

多行注释

""" 被注释的内容 """

二、Python 第一个程序

在linux下创建一个.py文件,如下:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

_Author_ = "Yuhl"

Google_Mail = "cnblogs.honglin@gmail.com"

# 提示前台输入 关键字"input"

Google_Url = input("Please Input Google The Url:")

print(Google_Url)

print(Google_Mail)

三、循环语句

if 语句

外层变量可以被内层代码使用,内层代码无法被外层代码使用

user_name = input("Please input user name:")

password = input("Please input user password:")

if user_name == "Yuhl" and password == "abc123"

print("welcome %s Login successful"%user_name)

else:

print("username and password errors..")

--------------------------------------------------------------------------------------------------------------------------

my_age = 27

user_age = input("Please Input your age")

if user_age == my_age:

print("Congratulations, you got it")

elif user_age > my_age:

print("Oops,got guess bigger")

else:

print("Oops,got guess small")

for 语句

注意for语言的嵌套使用

打印十个数字

for line in range(10):

print(line)

流程控制 只打印数字5后面的

for line in range(10):

if line > 5:

print(line)

for循环猜年龄游戏【只能猜三次】

Yuhl_age = 27

for line in range(3):

user_age = input("Please Input user age:")

if user_age == Yuhl_age:

print("welcome your guessed it")

elif user_age > Yuhl_age:

print("input errors larger. please again input use age")

else:

print("input errors smaller. please again input use age")

for else结合使用猜年龄游戏讲解【for else】

Yuhl_age = 27

for line in range(3):

user_age = input("Please Input user age:")

if user_age == Yuhl_age:

print("welcome your guessed it")

elif user_age > Yuhl_age:

print("input errors larger. please again input use age")

else:

print("input errors smaller. please again input use age")

else:

exit("Too Many Attemots....")

PS:for else的含义是程序在正常执行的时候走else下的代码.如果中间程序有呗break的那么就不会执行else里代码

如果以下代码中的if 判断和break注视加上. 那么会执行else中的代码. 正常执行

如果以下代码中的if 判断和break注视未加. 那么不会执行else中的代码. 这就是区别. 中间截断不执行else下数据

for i in range(10):

print(i)

if i == 5:

break

else:

print("程序正常结束")

for 循环嵌套讲解01

for i in range(5):

for j in range(5):

print(i,j)   #  此处pint包含3

if j > 3:

break   #  break跳出整个当前层循环. 只跳一层

print(i,j)   #  此处pint不包含3. 逻辑问题

for 循环嵌套讲解02

for i in range(10):

for j in range(10):

print(i.j)   #  此处print的话包含6

if j < 5:

break   #  break. 外层大循环依旧被执行过. 嵌套循环中j < 5就break了. 那么下边的print就不会被执行

continue  #    跳出当前循环. 继续下一次循环

print(i,j)   #    此处print的话不包含6 逻辑问题

while 语句

while 死循环

count = 0

while True:

print("welcome your enter while Infinite loop.....%s"%count)

count += 1

while 语句if进行流控制.只打印某一点数据

count = 0

while True:

if count == 9999

print("welcome your enter while Infinite loop.....%s"%count)

break

count += 1

while 语句. 只打印多少次

count = 0

while count < 10:

print("welcome your enter while Infinite loop.....%s"%count)

count += 1

while 语句.加if控制流

count = 0

while True:

print("welcome your came to Heaven on earth")

if count == 5:

print("What a paradise on earth it is hell on earth")

break

count += 1

while 猜年龄游戏

count = 0

YHL_age = 27

while count < 3:

"""strip() #去除空格 isdigit()判断是不是整数 int()转换成整数 """

input_age = input("Please input age:").strip()

if input_age.isdigit():

input_age = int(input_age)

else:

continue

if input_age == YHL_age:

print("guess correct .....")

break

elif input_age == YHL_age:

print("guess the.")

else:

print("Guess a little")

count += 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Python中,可以使用不同的方法在循环中创建不同的变量名。一种方法是使用字符串拼接的方式来创建变量名,然后使用`exec()`函数将字符串定义为符号变量。例如,可以通过循环创建带有不同数字后缀的变量名,然后使用`exec()`函数将字符串定义为对应的变量。另一种方法是使用`locals()`函数和字符串格式化来动态定义变量。通过在字符串前加上`f`并使用大括号内的Python表达式,可以创建带有不同后缀的变量名。接下来是两种方法的示例代码: 方法一: 使用`exec()`函数 ```python import sympy as sp def define_symbols(num): for i in range(num): str_sym = "x" + str(i) # 创建字符串形式的变量名 sym = sp.symbols('%s' % str_sym) # 将字符串定义为符号变量 exec(f"{str_sym} = {sym}") # 使用exec函数将字符串定义变量 # 调用示例 define_symbols(5) print(x0, x1, x2) # 输出变量值 ``` 方法二: 使用`locals()`函数和字符串格式化 ```python def define_symbols(num): for i in range(num): var_name = f"var_{i}" # 创建变量名 locals()[var_name = i # 使用locals()函数动态定义变量 # 调用示例 define_symbols(5) print(var_0, var_1, var_2) # 输出变量值 ``` 这两种方法都可以在循环中创建不同的变量名,并且在后续的代码中使用这些变量。根据具体的需求,可以选择适合的方法来动态定义变量名。123 #### 引用[.reference_title] - *1* [【Python】小技巧—自动循环定义符号变量](https://blog.csdn.net/qq_50920297/article/details/124582236)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *2* [python循环中创建并使用不同变量名](https://blog.csdn.net/gsgbgxp/article/details/117769685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *3* [Python|循环创建变量/规律性命名变量](https://blog.csdn.net/fangyibo24/article/details/122821596)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值