一 编程语言 什么是编程语言? 上面提及的能够被计算机所识别的表达方式即编程语言,语言是沟通的介质,而编程语言是程序员与计算机沟通的介质。在编程的世界里,计算机更像是人的奴隶,人类编程的目的就命令奴隶去工作。 什么是编程? 编程即程序员根据需求把自己的思想流程按照某种编程语言的语法风格编写下来,产出的结果就是包含一堆字符的文件。 强调: 程序在未运行前跟普通文件无异,只有程序在运行时,文件内所写的字符才有特定的语法意义。二 计算机组成部分 CPU 内存 外存 输入设备 输出设备三 python的安装与使用 1.安装python解释器 2.安装pycharm编辑器 3.编写python代码,并输出打印hello world!四 变量可变化的量。 变量值: 是真实存放在内存中的一块内存地址。 变量名: 用于与变量值绑定关系的。 赋值=号: 将变量的值绑定给变量名的。 print("hello world!") name="Tank"//定义变量变量命名规范:1.驼峰命名法 AgeOfTank2. 推荐命名:age_of_tank变量名定义规范:1.要以英文字母或者下划线开头,不能以数字开头2.关键字不能命名变量名规范age_of_Tank=18不使用中文命名定义变量的三个特征:id #用来表示内存中的唯一内存地址type #变量值类型value #变量值name1 ='tank1'name2 ='tank1' # id:变量的值一样,内存地址是不一样的。 # nameI =‘ tankI'| # name2 = ' tank1' # python优化机制(小计数池) #在某个长度内,python把值相同的变量值统一存放在同一一个内存地址中。 # print (id (namel))# print (id(name2)) 自# type:用于判断变量的类型 str1 =’hello' print (type(str1)) # value str2 =’hello' print(str1 == str2)五 常量 不可变的量。 以全大写命名。 并不是不能对其修改,而是大家都规定好凡是全大写的变量都称之为常量,不可对其进行修改。 #常量:(本质上还是变量 在python中不会有任何机制限制你不能修改常量 但凡是大写默认为常量 不能进行修改 SCHOOL='合肥学院’ print(SCHOOL) 六、#用户与程序交互: 输入: Python3 input() name=input("请输入姓名") input 中输入的任何数据都是字符串 Python2 输出: print() 七:注释 单行注释 # 快捷键:ctrl+/ 多行注释 三引号'''''' 快捷键:'''+回车 #int 基本数据类型: 数字类型: 1.整型:int 人的年龄、身份ID号...sole:8 2.浮点型: float 人的身高体重、薪资 #int age = int(18)14 pr int (age) pr int (type(age)) age2 = print (age2) print (type(age2)) # float22 sal = 1.0123 pr int (sal) pr int (type(sal)) 代先掌握的操作: 1、按索引取値(正向取+反向取) : 只能取 2、切片(頤斗不顕尾,歩长) 3、长度len 4、成员运算in和not in 5、移除空白strip 6、切分split 7、循环1.素引取値(正向取+反向取) :只能取#正向取 str1 = 'hello tank!' print(str1[0]) # h print(str1[9]) # k #反向取 print(str1[-2]) # k2.切片(顾头不顾尾,步长)str1='hello tank!'#0-(5-1)print (str[0:5] #hello#步长print(str1[0:11] ) #hello tank!print(str1[0:11:2] ) #hlotn隔两个读一个3.长度lenprint(len(str1)) #114.成员运算in 和not inprint('h' in str1) #Tureprint('h' not in str1) #False5.移除空白Strip,lStrip,rStrip#会移除字符串中左右两边的空格str1=' hello tank!'print(str1)str1=' hello tank! ‘print(str1)print(str1.strip())#去除指定字符串str2='!tank'print(str2.strip('!'))6.切分splitstr1='hello tank!'#根据str1内的空格进行切分#切分出来的值会存放在[]列表中print(str1.split('')) #['hello','tank']7.循环#对str1字符中进行遍历,打印每一个字符for line in str1:print(line)字符串格式化输出str1='话费扣除%s,还剩%d:'%("一百",50)print(str1)#######字符串类型############1.Strip,lStrip,rStrip移除空白#会移除字符串中左右两边的空格str1=' hello wuyuefeng 'print(str1)print(srt1.strip())#移除字符串中左右两边的空格print(srt1.lstrip())#移除字符串中左边的空格print(srt1.rstrip())#移除字符串中右边的空格2.lower upperstr1='hello wuyuefeng'print(str1.lower())#转换成小写print(str1.upper())#转换成大写3.startswith endswithstr1='hello wuyuefeng'print(str1.startswith('hello')#判断str1字符开头是否是helloprint(str1.endswith('wuyuefeng')#判断str1字符结尾是否是wuyuefeng4.format (格式化输出)的三种玩法str1='my name is %s,my age is %s!'%('tank',18)print str1方式一 根据位置顺序格式化print('my name is {},my age is {}!'.format('tank',18))方式二 根据索引格式化print('my name is {0},my age is {1}!'.format('tank',18))方式三 指明道姓格式化print('my name is {name},my age is {age}!'.format(name='tank',age=18))#5.split切分str1='hello tank!'#根据str1内的空格进行切分#切分出来的值会存放在[]列表中print(str1.split('')) #['hello','tank']#6、join 字符串拼接#根据空格 将列表中的每一个字符串进行拼接print(' '.join(['tank','18','from GZ']))#根据_ 把列表中的每一个字符串进行拼接print('_'.join(['tank','18','from GZ']))#7.replace 字符串替换str1='my name is wangwei,my age is 73!'print(str1)str2=str1.replace('wangwei','sb')print(str2)#8.isdigit 判断字符串是否是数字choice=input('请选择功能[0,1,2]:')#判断用户输入的选择是否是数字print(choice.isdigit())
转载于:https://www.cnblogs.com/Corbett/p/11087298.html