Py-day1 simple use method and syntax use details, py-day1 use Details
1. Getting started with python
1. standard python format
Create a python file ending with. py
For example, vi hello. py
#! /Usr/bin/env python
#-*-Coding: UTF-8-*-# Chinese characters can be used by default for Versions later than python3.5.
Print "Hello Word"
Run:
Python hello. py
Or add the execution permission: chmod 755 hello. py.
./Hello. py
2. Variables
Name = "liudong"
Name2 = name
Then name = "liuyansheng"
# Name2 remains unchanged because it is re-created once modified. Name2 will open up a new memory space.
The set is different:
Name = ['Liu ', 'any', 'sheng',]
Name2 = name
# Name2 changes because different elements in the set and string are not continuous. Adding an element in name will not open up a new space, and name2 will change.
3. Input
Name = raw_input ("Enter the User name :")
# Print the output content
Print name
To hide a password, use the getpass method in the getpass module, that is:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Liudong
Import getpass
# Assign the value of user input to the name variable
Pwd = getpass. getpass ("enter the password :")
# Print the input content
Print name
4. Process Control
(1) user identity authentication:
Syntax: if... else if... elif... else
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Liudong
Import getpass
Name = raaw_input ("Enter the User name :")
Pwd = raw_input ("enter the password :")
If name = "liudong" and pwd = "123456 ":
Print "Login successful"
Else:
Print "Logon Failed"
(2) Guess age:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Liudong
Liudong = 24
Guess_age = int (input ("guess age :"))
If guess_age = liudong:
Print ("yes ")
Elif guess_age> liudong:
Print ("smaller ....")
Else:
Print ("bigger ....")
Loop Three times, right click to exit:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Liudong
Liudong = 24
Count = 0
While True:
If count = 3:
Break
Guess_age = int (input ("guess age :"))
If guess_age = liudong:
Print ("yes ")
Break
Elif guess_age> liudong:
Print ("smaller ....")
Else:
Print ("bigger ....")
Count + = 1
A maximum of three rounds can be guessed. The system automatically exits. The system prompts "fuck off" by mistake ":
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Liudong
Liudong = 24
Count = 0
While count <3
Guess_age = int (input ("guess age :"))
If guess_age = liudong:
Print ("yes ")
Break
Elif guess_age> liudong:
Print ("smaller ....")
Else:
Print ("bigger ....")
Count + = 1
Else:
Print ("fuck off ")
Note: continue jumps out of the current loop, and break jumps out of the entire loop.
5. Flag Space