从零开始的Linux运维屌丝之路,资源免费分享平台   运维人员首选:简单、易用、高效、安全、稳定、社区活跃的开源软件
  • 首页
  • Python
  • 3、Python变量、交互、注释、数据类型

3、Python变量、交互、注释、数据类型

发布:蔺要红03-01分类: Python

一、Python快速入门:变量、交互输入输出、字符编码、注释、数据类型、

1、第一句python代码
>>> print("hello word")
hello word
#写到文件里用python执行
[root@test01 ~]# cat hello.py 
#!/usr/bin/env python
print("hello word!")
[root@test01 ~]# python hello.py 
hello word!

2、python变量的定义规则
变量用来存储信息,在后面可以被程序调用,并且标识名称和类型。主要的功能,命名,并且存储至内存。
变量名只能是 字母、数字或下划线的任意组合
变量名的第一个字符不能是数字
变量名规范:linyaohong_of_passwd(下划线)
变量名不应该为:中文,拼音,过长,词不达意
以下关键字不能声明为变量名:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 
'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 
'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
变量小试牛刀以及调用:
>>> age_of_lyh = 22
>>> print (age_of_lyh)
22
>>> age_of_dql = 21
>>> age_of_dql
21
>>> age_of_dql+age_of_lyh
43
>>> a = 10
>>> b = a
>>> b
10
>>> a
10
>>> a = 3
>>> b
10  #a重新赋值了3,b还是10

如图:

在Python中没有一个专门的语法代表常量,程序员约定成俗用变量名全部大写代表常量


3、用户交互和注释
Python的注释:
#单行注释,可以用中文,英文、
#!/usr/bin/python

#多行注释每行最多不能超过80个字符
''' 
print("Hello",username,password)
print("Hello",username,password)
print("Hello",username,password)
'''
最简单的输入输出
[root@test01 ~]# cat input.py 
#!/usr/bin/python
name = input("what's you name:")
print("Hello", name )
[root@test01 ~]# python input.py 
what's you name:linyaohong
Hello linyaohong
[root@test01 ~]# cat login.py 
#!/usr/bin/python
username = input("username:")
password = input("password:")
print("Hello",username,password)
[root@test01 ~]# python login.py 
username:linyaohong
password:123456
Hello linyaohong 123456

输入密码时,如果想要不可见,可以利用getpass模块中的getpass方法,但是在Pycharm下不支持此模块

[root@test01 ~]# cat login2.py 
#!/usr/bin/env python
# Author:linyaohong

#导入getpass库
import getpass
#将用户输入的内容赋值给name变量
name = input("请输入用户名:")
pwd = getpass.getpass("请输入密码:")
#打印输入的内容
print("username:",name)
print("password:",pwd)
[root@test01 ~]# python login2.py 
请输入用户名:linyaohong
请输入密码:
username: linyaohong
password: 123456

4、数据类型

integer整型int
long 长整型  python3没有long 只有int
str : string字符串


看字符串类型用:type(变量)

>>> name = "lin"
>>> age = 22
>>> a = 1.2
<class 'str'>  #字符串
>>> type(age)
<class 'int'>  #int整数
>>> type(a)
<class 'float'> #浮点

4.1、数据类型-数字

>>> age = 22
>>> type(age)
<class 'int'>
>>> name = 'linyaohong'
>>> type(name)
<class 'str'>
>>> age = 10
>>> age2 = 11
>>> age+age2
21
 
4.2、数据类型-字符串
在Python中,加了引号的字符串都被认为是字符串! 
用法包含 单引号  ’ 双引号: "   多引号 : '''
>>> name = 'lin'    #字符串
>>> age = '22'      #字符串
>>> age2 = 22       #int
>>> type(lin)
<class 'str'>
>>> type(age)
<class 'str'>
>>> type(age2)
<class 'int'>
单引号和双引号默认情况下没有区别,特殊情况下需要配合使用、被定义的字符串中本身含有 '
>>> msg = "my name is lin ,I'm 22 years old!"
>>> type(msg)
<class 'str'>
多引号:多行必须使用多引号、比如一段话就需要多引号
[root@test01 ~]# cat msg.py 
#!/usr/bin/python
msg = '''linux 
nginx'is "web" server
centos'''
print(msg)
[root@test01 ~]# python msg.py 
linux 
nginx'is "web" server
centos
name没有定义变量所以会报错!!!没有加引号的字符串被认为是变量
>>> name = lin
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'lin' is not defined
>>> lin = 'linyaohong'
>>> name = lin
字符串拼接
>>> name = 'lin'
>>> age = 2
>>> name + age  #字符串只能和字符串拼接、否则报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
>>> age = '2'  #把age定义变量为字符串
>>> name + age   #没有报错,进行拼接
'lin2'

>>> name * age  #字符串只能和数字相乘 否则报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
>>> name*10  #字符串相乘、就是把自己拼接多少次
'linlinlinlinlinlinlinlinlinlin'

字符不可变
 
>>> a = 'Lin' #字符串不可变、
>>> id(a)
140058856024640
>>> a = 'yao' 
>>> id(a)
140058856024864
#通过id可以看出重新赋值a,而且字符串也是3个、并没有把之前的a覆盖掉,而且新分配的内存空间
#原来的a = ‘lin’ python解释器会定时清空掉
4.3、数据类型-布尔类型
>>> a = 10
>>> b = 15
>>> a > b
False
>>> a<b
True
###类似这样,以后会天天用##########
if a>b 
	print (a is bigger than b)
else
	print (a is smaller than b)
#######################################
4.4、数据类型-浮点数和科学计数法
 
浮点数 有限或无线循环的小数,但并不包含无理数(π..等),
       一个浮点数肯定是小数
       但小数不一定是浮点数(π)
复数 :略 (5+4j)



科学计数法
>>> 1399
1399
>>> 1.399e3
1399.0
#
>>> 13.99e2  #取13不对、科学计数法:这个取值应该a:  1<= a <10
1399.0
-----------------------------------------------------------
>>> 23
23
>>> 2.3e1
23.0
数精度问题(了解即可)
>>> 1.45645645645645646545645645645646
1.4564564564564564  #只能获取到一定的位数
#如果想获取高精度借助decimal模块的“getcontext”和“Decimal”方法(了解即可没什么卵用)
温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,如有侵权我会在24小时之内删除!

欢迎使用手机扫描访问本站