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

python循环定义变量_Python 变量循环相关推荐

  1. python定义一个整数变量_python循环定义多个变量的实例分析

    python循环定义多个变量方法 我们可能会时长碰到这样一个场景,计算得到一个非固定值,需要根据这个值定义相同数量个变量. 实现方式的核心是exec函数,exec函数可以执行我们输入的代码字符串. e ...

  2. python循环定义变量_Python变量和循环

    1.Python变量 比C语言,Java语言更加简洁,不需要加int等等类型定义,直接变量名 = 值,Python里甚至不需要分号.有些特定的不能当做变量名,变量只能由字母.数字和下划线组成,下划线可 ...

  3. python定义一个整数变量_Python变量与常量

    1.什么是变量 a=1,其中 a 就是变量名称,1 就是它的值.在程序运行过程中,变量的值一般都会发生改变,内存中会专门开辟一段空间,用来存放变量的值,而变量名将指向这个值所在的内存空间.与变量相对的 ...

  4. python怎么定义int变量_Python 变量类型 | 菜鸟教程

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

  5. python定义整数型变量_Python变量类型有哪些?Python变量声明介绍

    变量不过是用于定义,存储和对输入数据执行操作的编程元素.Python变量类型有哪些?Python变量有四种不同类型,它们是Integer,LongInteger,Float和String.整数用于定义 ...

  6. python定义int变量_Python变量以及常用数字类型(上)

    好好学习,天天向上.又到了齐小猴写笔记的时间,今天的内容是python 变量以及常用数字类型,废话不多说,撸起袖子开始写 变量 1.说到变量,先回顾上一篇说过的标识符,自己定义,自己命名,由字母,下划 ...

  7. python如何实现隔行_Python编写循环的两个建议 | 鹅厂实战

    作者 | piglei(腾讯高级工程师) 转载自腾讯技术工程知乎专栏 循环是一种常用的程序控制结构.我们常说,机器相比人类的最大优点之一,就是机器可以不眠不休的重复做某件事情,但人却不行.而" ...

  8. python画魔法阵_Python编写循环的两个建议 | 鹅厂实战!

    本文系 "Python 工匠"系列的第 7 篇文章,已取得作者授权. 循环是一种常用的程序控制结构.我们常说,机器相比人类的最大优点之一,就是机器可以不眠不休的重复做某件事情,但人 ...

  9. python中for循环的代码_Python for循环及基础用法详解

    Python 中的循环语句有 2 种,分别是 while 循环和 for 循环,前面章节已经对 while 做了详细的讲解,本节给大家介绍 for 循环,它常用于遍历字符串.列表.元组.字典.集合等序 ...

  10. python中循环结构关键字_Python的循环结构,也简单!

    Python语言的循环结构包括两种:遍历循环和无限循环.其中遍历循环由for保留字建立,用来一次访问元素组中的每一个元素:无限循环由while保留字建立,依据是否满足特定条件决定是否结束循环. 1.遍 ...

最新文章

  1. JAVA 多用户商城系统b2b2c-kafka处理超大消息
  2. Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用...
  3. python3 for循环怎么用_Python3入门系列之-----循环语句(for/while)
  4. 看日本雅-miyavi演唱会
  5. XEON® Scalable-如何为虚拟化挑选合适的CPU
  6. ECSHOP商城网站建设之自定义调用广告方法(二)
  7. Hive 官网函数全列表(聚合函数/日期函数/字符串函数...)
  8. OKR实施细则(转)
  9. 5G技术详解系列-PDU会话签约数据(6)
  10. kafka不消费:9092 (id: 0 rack: null)
  11. 关于抢购秒杀的实现思路与事例代码
  12. AD9854+STM32正弦波信号发生器
  13. [MP3]MP3固件持续分享(2019.1.25)
  14. 博弈论--耶鲁大学公开课
  15. 系统虚拟化- 原理与实现
  16. 【0基础入门课程】实战入门柿饼UI,带你实现动感汽车仪表盘、APP Store等
  17. 什么是标准化,规范化,系统化?
  18. 黄金连分数(python)
  19. 大多数的愤怒源于自己的无知——《Excel受保护视图》
  20. YOLO—V1细节分析

热门文章

  1. “极链AI云”,专业的共享算力平台和紧跟前沿的AI技术社群
  2. angular集成websocket_Angular Websocket教程
  3. 线程池Executors.newFixedThreadPool
  4. 编程定义一个汽车类Car
  5. 火狐64版本禁止自动更新提示方法
  6. 移远5G模组RM500U-CN在Win11下的短信和通话演示
  7. 【QT开发笔记-基础篇】| 第二章 常用控件 | 2.11 列表框 QListWidget
  8. 汉文SEO大牛教你如何创建网站地图-seo必修课
  9. 阿里云镜像地址网易云镜像
  10. 阿里云镜像站下载问题