变量:‘容器’

python是一门弱语言。变量声明的时候对数据类型不是很严格。

例:python:a=100

格式:变量名 = 值

一、怎么起名?

变量名的命名规范:

  •  字母、数字、下划线,其他特殊符号不行。
  • 严格区分大小写
  • 不能使用关键字
  • 数字不能开头

起名要做到见名知义

驼峰式命名:

  • 小驼峰:开头第一个单词全部小写,例如:getNameByLine

              不过基本上使用下划线:get_name_by_line

  • 大驼峰:每个单词的首字母大写,例如:FirstName  (python面向对象部分会经常使用)

 关键字有哪些

false       none       ture       and         assert       as            break      class     continue    

def          elif          else     except       finally      for            from      global 

if           import        in           is         lambad    nonlocal     not     or  

pass        raise       return      try          while        with        yield

 

二、有哪些数据类型

变量的类型:python里为了应对不同的业务需求,也把数据分为不同的类型。

number(数字)

int(有符号整型)

 

log(长整型[也可以代表八进制和十六进制])

 

float(浮点型)

 

complex(复数)

布尔类型

true

 

false

string(字符串)

 

list(列表)

 

tuple(元组)

 

dictionary(字典)

 

 

数据类型:

  • int
  • float
  • string
  • boolean

 

例如:

money = 20  #money 是一个变量,后面的值允许发生变化

type(money)

print()   #print()属于一个内置函数,负责输出结果

想要查看money的类型就要print(type(money))

通过type(变量名) 输出变量类型

 

例子:

①     money = '1万'  #< class 'str' > 代表的就是字符串

print(money)

print(type(money))

 

②    money = 28.9      #< class 'float' > 代表的是浮点型字符串

        print(type(money))

 

 

总结:放在‘  ’里的就是字符串类型,字符串可以是单引号、双引号、三引号

           遇到交叉时,通过单双引号来控制包含关系,如:

                        message = ‘粽子说:“今天天气不错!”’

                        message = “粽子说:'今天天气不错! '”

                        print(message)

Eile Edit View Navigate Code Refactor Run Tools VCS Window 
pythonprojectl 
main.py 
python Project 1 
Help 
%i$Epy x 
v python Projectl 
bianliang.py 
hello-py 
main-py 
10 
11 
Scratches and Consol 
12 
13 
Run: 
hello-py x bianliang.py x 
huawel - 
iphone 
print(huawei 
message = 'E 
message "Efi5i: 
print (message) 
iphone) 
D: C:/Users/zongzi/PycharmProjects/pyth 
GM-ih: 
Process finished with exit code 0

 

保留文字的格式输出,使用三引号

 

Eile Edit View Navigate Code Befactor 
python Projectl 
71 ..y ¯ 
Run 
1001s 
vcs 
Window 
python 
Help 
v python Projectl 
bianliang.py 
hello.py 
main.py 
21 
22 
23 
24 
25 
Scratches and Consol 26 
27 
shi = " 
print(shi) 
Run: 
D:\python\python.exe C:/Users/zongzi/PycharmProject 
Process finished with exit code 6

布尔类型:Ture  False (在python中Ture False 都要大写)

                 主要用于开发中的判断,比如:是否登录成功。

                  < calss 'bool' > 布尔类型

pythonProject1 
bianliang.py 
hello.py 
main.py 
*igwpy 
O Scratches and ConsO 
27 
28 
29 
31 
32 
33 
print (shi) 
istog$o = True 
print(istogin) 
= False 
point (islogin) 
D: \ python\python.exe C:/Users/zongzi/PycharmProjects/p 
True 
False 
Process finished with exit code @

print('hello')

name =  '粽子'

print(name)  #在运行之前称为代码

 

input('请输入用户名:')  #阻塞型函数

print('哈哈哈哈')

print('hello') 
name = 
print(name) 
hello world 
print('hello world') 
name = 
print(name)

Eile Edit View Navigate Code Refactor Run 1001s VCS 
pythonProject1 
v pythonprojectl 
Window 
Help 
pythonProject1 
bianliang.py 
hello.py 
main.py 
32 
33 
34 
35 
36 
Scratches and Consol 37 
38 
39 
print ('hello') 
name 
print (name) 
input('ifitfijÄhåPZ:' 
userName 
print(userName) 
print (type (userName)) 
Run: 
D: C:/Users/zongzi/PycharmProjects/pythonProject1/ 
hello 
<ctass 'str'> 
Process finished with exit code 0

money=input('请输入缴费金额:')

print(money)

print(type(money))

41 
Scratches and Consol 
42 
43 
44 
input(' ' 
money = 
print (money) 
print (type (money)) 
D: C:/Users/zongzi/PycharmProjects 
hello 
zongziya 
<ctass 'str'> 
<ctass 'str'> 
Process finished with exit code O

三、类型转换

 ① str ------>  int

money=input('请输入缴费金额:')

print(money)

print(type(money))

print(int (money)+1000)

 

maln.py 
Scratches and Consol 
43 
44 
46 
input 
money = 
print(money) 
print(type(money)) 
Run: 
D:\python\python.exe C:/Users/zongzi/PycharmProj 
1234 
<class 'str'> 
2234

②int ------> str

money=input('请输入缴费金额:')

print(money)

print(type(money))

print(money+str(1000)) 运行结果如下 

字符串和字符串不能做加法,只能拼接。

bianliang.py 
hello.py 
main.py 
Scratches and Consol 
51 
52 
53 
55 
print (money) 
print (type (money)) 
print (money+str (1000)) 
D:\python\python.exe C: / Users/zongzi/PycharmProjec 
<ctass 'str'> 
12341000

练习:

键盘输入两个整数,输出两个整数的和,输出差

input('输出第一个整数:')

input('输出第二个整数:')

•ythonProject1 
bianliang.py 
hello.py 
ma.n.py 
:xternal Libraries 
64 
65 
68 
69 
Scratches and Consol 
70 
71 
72 
73 
74 
75 
one - input 
two = 
print(one+two) #10050 
print #150 
#150.@ 
print (int(one) -int (two)) #50 
print (float (one) -float (two)) 
D:\python\python.exe 
10050 
150 
150.0 
50 
50.0 
Process finished with exit code @

总结:

以变量名:a 为例

str ------> int   int(a)    但是如果是‘9.9’而且是字符串类型转成int的时候报错了

str------>  float   float(a)

 

int------> str       str(a)

float------> str     str(a)

 

int------> float    float(a)

float------> int    int(a)   float类型中小数点后面的数字被抹掉了

纯float类型转字符串可以转成整型,带小数点的字符串类型,不可以转整型

 

布尔类型:flag = True  #bool------> int    Tures是1  False是0

                 print(int(flag))

                 print(float(flag))

                 print(str(flag))

79 
81 
82 
flag 
True 
#bool-- 
--> int 
TuresÆ1 
print (int (flag)) 
print (float (flag)) 
print (st" (flag)) 
D:\python\python.exe C:/Users/zongzi/PycharmProjects/pythonPl 
1 
1.0 
True 
Process finished with exit code @

思考:能否将a转成bool类型

5 
print (boot (a)) 
print (boot (a)) 
print (boot (a)) 
D:\python\python.exe C:/Users/zongzi/PycharmP 
True 
False 
False 
Process finished with exit code @

总结:变量的值是:0, '  '(空字符串),转换结果是False,其他只要变量有值则为True

posted on 2022-01-04 10:34  粽子吖  阅读(187)  评论(0)    收藏  举报