Python-day1

7583 ワード

###まずもちろん私が本人の最初の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

なお、以上の2つの付与方式は、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)

注意:python 3.0では、デフォルトで受信変数の値はすべて文字型で、数字ではありません.変数を定義するときは、文字型が整数型に変換されるなど、タイプ変換に注意することがあります.
age = int(input("please input your age : "))

Linux環境でshellスクリプトを呼び出すなどpython環境でモジュールを呼び出すか、機密情報を入力するときに密文表示
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’).そうでなければ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/python 3に配置します.5/site-packagesではimport xxx(module name)時に呼び出しが理解できます
小作業:年齢入力が正しいかどうかを判断し、エラーが発生した場合、エラーが3回ユーザーに再度入力するかどうかを尋ね、複数回入力すると本プログラムを終了する
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の場合、パスワードを入力するたびに2回しか入力できないことが判明します.if=yesの場合、ループが飛び出し、NICがcount+=1を実行するため、解決策はcount=-1を使用するか、continue(continueはloop全体ではなく今回のループをスキップする(4回目のループを継続する)ことで実行されません.つまり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