Directory
1 Python introduction ... 1
1.1 Current Python main application areas: ... 1
2 Python installation ... 1
2.1 Windows installation ... 1
2.2 Linux installation ... 2
3 python Basic Combat ... 3
3.1 Pycharm Use the shortcut keys ... 3
3.2 Print Hello world. 3
3.3 Python interpreter ... 3
3.4-Character encoding ... 4
3.5 Variable Definition Rules ... 4
3.5.1 Define Variables ... 4
3.5.2 Variable Assignment ... 4
3.6-line Comment ... 5
3.7 User Input ... 5
3.7.1 input password is not visible ... 6
1 Introduction to Python
The founder of Python is Guido van Rossum (Guido van Rossum). During the Christmas of 1989, Guido van Rossum to spend time in Amsterdam, determined to develop a new script interpreter, as an inheritance of the ABC language.
1.1 Current Python main application areas:
- Cloud computing: The hottest language in cloud computing, typically using OpenStack
- Web development: A number of excellent web frameworks, many large sites are Python development, Youtube, Dropbox, watercress ... , a typical web framework has Django
- Scientific computing, Artificial intelligence: Typical library numpy, SciPy, matplotlib, Enthought Librarys,pandas
- System operation and maintenance: the necessary language for operation and maintenance personnel
- Finance: Quantitative trading, financial analysis, in the field of financial engineering, Python is not only used, but also used the most, and the importance of the year. Reason: As a dynamic language Python, the language structure is clear and simple, the library is rich, mature and stable, scientific calculation and statistical analysis are very good, production efficiency is much higher than c,c++,java, especially good at strategy backtesting
- Graphic GUI:PYQT, Wxpython,tkinter
2 Python installation 2.1 Windows installation
Download the installation package www.python.org
Install to C English path
To configure environment variables:
Computer---Properties---advanced system settings---Advanced---environment variables---path[note here is a semicolon-separated]
2.2 Linux Installation
Sohu Download HTTP://MIRRORS.SOHU.COM/PYTHON/3.5.1/website Slow of a lump
wget http://mirrors.sohu.com/python/3.5.1/Python-3.5.1.tgz
Tar XF python-3.5.1.tgz
CD Python-3.5.1
./configure
Make && make install
The installation is complete, the default road strength
/usr/local/bin/python3–v
#可以更改系统默认python版本, it's best to make a soft connection to the/usr/bin/to facilitate the designation interpreter
Ln-s/usr/local/bin/python3/usr/bin/
Mv/usr/bin/python/usr/bin/python2.bak
Ln-s/usr/local/bin/python3/usr/bin/python
3 Python Basic Combat 3.1 pycharm used shortcut keys
Ctrl+d copy a line of content
CTRL +? Bulk Comment Multiple lines
3.2 Print Hello World
Pycharm:
Print ("Hello World")
Windows:
C:\users\lenovo>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb, 22:43:06) [MSC v.1600
TEL)] on Win32
Type "Help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> print ("Hello World")
Hello World
You can also edit the. py script onto the Windows terminal
C:\users\lenovo>python "C:\Users\lenovo\Desktop\new 1.py"
Linux :
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Print ("Hello World")
Print ("Hello, World")
3.3 Python Interpreter
Python path can be specified directly
#/usr/bin/python
Specifies the version, which only takes effect after chmod +x, and the default Python test.py only executes the system default Python version.
#/usr/bin/env python
#/usr/bin/env Python3
3.4 Character encoding
python2.x only ASCII is supported by default, Chinese need to declare character set
#!/usr/bin/env python
#-*-Coding:utf-8-*-
3.5 variable definition rules 3.5.1 Defining variables
Variable names can only be any combination of letters, numbers, or underscores
The first character of a variable name cannot be a number
The following keywords cannot be declared as variable names
Character definition to add "" double quotes
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
3.5.2 Variable Assignment
Day1 = 1000 + 500 + 500
Day2 = 3000
DAY3 = 1000
DAY4 = 4000
Total = day1 + day2 + day3 + day4
Print ("Sum:", total)
Total: 10000
Name = "Zhang"
name2 = Name
Print (name,name2)
Zhang Zhang
3.6-line Comment
Single-line Comment by: #
Multi-line Comments:
"""
Three double quotes annotate multiple lines of content
Three double quotes annotate multiple lines of content
"""
‘’’
Three double quotes annotate multiple lines of content
Three double quotes annotate multiple lines of content
‘’’
Bulk Comment:
Mouse Check Ctrl +? Implementing bulk Annotations
3.7 User Input
Example 2:
Name = input ("What is your name?")
Print ("Hello", name)
Results:
What is your Name?sdfas
Hello Sdfas
Example 2:\n line break
Name = input ("What is your name?")
# print ("Hello", name)
Job = input ("Job:")
Hobby = input ("Hobby:")
Print ("My name is", Name, "\nmy job was", job, "\nmy hobby is", hobby)
Results:
What is your Name?zh
Job:it
Hobby:it
My name is en
My job is it
My hobby is lol
Example 3:
- Where%s is represented as a character
- Where%d is represented as an integer
Test value
str = "%s"%"Hello"
num = "%d"%33
Print (str)
Print (num)
Name = input ("Whatis your name?")
# print ("Hello", name)
Age =input ("Age:")
Job = input ("Job:")
Hobby =input ("hobby:")
Print ("My Nameis", name, "\nmy job is", job, "\nmy hobby is", hobby)
info = ""
--------Info of%s--------
Name:%s
Age:%s
Jog:%s
Hobby:%s
--------End---------
"% (Name,name,age,job,hobby)
Print (info)
Result: One of the first name is a title
What is your Name?zh
Age:22
Job:it
Hobby:lol
My name is en
My job is it
My hobby is lol
--------info of en--------
Name:zh
Age:22
Jog:it
Hobby:lol
--------End---------
%d integer problem
age = Int (input ("Ages:"))
info = ""
--------Info---------
Age:%d
--------End--------
"% (age)
Print (info)
Results:
Age: 32
--------Info---------
Age:32
--------End--------
Code:
Name = input ("inputyou Name:")
age = Int (input ("Inputyou Age:"))
Print (Type (age), type (name))
Job = input ("Inputyour job:")
Salary =int (Input ("Input your Salary:"))
info = ""
--------------informathionof%s
Name:%s
Age:%d
Job:%s
Salary:%d
--------------End----------
"% (name,name,age,job,salary)
Print (info)
Results:
Input you Name:zhang
Input you age:25
<class ' int ' > <class ' str ' >
Input your Job:it
Input your salary:8888
--------------Informathion of Zhang
Name:zhang
Age:25
Job:it
salary:8888
--------------End----------
Process finished with exit code 0
3.8 If condition statement
conditional expression, after which the condition line is to be followed by: colon, otherwise syntax error
Username =input ("username:")
Password =input ("Password:")
If username = = "Zhang" and password = = "123456":
Print ("Login SUCCESSD")
Else:print ("username Orpassword is worng")
3.8.1 input password is not visible
The Getpass module needs to be defined and the module can only be tested on the server
#!/usr/bin/env python
Import Getpass
PWD =getpass.getpass ("Password:")
Print (PWD)
Results:
[[Email protected] ~] #python test3.py
Password: The password value entered is not visible here
123456
3.9 Guess Age
Oldboy_age = 39
guess_age = Int (Input ("Age:")) #str--INT
If Guess_age ==oldboy_age:
Print ("Correct!!!")
Elif guess_age >oldboy_age: #else if
Print ("Try smaller ...")
Else
Print ("Try bigger ...")
Big guess.
Age:50
Try smaller ...
Process finished withexit code 0
Guess Small
Age:20
Try bigger ...
Process finished withexit code 0
That's right
age:39
Correct!!!
Process finished withexit code 0
3.10 Writing the Login interface
Enter User name password
Show welcome message after successful authentication
Three-time error after locking
Count = 0
Flag = 1
lock = []
User_pass = []
Username =input (' username: ')
F =open (' Heimingdan.txt ', ' R ')
Lock_file =f.readlines ()
F.close ()
For I in Lock_file:
line = I.strip (' \ n ')
Lock.append (line)
If username in Lock:
Print (' Your user%s is already locked please contact admin '%username ')
Else
While True:
Count + = 1
passwd = input ("Password:")
f = open (' Yonghu.txt ', ' R ')
User_file = F.readlines ()
F.close ()
For I in User_file:
Test = 3-(count)
User_pass = I.strip (). Split ()
If username = = User_pass[0] andpasswd = = User_pass[1]:
Print (' Welcome back to%s '%username)
Flag = True
Break
Else
Print (' Password error Please reenter you also have%s chance '%test)
Continue
If flag is True:
Break
Else
if Count = = 3:
Print (' Your user%s is locked, please contact admin '%username)
Lock_file =open (' heimingdan.txt ', ' a ')
Lock_file.write ('%s\n '%username)
Lock_file.close ()
Break
Error
Username:abc
Password:123
Password error Please re-enter you still have 2 chances
password:1234
Password error Please re-enter you still have 1 chances
password:12345
Password error Please re-enter you still have 0 chances
Your user ABC is locked, please contact the Administrator
Success
Username:oldboy
password:123456
Welcome back, Oldboy.
Process finished withexit code 0
Day1-python Learning