Python Learning notes 2--python file types, variables, values, strings, tuples, lists, dictionaries
One, Python file type
1. Source Code
Python source code file with a. py extension, interpreted by the Pyton program, does not require compilation
[[email protected] day01]# vim 1.py#!/usr/bin/python print ' Hello world! ' [[email protected] day01]# python 1.pyhello world!
2, byte code
Python source files are compiled with a file extension of ' PYc '
Compile method:
Import Py_compile
Py_compile.compile (' hello.py ')
[Email protected] amos]$ vim 2.py#!/usr/bin/pytonimport py_compilepy_compile.compile ('./1.py ') [[email protected] day01]# vim 2.py#!/usr/bin/pythonimport py_compilepy_compile.compile (' 1.py ') [[email protected] day01]# python 2.py [[ Email protected] day01]# ls1.py 1.pyc 2.py[[email protected] day01]# python 1.pyc Hello world!
3. Optimized code
Optimized source file with "pyo" extension
-python-o-M Py_compile hello.py
[Email protected] day01]# Python-o-M py_compile 1.py[[email protected] day01]# lltotal 16-rw-r--r--. 1 root root 17:55 1.py-rw-r--r--. 1 root root 113 Feb 17:56 1.pyc-rw-r--r--. 1 root root 113 Feb 18:15 1.pyo-rw-r--r--. 1 root root 17:56 2.py[[email protected] day01]# python 1.pyohello world!
Second, Python variables
A variable is an area of computer memory in which variables can store values within a specified range, and values can be changed.
The python under variable is a reference to a data
Python is another area of memory, and C is a re-assignment of the value of an area of memory
Name of the variable
Variable names consist of letters, numbers, underscores
Cannot start with a number
You cannot use keywords
-A A1 _a
Assigning values to variables
Is the process of declaring and defining variables
A = 1
ID (a)//after discovering the new assignment A, the variable a in-memory address changes from 7601952 to 16579968
[[email protected] day01]# ipythonpython 2.6.6 (r266:84292, jul 23 2015, 15:22:56) Type "copyright", "credits" or "License" for more information. Ipython 1.2.1 -- an enhanced interactive python.? -> introduction and overview of ipython ' s features.%quickref -> quick reference.help -> python ' S own help system.object? -> details about ' object ', use ' object?? ' for extra details. In [1]: a=123in [2]: print a123in [3]: id (a) Out[3]: 7601952In [4] : id (a) out[4]: 7601952in [5]: a = 456in [6]: id (a) Out[6]: 16579968
Iii. operators and expressions in Python
The Python operators include
Assignment operator x = 3,y = ' ABCD ' *=/=%= x+=2 x=x+2 x-=2 x=x-2
Arithmetic operator +-*/%//* *
Relational operators > < >= <= = = = = Return result is bool value TRUE or FALSE
Logical operator and logic with: TRUE and false or logical or FALSE or True not logical not true
Defining a variable does not need to declare the character type
in [+]: x=2in [+]: type (x) out[18]: Intin [+]: x= ' David ' in []: Type (x) out[20]: str
The arithmetic operator is simple and straightforward. 4.00//3=1.0 for rounding, 3**2=9 for 3 squared
In [£ º]: ' A ' + ' B ' out[29]: ' AB ' in [+]: 3-4out[30]: -1in []: 3*2out[31]: 6In [1In]: 4/3out[32]: []: 4.0/3out[33]: 1 .3333333333333333In []: 4.0//3OUT[34]: 1.0In []: 4.00//3OUT[35]: 1.0In []: 4%3out[36]: 1In [PNS]: 2**3out[37]: 8I n []: 3**2out[38]: 9
Relational operators
In [all]: 1>2out[39]: Falsein [MAX]: 1<9out[40]: Truein [+]: 1!=9out[41]: True
logical operators
In [all]: 1==1 and 2>1out[42]: Truein []: 1==1 and 2<1out[43]: Falsein []: 1==1 or 2<1out[44]: Truein [+]: No T 1==2out[45]: True
LAMBDA (top-down, higher priority, higher priority on the right side of the peer)
Logical operation: OR
Logical operations: and
Logical operation: not
Member Test: In,not in
Identity test: Is,is not
Comparison: <,<=,>,=>,!==,==
Bitwise OR: |
Bitwise XOR: ^
Bitwise AND: &
Shift:<<,>>
Addition and subtraction: +,-
Multiplication and division and remainder: *,/,%
Positive and negative number: +x,-x
Bitwise ROLLOVER: ~X
Index: * *
Example: writing a arithmetic device
Require a number to be read from the keyboard
[[email protected] day01]# vim 3.py#!/usr/bin/pythonnum1=input (' Please input a number ') num2=input (' please input a number ') print "%s+%s=%s"% (num1,num2,num1+num2) print "%s-%s=%s"% (num1,num2,num1-num2) print "%s*%s=%s"% (num1,num2,num1* num2) print "%s/%s=%s"% (num1,num2,num1/num2) [email protected]st day01]# python 3.py Please input a number2please input a Number32+3=52-3=-12*3=62/3=0
Input () and raw_input () go:
Input () accepts numbers and strings
raw_input () resolves all to a string
in [47]: input ("please input: ") please input: 123out[47]: 123in [48]: input ("please input: ") please input: abc #这里报错, input string must be added ' ABC ' quotation marks, otherwise not recognized---------------------------------------------------------------------------nameerror Traceback (most recent call last) <ipython-input-48-ae0272fccd43> in <module> ()----> 1 input ("please input: ") <string> in <module> () nameerror: name ' ABC ' is not definedIn [49]: input ("please input: ") Please input: ' abc ' out[49]: ' abc ' in [50]: input ("please input: ") Please input: 6789out[50]: 6789in [51]: raw_input ("please input: ") please input: sdfsahfhaout[51]: ' SDFSAHFHA ' in [52]: 234out[52]: 234in [53]: raw_input ("Please input: ") please input: 3242out[53]: ' 3242 ' in [54]: raw_input (" Please input: ") please input: abcout[54]: ' abc '
Iv. python data types
numeric shaping int long shaping long floating-point float (3e+7) complex -3.14j in [4]: Type (3.14j) Out[4]:complex
String three method definition string, str= ' This is a string ' str= "s is a string"
Str= ' This is a string ' ' Triple quotation mark (docstring) can be used as a comment in addition to the ability to define a string
List
Meta-group
Dictionary
Slicing operations on strings:
In [42]: a= ' ABCDE ' in [43]: aout[43]: ' abcde ' in [44]: a[0]out[44]: ' a ' in [45]: a[1]out[45]: ' B ' in [46]: a[4]out[46]: ' E ' in [47]: a[-1]out[47 ]: ' e ' in [48]: a[0]+a[1]out[48]: ' ab ' in [49]: a[0:2] #取值下标注0和1, excluding the last 2out[ 49]: ' ab ' in [50]: a[:2]out[50]: ' ab ' in [51]: a[1:2]out[51]: ' B ' In [52] : a[1:]out[52]: ' bcde ' in [53]: a[:]out[53]: ' ABCDE ' in [54]: a[-1]out[54]: ' E ' in [55]: a[:-1] #从头开始, excluding the last-1 corresponding eout[55]: ' ABCD ' in [56]: a[::1]out[56 ]: ' ABCDE ' in [57]: a[::2] #步进值为2Out [57]: ' Ace ' in [58]: a[::-1] #步进值-1, from the back forward value out[58 ]: ' EDCBA ' in [59]: a[::-2]out[59]: ' ECA ' in [61]: aout[61]: ' ABCDE ' In [ 60]: a[-4:-2]out[60]: ' BC ' in [62]: a[-2:-4:-1] from right to left, -2 d -3c -4b no value,- 1 = right-to-left out[62]: ' DC '
An expression is a formula that joins different data (including variables, functions) with a number of operators to follow certain rules.
Assignment operators
This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1771944
Python Learning notes 2-python file types, variables, values, strings, tuples, lists, dictionaries