###首先当然是我写出了我本人的第一个python程序!!hello,world!!这是一个新的开始!
#!/usr/bin/env python3
print("hello,world")
要声明解释器具体位置,由于python官方已经不推荐使用python2(只支持到2022年),所以用python3
定义字符变量时需要加引号“”引起来,数字则不用,python会认为被引号引起来的都为字符变量
所有的变量名不能随便起,必须具备标识意义,否则是不专业的程序员
name = "charlie"
age = 20
print(name,age)
另外,定义变量的时候只能是字母、数字或者下划线的任意组合,不可以数字开头的变量,还有特殊字符也不可以设置成变量,因为某些已经成为python的关键字
重点:
#!/usr/bin/env python3
a=1
b=a
print(a,b)
a='2'
print(a,b)
[root@python ~]# ./test.py
1 1
2 1
注意以上两种赋值方式,虽然b等于a,但是只是意味着b等于a的值(变量值),这时内存中已经记录了b的地址,在下一次a的值发生改变时,b并不会随着a的改变而改变,因为只是b=a的“变量值”,在输出ab的时候,a的值发生改变,在内存中重新修改以及占用a的值然后输出,但是此时b的值依旧存在于内存中,是不受a的值的修改而影响的
字符编码:
unicode:人称统一码,或者单一码,是国际xxx组织(名字忘了)为了解决各个国家指定的自己的编码而带来的种种问题从而诞生的一个统一的国际编码,也是为了解决传统编码而出现的局限性,为每个字符设定了统一并且唯一的二进制编码,规定所有的字符和符号最少由(2个字符)组成。
UTF-8:是针对unicode而进行压缩和优化,它不在使用最少2个字符,而是将所有字符号进行分类:
ascii码中的内容用1个字节保存、欧洲字符使用2个字节保存、东亚的字符用3个字节保存
用户输入:
user_input=input("please input your name:")
print("your name is:",user_input)
(比C语言方便多了!!!给python点赞!!)
注:以下为多行注释
'''
Informationforyour:
Nameis:%s
Ageis:%s
Sexis:%s
Tallis:%s
'''
文本格式化输出,将需要输出的值按照用户的输入而输出
name=input("pleaseinputyourname:")
age=input("pleaseinputyourage:")
sex=input("pleaseinputyoursex:")
tall=input("pleaseinputyourtall:")
msg='''
Informationforyour:
Nameis:%s
Ageis:%s
Sexis:%s
Tallis:%s
'''%(name,age,sex,tall)
print(msg)
注:在python3.0里,默认接收的变量值都是字符型,不是数字,
定义变量时有时要注意类型转换,比如字符型转换成整型.
age = int(input("please input your age : "))
在python环境中调用模块,比如在Linux环境调用shell脚本,亦或者输入敏感信息时密文显示
import getpass
password = getpass.getpass("please input your password")
print(password)
[root@python ~]# python3 test.py
please input your password:
123
[root@python ~]# cat test.py
import os
os.system('df -ThP')
[root@python ~]# python3 test.py
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 16G 1.5G 15G 10% /
devtmpfs devtmpfs 983M 0 983M 0% /dev
tmpfs tmpfs 993M 0 993M 0% /dev/shm
tmpfs tmpfs 993M 8.6M 985M 1% /run
tmpfs tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 xfs 197M 95M 103M 48% /boot
tmpfs tmpfs 199M 0 199M 0% /run/user/0
[root@python ~]#
vim shell.py
import os
res_res = os.system('df -ThP')
print(res_res)
res_res1 = os.popen('df').read()
print(res_res1)
[root@python ~]# python3 shell.py
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 16G 1.6G 15G 10% /
devtmpfs devtmpfs 983M 0 983M 0% /dev
tmpfs tmpfs 993M 0 993M 0% /dev/shm
tmpfs tmpfs 993M 8.6M 985M 1% /run
tmpfs tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 xfs 197M 95M 103M 48% /boot
/dev/sr0 iso9660 3.8G 3.8G 0 100% /mnt
tmpfs tmpfs 199M 0 199M 0% /run/user/0
0
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/rhel-root 16558080 1619660 14938420 10% /
devtmpfs 1006284 0 1006284 0% /dev
tmpfs 1016760 0 1016760 0% /dev/shm
tmpfs 1016760 8772 1007988 1% /run
tmpfs 1016760 0 1016760 0% /sys/fs/cgroup
/dev/sda1 201388 96332 105056 48% /boot
/dev/sr0 3947824 3947824 0 100% /mnt
tmpfs 203356 0 203356 0% /run/user/0
[root@python ~]#
调用shell脚本,需要引入Python模块import os
但是如果想把输出的shell结果赋值到变量,再通过变量输出输出出来的话,就需要使用os.popen(‘xxx’).read()
否则os.system赋值给变量的时候,变量保存的值只是shell脚本(命令)执行的结果(0位成功,1位失败)
如果需要自定义一个Python,然后随处编写Python脚本的时候随时调用,有可能会出现模块路径的问题,这时就需要引入sys模块
import sys
print(sys.path)
[root@python ~]# python3 shell.py
['/root', '/usr/local/lib/python35.zip', '/usr/local/lib/python3.5', '/usr/local/lib/python3.5/plat-linux', '/usr/local/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/site-packages']
[root@python ~]#
将模块放在/usr/local/lib/python3.5/site-packages下,在import xxx(module name)时就可以理解调用
小作业:判断年龄输入是否正确,如果错误,错误三次询问用户是否再次输入,累次输入10次将退出本程序
age = 22
count = 0
for i in range(10):
print("count number is ",count)
print("I number is ",i)
if count<3:
myage = int(input("Please input your age "))
if myage == age:
print("you are right")
break
elif myage > age:
print("It is too big!!!")
else:
print("It is too small!!")
else:
continue_confirm = input("Could you try to input your age? Please input 'yes' or 'no' ")
if continue_confirm == "yes":
count = 0
continue
else:
print("bye bye!!!")
break
count+=1
如果count=0会发现当input yes时,每次输入密码只能输入两次就会再次提示,那是因为当if=yes,会跳出循环接着网卡执行了count+=1,解决办法:要么count=-1,或者使用continue(continue是跳出本次循环而不是整个loop(继续第四次循环)就不会往下执行了,也就是不会执行count+=1)
现在执行结果如下:
[root@python ~]# python3 login3.py
count number is 0
I number is 0
Please input your age 1
It is too small!!
count number is 1
I number is 1
Please input your age 2
It is too small!!
count number is 2
I number is 2
Please input your age 3
It is too small!!
count number is 3
I number is 3
Could you try to input your age? Please input 'yes' or 'no' yes
count number is 0
I number is 4
Please input your age 1
It is too small!!
count number is 1
I number is 5
Please input your age 2
It is too small!!
count number is 2
I number is 6
Please input your age 3
It is too small!!
count number is 3
I number is 7
Could you try to input your age? Please input 'yes' or 'no' yes
count number is 0
I number is 8
Please input your age 1
It is too small!!
count number is 1
I number is 9
Please input your age 2
It is too small!!
[root@python ~]#
[root@python ~]# vim login3.py
[root@python ~]#
[root@python ~]# python3 login3.py
count number is 0
I number is 0
Please input your age : 1
It is too small!!
count number is 1
I number is 1
Please input your age : 2
It is too small!!
count number is 2
I number is 2
Please input your age : 22
you are right
[root@python ~]# vim login3.py
[root@python ~]#
[root@python ~]# python3 login3.py
count number is 0
I number is 0
Please input your age : 1
It is too small!!
count number is 1
I number is 1
Please input your age : 2
It is too small!!
count number is 2
I number is 2
Please input your age : 3
It is too small!!
count number is 3
I number is 3