Python Automation Development (2): HelloWorld Program, Variables and Assignment, User Interaction, Conditional Judgment and Indentation, Cyclic Control

Keywords: Python vim shell Pycharm

You can edit py files with vim in the shell, or you can write code with pycharm, with. py as the file name

The simplest code presentation

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time    : 2017/3/13 10:15
# @Author  : eason
# @File    : FirstProgram.py

print("Hello World!")

Meaning of variables: Store temporary data while the program is running

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time    : 2017/3/13 10:15
# @Author  : eason
# @File    : FirstProgram.py


a = 3
b = a
a = 5
print(a,b)

C:\Python27\python.exe D:/worklog/pytools/s12/day1/FirstProgram.py
(5, 3)

Variables = "" must be strings here, otherwise they will be considered variables, such as

a = "alog"

a = b # If b is not defined, an error will be reported

These two are different.

a = 3
b = a
a = 5
print(a,b)

C:\Python27\python.exe D:/worklog/pytools/s12/day1/FirstProgram.py
(5, 3)

In python, it is stored as a pointer, so when b=a, b executes the corresponding data block of A. When a = 5 becomes this, the pointer of a will change, but the pointer of b will not change. In Liao Xuefeng's blog,

It's so analytic.

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001374738264643de15c5c4abad47dd9510e3b86286acb8000

*******************************************************************************************************************

a = 'ABC'
b = a
a = 'XYZ'
print b

Does the last line print out the content of variable b'ABC'or'XYZ'? If we understand it mathematically, we will mistakenly conclude that B is the same as a and should also be'XYZ', but in fact the value of B is'ABC'. Let's execute the code line by line and see what happened.

Execute a ='ABC', the interpreter creates the string'ABC'and variable a, and points a to'ABC':

Executing b = a, the interpreter creates the variable b and points b to the string'ABC':

Execute a ='XYZ', the interpreter creates the string'XYZ', and changes the direction of a to'XYZ', but b does not change:

So, the result of printing variable b is naturally'ABC'.

*******************************************************************************************************************

Principles of variable naming:

1 Explicit, easy to understand
2 nums_of_alex_gf = 10
 3 NumsOfAlexGf = 2 This is called hump writing (first letter capitalized)

Write 2 and 3. Don't use two styles in your code.

Illegal naming rules

1. name-of-alex-gf = 22 can't be used - - in python it's a minus sign
 25 name = Number cannot begin
 3! Name special characters can not be!$^&*
4 name of teacher = no space
5 Some keywords cannot be declared as variables

 

The method to determine whether the memory address has changed, id()

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time    : 2017/3/13 10:15
# @Author  : eason
# @File    : FirstProgram.py
a = 3
print id(a)
b = a
print id(b)
a = 5
print(a,b)
print id(a)
print id(b)



C:\Python27\python.exe D:/worklog/pytools/s12/day1/FirstProgram.py
34318112
34318112
(5, 3)
34318088
34318112

 

In Python 3, input() corresponds to raw_input() of 2.7, and the raw_input() method has been abolished.

 

Judgement sentence

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time    : 2017/3/14 11:04
# @Author  : eason
# @File    : if_ext.py

sex = raw_input("input your gender:")
if sex == "girl":
    print("i wolud like to have a little monkey with tenglan")
elif sex == "man":
    print("Going to homesexual!...")
else:
    print("Pervert")

The results are as follows:

  

C:\Python27\python.exe D:/worklog/pytools/s12/day1/if_ext.py
input your gender:man
Going to homesexual!...

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time    : 2017/3/14 11:23
# @Author  : eason
# @File    : guest_lucknum.py.py
lucky_num = 19

input_num = raw_input("Input the guess num:")

if input_num == lucky_num:
    print("bingo!")
elif input_num > lucky_num:
    print("the real number is smaller.")
else:
    print("the real num is bigger...")

Upgraded version

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time    : 2017/3/14 11:23
# @Author  : eason
# @File    : guest_lucknum.py.py
lucky_num = 19

while True:
    input_num = int(raw_input("Input the guess num:"))

    if input_num == lucky_num:
        print("bingo!")
        break
    elif input_num > lucky_num:
        print("the real number is smaller.")
    else:
        print("the real num is bigger...")

 

The results obtained

C:\Python27\python.exe D:/worklog/pytools/s12/day1/guest_lucknum.py
Input the guess num:20
the real number is smaller.
Input the guess num:30
the real number is smaller.
Input the guess num:10
the real num is bigger...
Input the guess num:5
the real num is bigger...
Input the guess num:19
bingo!

Process finished with exit code 0

If break is not used, then how to deal with it?

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time    : 2017/3/14 11:23
# @Author  : eason
# @File    : guest_lucknum.py.py
lucky_num = 19
input_num = -1
while lucky_num != input_num:
    input_num = int(raw_input("Input the guess num:"))
    if input_num > lucky_num:
        print("the real number is smaller.")
    elif input_num < lucky_num:
        print("the real num is bigger...")

print("Bingo!")

The results are as follows:

C:\Python27\python.exe D:/worklog/pytools/s12/day1/guest_lucknum.py
Input the guess num:20
the real number is smaller.
Input the guess num:30
the real number is smaller.
Input the guess num:10
the real num is bigger...
Input the guess num:19
Bingo!

Process finished with exit code 0

Posted by xentia on Fri, 19 Apr 2019 13:09:34 -0700