Python study notes - day1

#Python day1

tags: python


[TOC]
##1.Hellow World program####
1) Hellow World
creates a file called hellow.py in linux.

document content:

#!/usr/bin/env python
print("Hellow World!")

The need to specify the interpreter in linux #!/usr/bin/env python
means to find the environment variable named python in the whole linux system.

Give the file execute permission and use ./hellow.py to execute the file.

Output result:

Hellow World!

<br />
<br />


<br />
<br />

##2. Variables
###1) What are variables?

Variables are used to store information that is referenced and manipulated in a computer program. They also provide a way to label data with descriptive names so that our programs are more clearly understood by readers and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout the program.

###Variable definition rules:

  • Variable Definition Rules
    • Variable names can only be 字母、数字或下划线any combination of
    • The first character of a variable name cannot be a number
    • The variable name you write must have meaning, otherwise you and others will not understand
    • The variable name should not be Chinese or Pinyin, and multiple words should be separated by underscores
    • The following keywords cannot be declared as variable names
      ['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']

###What are constants?

Constant: A quantity that does not change, such as π is 3.14...

Python defines constants, and variable names should be capitalized. For example:PIE = "em"

<br />

###2) Assignment of variables:

name = "byh"
name2 = name

print(name,"-----",name2)

name = "em"
print(name,"-----",name2)

print: output characters

<br/>

Output result:


byh ----- byh
em ----- byh

Process finished with exit code 0


>###为什么第二个name2输出的是 byh,而不是em呢?
* 因为:
    - name2只是通过name找到byh的,name2≠name
    - name = em只是从新定义了name,name2还是通过第一个name变量找到byh

<br/>
>注释

当行注释|“#”标识当行
---------|---------------
多行注释|''' 或 """  都表示多行注释

<br>
<br>

---

<br>
<br>

##3.用户输入
###1)简单用户输入

username = input("username:")
password = input("password:")

print("Hellow",username)


>输入密码时,如果要密码不可见,需要使用`getpass`模块中的getpass方法:

import getpass

username = input("username:")
password = getpass.getpass("password:")

print("Hellow",username)

<br />

###2)用户输入,输出调用变量

>方法一:

name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = int(input("salary:"))
print(type(salary))

info = '''
-------- info of %s--------
Name:%s
Age:%d
Job:%s
Salary:%d
''' %(name,name,age,job,salary)

print(info)


%s|代表string字符串
---|---
%d|代表整数
%f|代表有小数点的数
int()|强制转换类型为integer
str()|强制转换类型为string
print(type(salary))|输出字符串类型

>输出结果:

name:em
age:19
<class 'int'>
job:IT
salary:1234567
<class 'int'>

-------- info of em--------
Name:em
Age:19
Job:IT
Salary:1234567

Process finished with exit code 0

<br/>
>方法二:

#Author:byh
name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = int(input("salary:"))
print(type(salary))

info = '''
-------- info of {_name}--------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
''' .format(_name=name,
_age=age,
_job=job,
_salary=salary)

print(info)


>输出结果:

name:em
age:19
<class 'int'>
job:IT
salary:1234567
<class 'int'>

-------- info of em--------
Name:em
Age:19
Job:IT
Salary:1234567

Process finished with exit code 0


<br/>

>方法三:

#Author:byh
name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = int(input("salary:"))
print(type(salary))

info = '''
-------- info of {0}--------
Name:{0}
Age:{1}
Job:{2}
Salary:{0}
''' .format(name,name,age,job,salary)

print(info)


>输出结果:

name:em
age:19
<class 'int'>
job:IT
salary:1234567
<class 'int'>

-------- info of em--------
Name:em
Age:em
Job:19
Salary:em

Process finished with exit code 0


<br/>
<br/>

---

<br/>
<br/>

##4.表达式if ... else
###场景一 : 用户登陆验证

_username = "byh"
_password = "123"
username = input("username:")
password = input("password:")

if _username == username and _password == password:
print("Welcome user {name} login...".format(name=_username))
else:
print("Invaild username or password!")

elif|如果这个条件不成立,那么下个条件是否成立呢
---|---

>输出结果:

#Verification successful output
username:byh
password:123
Welcome user byh login...

#Authentication failure output
username:em
password:123
Invaild username or password!

###场景二 : 猜年龄游戏

_myage = 22
myage = int(input("myage:"))

if myage == _myage:
print("yes, it is")
elif myage > _myage:
print("should be smaller..")
else:
print("should be more big..")


>输出结果

myage:17
should be more big..

myage:23
should be smaller..

myage:22
yes, it is

<br/>
<br/>

---

<br/>
<br/>

##5.while循环
###1)简单while循环

count = 0
while True:
print("count:",count)
count +=1
if count == 1000:
break

>当这个条件成立时:True(永远为真),如果没有定义 `break`条件结束循环,则会一会循环下去。

<br/>

###2)while循环猜年龄游戏

# Realize that the user can continuously guess the age, up to 3 chances, continue to press the wish key, exit and press "n"

_myage = 22
count = 0

while count < 3:
myage = int(input("myage:"))
if myage == _myage:
print("yes it is.")
break
elif myage > _myage:
print("should be smaller...")
else:
print("should be more big...")
count +=1
if count == 3:
countine_config = input("do you want to keep gussing?")
if countine_config !="n":
count = 0


break|结束当前整个循序
---|---
continue|跳出本次循环,进入下次循环
count|添加计数

>输出结果:

#继续猜:
myage:1
should be more big...
myage:2
should be more big...
myage:3
should be more big...
do you want to keep gussing?
myage:12
should be more big...
myage:22
yes it is.

Process finished with exit code 0

#不猜退出:
myage:1
should be more big...
myage:2
should be more big...
myage:3
should be more big...
do you want to keep gussing?n

Process finished with exit code 0

<br/>
<br/>

---

<br/>
<br/>

##6.for循环
###1)简单for循环

#0,10 is the range, 1 is the increment
for i in range(0,10,1):
print("number",i)

>输出结果:

number 0
number 1
number 2
number 3
number 4
number 5
number 6
number 7
number 8
number 9

Process finished with exit code 0


###2)for循环猜年龄游戏

#Users guess up to 3 times
_myage = 22

for i in range(3):
myage = int(input("myage:"))
if myage == _myage:
print("yes it is.")
break
elif myage > _myage:
print("should be smaller..")
else:
print("should be more big..")

else:
print("you have tried too many times")


>输出结果:

myage:1
should be more big..
myage:2
should be more big..
myage:3
should be more big..
you have tried too many times

Process finished with exit code 0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324823084&siteId=291194637