Study notes|python (1)

1. Sinicization plug-in (just a small tool, select to translate the selected sentence)

Search A8translate to download

Install and restart pycharm

2. print output function

print is a printing function

print('hello, word')# must be English brackets

print(*values,sep=' ',end='\n')

The three common parameters of the print output function

2.1 values:

①Multiple values ​​can be output. If the value is Chinese or other symbols (single letter), it needs to be surrounded by quotation marks (single and double quotation marks are acceptable), and multiple values ​​are separated by commas

② There can only be one print function in a line

print('Hello') print×

print('welcome','thank you')√

2.2 sep (separte) (interval)

① Used to set the interval symbol between values

② After multiple values

Visible at this time

2.3 end (set to end with ..., the default value is a newline character\n)

print('white ball', end='\n')

print('red ball', end='\n')

print('black ball', end='\n')

to modify

print('white ball', end='{}')

print('red ball', end='{}')

print('black ball', end='\n')

Exception:

If there is \n in the text, it may be recognized as a newline symbol at this time, you need to add an r in front to prevent it from being recognized as a newline symbol

example:

print('Hi\nThere is a newline symbol here')

print(r'Hello\nThere is a newline symbol here')

3. Notes (will not run, for explanation)

①Single-line comment:

#Hello

Comments from the pound sign to the end of the paragraph

② Multi-line comments:

Select the field, then ctrl+/

③Three quotation mark notes:

"""(Comment content) """

4. Variables (save data in memory)

Principle: Define first and quote later

Variable characteristics; can be assigned repeatedly

Note: The data surrounded by quotation marks is a string type, so

打印变量的时候,和赋值的时候不需要加引号

name='hello'

print('name')

name='hello'

print(name)

5. 标识符(变量、函数、类、模块其他对象的名称)

规则:只能是数字、字母、下划线_的任意组合

不能用关键字命名

import keyword

print(keyword.kwlist)

--该部分只是建议前期养成一个好习惯(>^ω^<)--

5.1 见名知意

顾名思义尽量用它本来的意思的英文命名

5.2 下划线分割法(蛇形命名法)

多个单词组成的名字,使用小写、字母、单词之间引用下划线分隔开

蛇形命名法

teacher_name='托尼'

student_name='小王'

5.3 大驼峰命名法

多个单词组成的名词,第一个单词首字母大写,其他的字母小写

TeachName='托尼'

StudentName='小王'

5.4 小驼峰命名法

多个单词组成的名词,第一个单词首字母小写,后面单词的首字母大写,其他字母小写

teachName='托尼'

studentName='小王'

6. 数据类型

如图有两种,我们先进行上面两种数值类型的学习,其他的后期更新

6.1 数值类型

6.1.1 int(integer)整型

int整型——>数学中整数类型

age=18

检测是否为int整数类型

type( ):检测数据类型

print(type(age))

6.1.2 float浮点型

记录带有小数的数据

salary=11111.111

6.1.3 bool值

只记录两种状态真和假(对和错)

不能被随便定义,有固定写法

语法:True(真)False(假)首字母必须大写(否则会报错,没有大写会识别成一个没有定义的变量)

应用场景:一般和判断进行使用

is_ok = True

6.1.4 complex复数(了解)

复数是由实部和虚部组成,在python里复数的虚部以j作为后缀

hh=1+2j

print(type(hh)) #<class 'comlplex'>

zz=2+3j

print((hh+zz) #可以进行简单的计算

(3+5j)

6.2str类型

字符串的作用:用来描述人的名字、姓名等带有描述性质的内容

定义:单引号,双引号、三引号都是字符串

str1=''

str2=""

str3="' '" #三引号除了可以表示注释,还可以变量定义=三引号就是字符串

打印多行的时候就用到三引号(下图可看效果,明显三引号更好用,直接可以换行)

msg="七月份的尾巴"\

"你是狮子座"

print(msg)

msg1="""

七月份的尾巴

你是狮子座

"""

print(msg1)

字符串嵌套

print('hello""tom') #外层用的单引号,内层就使用双引号

print(" I'm tom ") #外层是双引号,内层可以用单引号

print('I'm tom')#此时会报错,因为外层用了单引号,内层一个单引号找不到对应的另一个单引号

解决办法加上\代表取消引号的特殊意义

print('I\'m tom')

字符串只能和字符串进行相加,字符串拼接

print('hello'+'word')

字符串可以相乘 *符号是乘法

print('我想你' *99)字符串乘以99

7.格式化的输出

把字符串里面代码某些内容替换掉之后再输出

7.1 %占位符 符号用来格式话字符串

7.1.1 %s 用字符串替换(可以接收整数和浮点数类型)

需求:打印出一句话 我的家乡在 xxxx 我的名字叫 xxx 芳龄 xx岁

第一步:准备数据

address = '东北'

name = 张

age = 18

第二步输出数据

print('我的家乡在%s 我的名字叫%s 芳龄%s岁)

%s 会按照%s位置对应传递

7.1.2 %d 用整数替换(只接收整数)

num1 =9

num2 =90

num3 =900

print('%02d'%num1) #保持两位,空的一位用0补齐

print('%3d'%num2)#保持三位,空的一位用空格补齐

print('%04d'%num3)#保持三位,空的一位用0补

7.1.3 %f 用浮点数替换

float1 =1.99

print('浮点数%f'%float1)#默认输出会取6位小数

print('浮点数%.3f'%float1)#加 . 后面的数字表示保留几位小数

print('浮点数%.1f'%float1)#默认遵守四舍五入

7.2 f格式化(常用python3.6才推出)

语法:f'{变量}'

id=1

name="tom"

print(f'我的号码是{id},名字叫{name}')

id_card=2.5

print(f'我的卡号为{id_card:.2f})

print(f'我的学号为{id:3d}')

8.算术运算符

作用:完成基本的算术

print(11+5) #+号的作用都是数字中的运算,只限制数值类型和数值类型进行做数学运算 16

print('11'+'5') #字符串的拼接 115

print('11'+5) #会报错

print('5'-'4') #字符串和字符串不支持减法

print(5*5)

print('5'*50) #字符串重复50次

print('5'*'5') #报错,字符串和字符串不支持乘法

print(4/2)

#2.0 在python除法运算得到的结果商一定是浮点数(小数,#除数不能为0,为0报错

注意:python 中的小数是不精确的

取整数的符号 //

print(10//3) #3

取余数的符号 %

print(10%3) #1

验算公式:被除数-(整除结果*除数)=余数

幂次方

print(3**3) #3*3*3

从2020年10月27,拿到上面的2020 10 27

num=20201027

print(num//10000) #2020

print(num//100%100) #10

print(num%100) #27

9.赋值运算符

由算术运算符和赋值符组成

具体为+=,-=,*=,/=

意思就是加了具体的值后再赋值给前面的数

加法复合运算符

age+=1,年龄加一后赋值给age,其他同理

10.input输入函数

输入函数:input

通过input实现接收用户输入的数据

语法:input(prompt=None)

prompt是提示信息,会在控制台展示,默认值为空

user_name=input('提示信息')

print(user_name,type(user_name))

必须等用户输入内容回车才会继续执行print

f格式化输出用户名和密码

user_id=input('请输入你的用户名')

password=input('请输入你的密码')

print(f'你的用户名为{user_id},你的密码为{password}')

11.转义字符

11.1 续行符

\

' '当文本太长时,回车时会自动出现一个\续行符

例外:如果是三引号则不需要用到续行符

11.2 换行符

\n

表示把当前位置移动到下一行的开头

11.3 制表符

键盘上的tab,默认四个空格的距离

使用\t时候如果发现空格数量不对,自己手动调整

11.4 \\ 取消转义

11.5 \" , \' 输出' "

print('i don\'t say ' )

11.6 r

r 原生字符串,取消转义

r ' ' 表示引号内部的字符串默认全部都转义

例题(用到转义和输入)

print('欢迎来到购物商场~')

print('有如下商品供你挑选')

print('-'*50)

print('商品','价格','数量',sep='\t\t')

print('小笼包',15,'\t100',sep='\t')

print('蒸饺',10,20,sep='\t\t')

print('重庆抄手',18,'\t20',sep='\t')

print('-'*50)

id=input('请输入你想购买的商品:')

print('重庆抄手购买成功!欢迎下次光临~')

结果如下

内容很简单,主要是记录下第一次学python =v=

Guess you like

Origin blog.csdn.net/m0_52527037/article/details/129193789