Prerequisite: You are ready for the personal PC environment, mine is Ubuntu14.04,python version for 2.7.6 so start the following environment familiarity experiment.
Experiment One: Install Python
sudo apt-get install python
Experiment Two: Enter the interactive Python interpreter and exit
Python
ctrl-d (shortcut key)
Experiment three: Output one line of characters
Python (go to command line interpreter)
Print "hello,world!"
The following experiments are performed by default in the command interpreter
Experiment Four: The interactive command interpreter can be used as a powerful calculator
Addition: + 4 output
Subtraction: 3-2 Output 1
Multiplication: 3*2 Output 6
Division: 3/2 Output 1 2/3 output 0 (here The division result takes only the integer part, there are two ways to do normal division)
Method One: All with real numbers (also called floating point numbers) 1.0/2.0 output 0.5
Method Two: Change the way Division works
From __future__ Import Division
1/2 Output 0.5
1//2 output 0 (in which case the double slash performs an integer division)
Residual: 10%3 Output 1
Power (exponentiation) 2**3 output 8-3**2 output-9
Experiment five: Using variables
X=3
X*2 Output 6
Experiment Six: Get user input
X=input ("x:")
2
Y=input ("Y:")
3
Print X*y
6
Experiment Seven: function use
2**3 Output 8
POW (2,3) output 8
Experiment Eight: module use
In fact, the module can be imagined as an extension to Python to enhance its functionality. You need to import the module using the Import command
Import Math
MATH.SQRT (9) Output 3.0
can also
From math import sqrt
SQRT (9) Output 3.0
The difference between the two look at themselves, should be able to understand
Experiment Ten: Let the script run like a program, not executed in the command interpreter
1. Add in the first line of the document
#!/usr/bin/env python
2. File name ends with a. py
3. Allow scripts to have executable permissions
chmod a+x hello.py (the a+x here is to add executable permissions to everyone)
4. Run the script with the Python command or double-click the script
Python hello.py
Lab 11: String and Escape symbols
It's like print "hello,world!." , a string is printed after print, and the string must be enclosed by double or single quotation marks.
There is no difference between single and double quotes, but sometimes it can come in handy.
For example, to output a string let's go!, in single quotes to express ' Let's go! ' will report a grammatical error, because there are three single quotes, the machine will not be processed, you must use double quotation marks to express "let's go!". Or here with the escape character \ (is the right slash), the escape character, as the name implies, is the original meaning of the character changes, so you can be the letter T after the ' escape, write ' let\ ' s go! '
If the string is long and spans more than one line, you can use three quotation marks instead of normal quotes
Print ""
Fdafdaf.
Fdefwewf
‘‘‘
This way, in fact, can also write,
Print
FDAFDAF \
Fdefwewf
Escaped with escape characters.
Lab 12: Stitching Strings
Way one: A string is written next to another string, and Python automatically merges them, which is a special method that is sometimes especially useful
' Fdafdaf ' 123456 ' output ' fdafdaf123456 '
Way two: Just like the addition operation
' Hello, ' + ' world! ' Output ' hello,world! '
x= ' 123 ' y= ' abc ' XY This output will error, x+y output ' 123abc '
Experiment 13: String representation
STR (), repr (), and ' ' (anti-quote) can convert the value to ask the string.
Like what:
X=42
y= ' abc '
X+y output will be error, two types of data can not be added
STR (x) +y output ' 42abc '
In fact, just said the escape character, but sometimes this character is not very popular
Like what:
print ' hello,\nworld! ' Will output
Hello
world!
What is this for? It is mainly due to the output of \ n as a newline character, in order to avoid this situation can be so, again to escape
print ' hello,\\nworld! ' Output ' hello,\nworld! '
I can do that.
Print R ' hello,\nworld! ' Also output ' hello,\nworld! '
This is called the original string, and it does not treat the backslash as a special character.
There is also a type of Unicode string (or Unicode object), where ordinary strings in Python are stored internally as 8-bit ASCII code, while Unicode objects are stored as 16-bit Unicode characters and can represent more character sets, which means
Print U ' hello,world! '
This article from "ZYZDBK" blog, declined reprint!
Python Environment familiarity