day1 -- Python变量、注释、格式化输出字符串、input、if、while、for
1.python变量
不需要声明类型,直接 变量名 = 变量值,如 : name = "hahaha"
2.注释:
单行注释,前面加 #,如 # print(info)
多行注释,用三组单引号或者三组双引号包围起来,如 '''print(info)''' """print(info)"""
注意:三组单引号包围起来的变量可以直接格式化输出,看下面的3的第一个案例
3.格式化输出字符串
1 info = '''name is %s and age is %s''' % (name, age)
2 info2 = "name2 is {n} and age2 is {a}".format(n=name, a=age)
3 info3 = "name3 is {0} and age3 is {1}".format(name, age)
4.input()方法默认输入存储的类型是str,如果需要转换类型可以使用类型转换函数如int(),如:
age = int(input("input your age:"))
5.if、elif、else:注意顶格对齐
my_age = int(input("guess my age:"))
if my_age > 60:
print("you guess error")
elif my_age == 60:
print("you guess right")
else:
print("hhh")
6.while
count = 0
while count < 3:
age = int(input("guess my age:"))
if age < 50:
print("sorry, your answer is error")
elif age == 50:
print("{a} is right".format(a=age))
break
else:
print("hhh")
count += 1
# while条件不符合,则走else里面的
else:
print("you have tried too many times!!!")
7.for
for i in range(3):
age = int(input("guess my age:"))
if age < 50:
print("sorry, your answer is error")
elif age == 50:
print("{a} is right".format(a=age))
break
else:
print("hhh")
# for正常结束,则走else里面的,
# 如果for被break,则不走else里面的
else:
print("you have tried too many times!!!")
tip:range方法看下面
8.range方法
# 起始,终点,步长,输出0,2,4,6,8
for i in range(0, 10, 2):
print("i = ", i) # 默认步长为 1,即输出1-99
for j in range(99):
print(i)
day1 -- Python变量、注释、格式化输出字符串、input、if、while、for的更多相关文章
- Python 3.x 格式化输出字符串 % & format 笔记
Python 3.x 格式化输出字符串 % & format 笔记 python格式化字符串有%和{}两种 字符串格式控制符. 字符串输入数据格式类型(%格式操作符号) %%百分号标记 %c字 ...
- 2.Python基础认识(格式化输出,while语句,运算符,编码,单位转化)
Python基础认识 1.字符串的格式化初识及占位符的简单应用 字符串的格式化 按照既定的要求进行有规定排版的一种输出方式. #我们想要输出的格式如下: ----------------------- ...
- Python基础篇(格式化输出,运算符,编码):
Python基础篇(格式化输出,运算符,编码): 格式化输出: 格式:print ( " 内容%s" %(变量)) 字符类型: %s 替换字符串 %d 替换整体数字 ...
- python 基础之格式化输出
字符占位符%s #_cvvh:"chenxi" #date: 2019/6/24 print ('chhjg') # 格式化输出 name = input("Name:& ...
- Python 入门之格式化输出
Python 入门之格式化输出 1.格式化 (1)%为占位 (2)%s --- 站字符串的位置(数字.字符串都能够进行填充) name = input('请输入姓名:') age = input('请 ...
- python基础_格式化输出(%用法和format用法)(转载)
python基础_格式化输出(%用法和format用法) 目录 %用法 format用法 %用法 1.整数的输出 %o -- oct 八进制%d -- dec 十进制%x -- hex 十六进制 &g ...
- python之路-格式化输出、编码
格式化输出 user = input('Username:') pwd = input('Password:') msg = 'your username:%s,your password:%d,10 ...
- 语法注释格式;格式化输出;input在py2和py3中的区别;数据的基本类型;运算符;
一.Python中的注释 Python的注释是代码的评论,是让代码让人能更加清晰明确.代码的注释可分为单行注释和多行注释,单行注释用“#”,多行注释用三对单引号或者三对双引号来表示. ps:# 单行注 ...
- Python:Day05 格式化输出、列表
注释:3个单引号或3个双引号 3个引号(单引或双引)还有另外一个作用:打印多行. msg = """hello 1 hello 2 hello 3"" ...
随机推荐
- Linux-saltstack-1 saltstack的安装与基本配置
@ 目录 一.环境介绍 二.安装saltstack 1.配置安装yum源 2.安装salt-master 3.安装slat-minion 三.配置salt-master 1.指定master的地址 2 ...
- spring boot 解决 跨域 的两种方法 -- 前后端分离
1.前言 以前做项目 ,基本上是使用 MVC 模式 ,使得视图与模型绑定 ,前后端地址与端口都一样 , 但是现在有些需求 ,需要暴露给外网访问 ,那么这就出现了个跨域问题 ,与同源原则冲突, 造成访问 ...
- spring boot 启动读取的配置文件优先级
1.优先级从高到低 1. file:/config/ 2. file:/ 3. classpath:/config/ 4. classpath:/ 所有位置的application.properti ...
- react中使用styled-component
styled-component的使用地址(https://www.cnblogs.com/aichenxy/p/8672752.html)
- nginx配置指定域名访问,nginx禁止ip访问,配置空主机头 syscal
1.大家有过这方面的困扰,就是自己的网站给其他人恶意域名解析到自己的服务器ip上. 特别不爽,那大家可以用用空主机头的方法. 先给大家看下我的nginx.conf配置 http { log_forma ...
- Echart可视化学习(一)
文档的源代码地址,需要的下载就可以了(访问密码:7567) https://url56.ctfile.com/f/34653256-527823386-04154f 正文: 创建需要的目录结构及文件 ...
- 自定义Nginx日志格式获取IP地址的省市份信息
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6806672112477012493/ 在linux中nginx日志产生的格式是下面的配置: $remote_add ...
- Unity3D开发入门教程(四)——用Lua实现组件
五邑隐侠,本名关健昌,12年游戏生涯. 本教程以 Unity 3D + VS Code + C# + tolua 为例. 一.Lua组件基类 1.在 Assets/Lua 目录下新建com目录用于存放 ...
- 利用Spring AOP切面对用户访问进行监控
开发系统时往往需要考虑记录用户访问系统查询了那些数据.进行了什么操作,尤其是访问重要的数据和执行重要的操作的时候将数记录下来尤显的有意义.有了这些用户行为数据,事后可以以用户为条件对用户在系统的访问和 ...
- 【刷题-PAT】A1111 Online Map (30 分)
1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...