1. Introduction
Python is an interpretive, object-oriented, high-level programming language with dynamic semantics.
Liao Xuefeng Website: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000
Download python:https://www.python.org/downloads/
Interactive Python interpreter: IDLE
Python organizes code snippets by indentation, rather than curly braces in C #. It is generally recommended to use four spaces as indentation.
2. Expressions
Division:
By default, an integer is divided by another integer, and the result is still an integer: 1/2 =0
If one of the numbers is a floating-point number, then the result is a floating point.
1.0/2 =>0.5
1/2.0=>0.5
. =>0.5
Double slash: Use a double slash even if the parameter is a floating-point number, the result is divisible.
1.0//2= "0.0
Power Operation:
2**3=>8
-2**3=>-8
Gets the user input: Input (str) method.
>>> x=input (' x= ')
X=5
>>> y=input (' y= ')
Y=6
>>> Print X*y
30
3.
functions
Power function: Pow (2,3) =>8
Absolute Value: ABS ( -10) =>10
Rounding: Round (0.4) =>0.0 round (0.6) =>1.0
4. Module
Special Modules can be imported using the Import command to enhance functionality.
For example:
Import Math
Math.floor (1.9)
=>1.0
Cmath and plural:
>>> Import Cmath
>>> cmath.sqrt (-1)
1j
Python itself supports complex calculations:
>>> (1+2J) + (3+4j)
(4+6J)
>>> (1+2J)-(3+4J)
( -2-2J)
>>> (1+2J) * (3+4j)
( -5+10J)
5.
Save and execute
Python saved as a file with a. py suffix
6.
Notes
With the # notation, the content on the right of the pound sign is not interpreted.
7.
string
strings can use single or double quotes, and escape characters can be used if the intermediate string contents also include quotation marks.
>>> "Let's say \" Hello world\ ""
' Let\ ' s say "Hello World"
stitching strings using the plus sign:
>>> x= ' a '
>>> y= ' B '
>>> X+y
' AB '
String representations: Str and REPR
- STR: Converts the value into a reasonable string form, which is easy for the user to understand;
- Repr: Converts a value into a reasonable form of a Python expression.
>>> x= "Hello World";
>>> print str (x)
Hello World
>>> print repr (x)
' Hello World '
>>> y=10000l
>>> print str (y)
10000
>>> print repr (y)
10000L
Input and Raw_input
- Input assumes that the user entered a valid Python expression, and the following example only succeeds if the input string is entered.
- Raw_input takes the input as the raw data and then puts it into the string.
In general, we recommend the use of Raw_input.
#使用Input
>>> name=input (' Your name? ')
Your name?chenjing
Traceback (most recent):
File "<pyshell#19>", line 1, in <module>
Name=input (' Your name? ')
File "<string>", line 1, in <module>
Nameerror:name ' chenjing ' is not defined
>>> name=input (' Your name? ')
Your name? ' Chenjing '
>>> print ' Hello ' +name
Hellochenjing
#使用raw_input
>>> name=raw_input (' Your name? ')
Your name?chenjing
>>> print ' Hello ' +name
Hello chenjing
Note that Raw_input is returned as a string, if you want the integer type to be converted using int (). For example:
Birth=int (raw_input (' Your birth '))
if (birth<2000):
print ' before 00 '
Else
print ' after 00 '
Long string
If the longer string needs to cross the line, you can use three quotation marks, and the quotation marks do not have to use escape characters anymore.
>>> str= "Hello World"
Hello ' Cathy '
Hello chenjing ""
>>> Print str
Hello World
Hello ' Cathy '
Hello chenjing
Raw string
The original string starts with R, the original string does not handle the backslash as a special character, and each character entered in the original string is consistently output, although the original string cannot end with a backslash.
C:\Program Files
>>> print R ' C:\newfile '
C:\newfile
String encoding
ASCII encoding is 1 bytes, while Unicode encoding is usually 2 bytes.
UTF-8 Encoding: variable length encoding. Encodes a Unicode character into 1-6 bytes according to a different number size, the commonly used English letter is encoded in 1 bytes, the Chinese character is usually 3 bytes, only the very uncommon characters will be encoded into 4-6 bytes
In computer memory, Unicode encoding is used uniformly, and is converted to UTF-8 encoding when it needs to be saved to the hard disk or when it needs to be transferred.
Python supports ASCII encoding, which converts letters and corresponding numbers to each other through the Ord () and Chr () functions.
Print ord (' A ') >>>65
Print Chr (>>>a)
Python later added support for Unicode, with a Unicode representation of the string represented by the u‘...‘
U ' Test '
Encode (' Utf-8 ') method is required to convert to Utf-8
U ' Test '. Encode (' Utf-8 ')
String formatting
You can also specify whether to complement 0, and the number of decimal digits
If the string contains%, use two percent sign instead of
8. Time and date
Python's built-in datetime module provides a common time type such as Datetime,date,time.
- The date () and time () methods can extract the dates and times separately;
- The Strftime () method is used to format the time as a string;
- The Strptime () method is used to convert a string to a date format;
- Subtract two times to get the delta type time difference.
#-*-coding:cp936-*-
From datetime import Datetime,date,time
Dt=datetime (2016,5,23,14,40,0)
Print (Dt.day) #23
Print (Dt.hour) #14
Print (Dt.date ()) #2016-05-23
Print (Dt.time ()) #14:40:00
#格式化字符串
Print (Dt.strftime ("%y-%m-%d%h:%m")) #2016-05-23 14:40
#字符串转换为时间
Dt=datetime.strptime (' 201605231440 ', '%y%m%d%h%m ')
Print (DT) #2016-05-23 14:40:00
#时间差
Dt1=datetime (2016,5,23,14,40)
Dt2=datetime (2016,5,1)
Delta=dt1-dt2
Print (Type delta) #<type ' Datetime.timedelta ' >
Print (delta) #22 days, 14:40:00
Print (Dt+delta) #2016-06-15 05:20:00
Python "1"-Basics