这里就直接跳过安装,python 安装的解释器是python 3.9(虽然是在VS里下载的),写代码用的还是pycharm
正文开始——
一.python的输入函数input()和输出函数 print()
1.输入函数input()
#input()函数
#输入函数默认输入的都是字符串类型
#如果要使用的是数字,则要对输入的内容进行类型转换,如果不进行转换的化,则认为是字符串,对运算符进行字符串的操作
#字符串操作:*n 重复n次 +追加
a=input('')
b=input('')
print(a+b)
print(a*5)
a=int(a)
b=int(b)
print(a+b)
print(a*b)
这里input()函数要注意,输入的内容会被默认为字符串(这和C语言是不同的,C语言是定义什么,输入什么)。这里浅浅举个例子看看,比如输入10和5
#输入的a为10 #输入的b为5 #未经过类型转换处理 105 #直接把b追加在了a的后面 1010101010 #将a重复了5次输出 #经过类型转换处理 15 #加法运算 50 #乘法运算
2.输出函数print()
#print()函数
#输出字符串,带引号,单引号,双引号,三引号均可
print("hello world")
#输出表达式计算值
print(3+1)
print(3*5)
#如何同行输出,python默认结尾字符为换行,只需要对结尾字符修改即可:end=‘ ’
print("hello")
print("world")
print("hello",end=' ')
print("world")
下面是上面的输出结果
hello world 4 15 hello world hello world
二、转义字符和字符编码
1.转义字符
#转义字符
print("hellonworld")
#换行符’n':newline 光标移至下一行首处
print("hellotworld")
#制表符'b':tab,四个位置为一个表单元,”hell"为一个单元,“o "也是一个表单元
print("hellorworld")
#'r'(return)表示将光标的位置回退到本行的开始位置
print("hellobworld")
#'b'‘backspace’表示回退一个字符
#'':斜杆,''':单引号,'"':双引号 如果要正常输出,其正确前需要加''
#原字符,使得转义字符不起作用 “r" or "R"
print(r"hellonworld")#注意的是,在使用原字符的时候输出的最有一个字符不能是单反斜线 如"print(r'hello'),会报错
看看输出结果
hello world hello world world hellworld hellonworld
2.字符编码
#字符编码 国际标准字符编码 UTF-8
print(chr(0b0010011011101101))
#根据UTF-8输出对应的字符,其中前缀0b表示后面的数据为二进制数 :print(chr(0b))
print(ord('⛭'))
#输出字符对应的十进制数 ;print(ord(‘’))
看看输出结果
⛭ 9965
三、Python定义变量
1.保留字
在定义变量时,变量名不能直接是保留字,如果是会报错
#python保留字,对于变量的命名不可直接使用保留字 import keyword print(keyword.kwlist) #输出python的所有保留字
2.变量定义
字符串定义,需要使用引号,单引号或双引号(和C语言不同的是,python没有字符变量类型)
数字定义不需要使用引号,直接赋值即可
#python定义变量 #python变量命名不同与C语言命名 name='heyuanji-yu' #这其中的变量包括了标识(存储地址),类型(字符串还是数字),值(变量的值)value #这里的name相当与C语言字符数组名,可以看作是指针,name存下的是一个地址,根据地址索引name的值以及其他内容 print(name)#得到name的value print(id(name))#得到name的地址 print(type(name))#得到name的类型 number=100 print(number) print(id(number)) print(type(number))
输出结果如下
heyuanji-yu 1544762138928100 1544756614608 100
四、python的数据类型转换
1.四大数据类型
#python的数据类型
#整数型 int
number0=100
print(number0,type(number0))
print(0b100)#二进制
print(0o100)#八进制
print(0x100)#十六进制
#浮点型 float
#浮点数计数是由于是二进制存储会有点误差
#解决浮点数误差的方法
from decimal import Decimal
print(Decimal('2.2')+Decimal('1.1'))#精确计算
print(1.1+2.2)#存在一定的误差,不准确性
#布尔型 bool "True" "False"
f1=True
f2=False
print(f1,f2,type(f1))
print(f1+0)#true==1
print(f2+0)#false==0
#字符型 str
#字符串定义时可以是单引号,单双引号和三引号;区别在于,三引号赋值可以多行,单引号不能
str1='python0'
str2="""python1
python2"""
print(str1)
print(str2)
看看输出结果
1004 64 256 3.3 3.3000000000000003 True False 1 0 python0 python1 python2
2.数据类型转换
#数据类型转换
#类型转换函数 int(),float(),str()
#str型转int型时,不可转字符串和小数串
str3='1234'
print(int(str3)) #输出1234
#float转int时,截取整数段
float1=108.88
print(int(float1)) #输出108
name='heyuanji-yu'
age=19
print(' I'm '+name+' ,my age is '+str(age)+' years old')
#这里之所以要进行类型转换是因为,int 和str 不能直接链接,这里的'+'表示链接
输出结果如下
1234 108 I'm heyuanji-yu ,my age is 19 years old
五、python的运算符
#python运算符 #算术运算符 +加 -剪 *乘 /除 //整除 %取模运算 **: 次方运算 example:2**3=2*2*2=8 #注意整除时,若是一正一负,向下取整 #比较运算符 > < >= <= != == -->是对value进行比较 is is not -->是对id进行比较 #布尔运算符 (and) (not) (or) 参考离散数学的合取,非,析取 (in) (not in) 通常用来判断某一字符或字符串是否在另一字符串中 #位运算符 (&:按位与 |:按位或 >>:右移 <<:左移)



