Python-day1

1. 首先当然是我写出了我本人的第一个 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 的值的修改而影响的

2. 字符编码

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)

注:在 python 3.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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值