Python Learning 1, python Learning
1. Introduction to python
Introduction to Python on various websites
Python is powerful... and fast;
Plays well with others;
Runs everywhere;
Is friendly & easy to learn;
Is Open.
2. python version differences
Short version: Python 2.x is legacy, Python 3.x is the present and future of the language
Python 2.7 is a compatible version, but Python 2.7 wocould be supported until 2020
Print usage is different:
# python 2.xprint "Hello World!"print >>sys.stderr, "fatal error"
# python 3.xprint("Hello World!")print("fatal error", file=sys.stderr)(first,*middle,last) = range(10)
Python 3.x supports Unicode by default, and Python 2.x supports ASCII by default.
When Python 2.x needs to support Chinese characters:
#/Usr/bin/env python #-*-coding: UTF-8-*-print "Hello! "
Python 3.x supports Chinese characters by default:
#/Usr/bin/env pythonprint ("Hello! ")
Python 3.x name changes for some libraries
Old Name |
New Name |
_ Winreg |
Winreg |
ConfigParser |
Configparser |
Copy_reg |
Copyreg |
Queue |
Queue |
SocketServer |
Socketserver |
Markupbase |
_ Markupbase |
Repr |
Reprlib |
Test. test_support |
Test. support |
So. Later, select python 3.x.
3. Installation
Install in Windows
Download the installation package
https:
/
/
www.python.org
/
downloads
/
Install
Default installation path: C: \ Program Files \ Python35
Add Environment Variables
Right-click the computer and choose Properties> advanced system Settings> advanced> environment variables> path> Add path
Example: Add; C: \ Program Files \ Python35; C: \ Program Files \ Python35 \ Scripts
-
- Download the installation package
Python-3.5.2.tgz
Root @ localhost tmp] # tar zxf Python-3.5.2.tgz [root @ localhost tmp] # cd Python-3.5.2 # default installation in the/usr/loca/directory [root @ localhost Python-3.5.2] #. /configure [root @ localhost Python-3.5.2] # make & make install [root @ localhost Python-3.5.2] # python3.5Python 3.5.2 (default, Aug 13 2016, 19:07:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linuxType "help", "copyright", "credits" or "license" for more information. >>>
4. the first program
Create a hello. py file in the Linux directory and grant permissions to it.
[root@localhost learning]# cat hello.py #!/usr/bin/env pythonprint("Hello World!")[root@localhost learning]# chmod +x hello.py [root@localhost learning]# ./hello.py Hello World!
You can also execute
[root@localhost learning]# pythonPython 3.5.2 (default, Aug 13 2016, 19:07:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> print("Hello World!")Hello World!>>>
5. Variables
VariablesAre used to store information to be referenced and manipulated in a computer program. they also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader 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 your program.
Variable naming rules
Variable names can only contain uppercase/lowercase letters, numbers, and underscores.
The variable name cannot start with a number.
Variable names cannot be the following keywords
['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield ']
>>> Name = "wenchong" >>> print (name) wenchong >>># when the variable name1 is assigned a value of name, it is the value of the variable name (that is, the value in memory) assign a value to name1. If the name changes again, the value of name1 will not be affected >>>> name1 = name >>> id (name) 139659587567344 >>> id (name1) 139659587567344 >>>> name = "Jack" >>> id (name) 139659587549592 >>> id (name1) 139659587567344 >>>> print (name, name1) Jack wenchong
6. user input
1 #!/usr/bin/env python2 # Get User Input String3 name = input("Please Input Name:")4 """5 Python 2.x6 name = raw_input("Please Input Name:")7 """8 print(name)
Row 1st: Specifies the interpreter.
Row 3: Single Row comment
Row 3-7: multi-line comment
Row 3: Enter the content entered by the user
When you enter the password, the password you want to enter is invisible. This module does not take effect in pycharm of windows.
#!/usr/bin/env pythonimport getpasspasswd = getpass.getpass("Please Input Passwd: ")print(passwd)[root@localhost learning]# python hello.py Please Input Passwd: password[root@localhost learning]#
7. Modules
In the python program that captures the input password, one of the actions is import getpass, which is the import module.
OS. mkdir () create a directory
OS. system () is used to execute system commands. The command result cannot be assigned to a variable. Only the return code can be used to assign a value to the variable.
OS. popen () executes the system command. The command result can be assigned to the variable.
# Import module OS >>> import OS >>> OS. mkdir ("testDir") >>> OS. system ("ls-l") total 4-rwxr-xr-x 1 root 238 Aug 13 20:17 hello. pydrwxr-xr-x 2 root 6 Aug 13 20:23 testDir0 >>> command_res = OS. system ("ls-l") total 4-rwxr-xr-x 1 root 238 Aug 13 20:17 hello. pydrwxr-xr-x 2 root 6 Aug 13 20:23 testDir >>> print (command_res) 0 >>> command_res = OS. popen ("ls-l "). read () >>> print (command_res) total 4-rwxr-xr-x 1 root 238 Aug 13 20:17 hello. pydrwxr-xr-x 2 root 6 Aug 13 20:23 testDir
>>> import sys>>> print(sys.path)['', '/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@localhost learning]# cat mymodel.py #!/usr/bin/env pythonmyname = "Test Model"[root@localhost learning]# pythonPython 3.5.2 (default, Aug 13 2016, 19:07:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import mymodel>>> print(mymodel.myname)Test Model>>>
8. Process Control
#! /Usr/bin/env pythonimport getpassusername = "wenchong" passwprd = "pwd123" input_username = input ("Enter the User name:") input_password = getpass. getpass ("Enter password:") if username = input_username and password = input_password: print ("Welcome to heaven... ") else: print (" invalid user name or password ")
#! /Usr/bin/env pythonnumber = 20input_num = int (input ("Please guess a number:") if input_num = number: print ("Congratulations, you guessed it ") elif input_num> number: print ("Please guess a smaller number") else: print ("Please guess a bigger number ")
9. Loop
#! /Usr/bin/env pythonnumber = 20for I in range (10): input_num = int (input ("Please guess a number:") if input_num = number: print ("Congratulations, you guessed it") # If you guessed it, exit the entire loop break elif input_num> number: print ("Please guess a smaller number") else: print ("Please guess a bigger number ")
#! /Usr/bin/env pythonnumber = 20 # define a counter variable count = 0 for I in range (10): # When the counter value is greater than 2, the system prompts whether to continue, if you choose to continue, the counter is reset to 0. Otherwise, the entire loop is exited. if count> 2: user_input = input ("do you want to continue? (Y/n): ") if user_input =" y ": count = 0 continue else: break input_num = int (input (" Please guess a number :")) if input_num = number: print ("Congratulations, you guessed it") break elif input_num> number: print ("guess a smaller number") else: print ("Please guess a bigger number") # execute it once in a loop, that is, to guess a number. Adding 1 # count + = 1 to the counter is equivalent to count = count + 1 count + = 1.