一、Python环境
windows环境安装Python步骤
1 1、下载安装包:https://www.python.org/downloads/windows/ 3 2、安装:默认安装路径:C:python27 5 3、配置环境变量
二、Python初识
2.1 第一个Python程序编写(pycharm python)
print ("hello,python!!!")
# Python2输出中文问题 首行添加 # -*- encoding:utf-8 -*-
# python3不存在输出中文问题
2.2 Python注释
1.单行注释 # 2.多行注释 '''''' """"""(三个单引号或者三个双引号)
# 在Python中单引号与双引号作用一样
# 三个引号(单/双)在字符串中表示原样输出
2.3 Python中的变量与常量
变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用
name = 'zhangsan' # 声明变量 #变量定义规范: 1.必须是字母数字下划线组合 2.数字不能开头 3.不能是关键字
常量,Python中没有常量概念,通常变量名大写表示常量
NAME='zhangsan'
2.4 python基础数据类型初识
1.整数类型(int)
# 除了int 外还有float,复数等 2.字符串类型
# 在Python中,加了引号的字符都被认为是字符串 (单引号、双引号、三引号) 3.bool类型 True False
2.5用户交互
# 接收用户输入,获取字符串 name = input("请输入姓名:") # 打印输出用户输入内容 print(name)
2.6流程控制 (if条件语句)
1.if else语句
# 单分支
name = 'zhangsan' if name == 'zhangsan': print("你好,张三")
# 双分支
name = 'zhangsan' if name == 'zhangsan': print("你好,张三") else : print("抱歉,你不是张三")
2.if elif else语句
num = 10 if num > 50 : print("num > 50") elif num > 30: print("num >30 and num <= 50") else: print("num<=30")
# Python中的语句块跟缩进有关
2.7循环语句 (for、while)
1.for循环
# for循环 s = 'sadifa' for i in s: print(i)
2.while循环
# while 循环 i = 1 while i < 5: i += 1 print(i) # while else i = 1 while i < 5: i += 1 print(i) else: print("执行结束")
2.8 Python中的格式化输出
格式化输出目的,代码简介
#格式化输出 # % s d name = input('请输入姓名') age = input('请输入年龄') height = input('请输入身高') msg = "我叫%s,今年%s 身高 %s" %(name,age,height) print(msg)
name = input('请输入姓名:') age = input('请输入年龄:') job = input('请输入工作:') hobbie = input('你的爱好:') msg = '''------------ info of %s ----------- Name : %s Age : %d job : %s Hobbie: %s ------------- end -----------------''' %(name,name,int(age),job,hobbie) # input用户交互得到的都是字符串 print(msg)
name = input('请输入姓名') age = input('请输入年龄') height = input('请输入身高') msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height) # 就是要输出%时,需要使用%转义 print(msg)
2.9Python中的逻辑运算符 (and 、or、not)
#and or not #优先级,()> not > and > or print(2 > 1 and 1 < 4) # True print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)# T OR T OR F -- > T print(3>4 or 4<3 and 1==1) # F or F --> F print(1 < 2 and 3 < 4 or 1>2) # T or F --> T print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # T print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # F print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F #ps int ----> bool 非零转换成bool True 0 转换成bool 是False #ps str ----> bool 非""转换成bool True # print(bool(2)) print(bool(-2)) print(bool(0)) #bool --->int print(int(True)) # 1 print(int(False)) # 0 '''x or y x True,则返回x''' print(1 or 2) # 1 print(3 or 2) # 3 print(0 or 2) # 2 print(0 or 100) # 100 print(2 or 100 or 3 or 4) # 2 print(0 or 4 and 3 or 2) '''x and y x True,则返回y''' print(1 and 2) print(0 and 2) print(2 or 1 < 3) print(3 > 1 or 2 and 2)