1. python file type 1. Source code--parsed directly by Python
vi 1.py #!/usr/bin/pythonprint ‘hello world‘
Here 1.py is the source code
The execution style is similar to shell script:
-
- After Chmod +x,./1.py
-
- Python 1.py
2. Byte code
- Python source files are compiled and generated with the file extension PYc
Compile method:
[[email protected] py]# cat 2.py #!/usr/bin/pythonimport py_compile py_compile.compile(‘1.py‘)
Write a 2.py script, execute, the interface does not output, but look at the file list, more than a 1.PYC
[[email protected] py]# python 2.py [[email protected] py]# ll总用量 12-rw-r--r-- 1 root root 38 12月 20 21:06 1.py-rw-r--r-- 1 root root 112 12月 20 21:10 1.pyc-rw-r--r-- 1 root root 66 12月 20 21:09 2.py
How do you do it? or Python 2.py.
Also, if the source file 1.py is not in, 2.py can still be executed
3. Optimizing the Code
- Optimized source files with extension pyo
Python–o–m Py_compile 1.py
[[email protected] py]# python -O -m py_compile 1.py[[email protected] py]# ls1.py 1.pyc 1.pyo 2.py
After executing the optimized code, generate 1.PYO. Perform 1.pyo
[[email protected] py]# python 1.pyohello world
Variables of 2.python
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
- Name of the variable
- Variable names consist of letters, numbers, and underscores.
- Variable 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) #id显示a在内存的位置号
In [1]: a = 123In [2]: id(a)Out[2]: 25933904In [3]: a = 456 In [4]: id(a)Out[4]: 33594056In [5]: x = ‘abc‘In [6]: x = abc ---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-6-c455442c5ffd> in <module>()----> 1 x = abcNameError: name ‘abc‘ is not defined
The above error explanation, by default:
数字直接写表示数字 数字带引号表示字符串 字符带引号表示字符串 字符不带引号表示变量
Python does not need to declare the type of the variable in advance and automatically determines
In [7]: a = 456In [8]: type(a)Out[8]: int
Type of variable that identifies a is an integer int
In [9]: a = ‘456‘In [10]: type(a)Out[10]: str
Type of variable that identifies a is a string str
- The Python operator includes
1. Assignment operators
=: x = 3, y = ‘abcd’ #等于+=: x += 2 #x=x+2-=: x -= 2 #x=x-2*=: x *= 2 #x=x*2/=: x /= 2 #x=x/2%=: x %= 2 #取余数
2. Arithmetic operators
+-*///%**
Example 1:
In [20]: a = 1 ;b = 2In [21]: a+bOut[21]: 3In [22]: ‘a‘ + ‘b‘Out[22]: ‘ab‘
After AB assignment, A+b is a number. Add two characters directly to a string that fits together
Example 2:
In [24]: 4 / 3Out[24]: 1In [25]: 4.0 / 3Out[25]: 1.3333333333333333
4 directly except 3, because the default is an integer, so the result takes an integer 1
To get a decimal, turn 4 into a floating point number 4.0
Special,//indicates mandatory rounding
In [26]: 4.0 // 3Out[26]: 1.0
Example 3:
In [27]: 2 ** 3Out[27]: 8In [28]: 2 * 3Out[28]: 6
A * is multiply, two * * is a power
3. Relational operators
> : 1 > 2< : 2 < 3>=: 1 >= 1<=: 2 <= 2==: 2 == 2!=: 1 != 2
In [33]: 1 > 2Out[33]: FalseIn [34]: 1 < 2Out[34]: True
Set is true, does not set false
4. Logical operators
and逻辑与: True and Falseor逻辑或: False or Truenot逻辑非: not True
Example:
In [35]: 1 < 2 and 1 >2Out[35]: FalseIn [36]: 1 < 2 or 1 >2Out[36]: TrueIn [37]: not 1 > 2Out[37]: True
Priority order of Operations:
- Input and Raw_input
Input suitable for numbers, raw_input for characters
In [41]: input("input num:")input num:123Out[41]: 123In [42]: input("input num:")input num:abc---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-42-3cd60768312e> in <module>()----> 1 input("input num:")<string> in <module>()NameError: name ‘abc‘ is not definedIn [43]: input("input num:")input num:‘abc‘Out[43]: ‘abc‘In [44]: raw_input("input num:")input num:abcOut[44]: ‘abc‘
There can be seen above in input, direct input ABC error, but raw_input normal display.
This allows you to write a calculation script
!/sur/bin/python
NUM1 = input ("Please input a num:")
num2 = input ("Please input a num:")
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)
%s分别对应后面的数值 执行脚本
[email protected] py]# python cal.py
Please input a num:5
Please input a num:6
5 + 6 = 11
5-6 = 1
5 * 6 = 30
5/6 = 0
### 3.Python的数值和字符串数值类型: - [ ] 整形int 整型int可以存储2^32个数字,范围-2,147,483,648到2147483647 例如:0,100,-100- [ ] 长整型long Long的范围很大,几乎可以说任意大的整数均可以存储。 为了区分普通整型,需要在整数后加L或l。 例如: 2345L,0x34al - [ ] 浮点float 例如:0.0,12.0,-18.8,3e+7等- [ ] 复数型complex Python对复数提供内嵌支持,这是其他大部分软件所没有的。 复数例子:- 3.14j,8.32e-36j - [ ] 字符串 string 有三种方法定义字符串类型 - str = ‘this is a string’ #普通字符串 - str = “this is a string” #能够解析\n等特殊字符 - str = ‘’‘this is a string’‘’ #可以略去\n 三重引号(docstring)除了能定义字符串还可以用作注释。 举例:
In [3]: a = "' Hello"
...: World "
In [4]: A
OUT[4]: ' Hello\nworld '
In [5]: Print a
Hello
World
- 字符串索引,0开始,-1表示最后一个,-2倒数第二个,类推
In [6]: a = ' ABCDE '
In [7]: A[0:2]
OUT[7]: ' AB '
a[]表示取索引指定的字符,[0:2]可以类比数学中的0<=a<2,即0<=a<=1,就是取第一个和第二个,不包括第三个
In [8]: A[:2]
OUT[8]: ' AB '
In [9]: a[:]
OUT[9]: ' ABCDE '
0或者-1可以省略 - 字符串切片
In [all]: A[0:3:2]
OUT[11]: ' AC '
只取a和c,首先应该是a[0:3],但是这样的结果是abc,a[0:3:2]的2表示步进2个,跳过b。同理,如果是a[0:3:3]表示步进3。 **如果要将整个字符串倒过来,需要用-1
In []: A[::-1]
OUT[17]: ' EDCBA '
可以类比下面的
in [+]: a[:-1]
OUT[16]: ' ABCD '
来个更加直观的
In []: A[-2:-4:-1]
OUT[13]: ' DC '
取倒数第2个和倒数第三个,而且倒序显示### 4.作业
Convert "123" to an integer
Convert "9999999999999999999" to grow integer
Convert "3.1415926" to a floating-point number
Convert 123 to a string
The following string exists
String 1: "ABC defgh&IJKL opq mnrst (uvwxyz"
String 2: "Abc#def gh%ij mnopq klrs&&tuvwx (&yz")
Various methods of using strings are converted into the following ways
Abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba
解答: 1.将 “123” 转换成整数
num = Int (123)
Print num
2.将 “9999999999999999999” 转换成长整数
num = Long (9999999999999999999)
Print num
3.将 “3.1415926” 转换成一个浮点数
num = float (3.1415926)
Print num
4.将 123 转换成一个字符串
num = str (123)
Print num
5.最后一题 分析思路:两个字符串都要剔除首尾空格,特殊字符,转换大小写,切片,相加 - [ ] 剔除首尾空格,特殊字符
STR1 = "ABC defgh&IJKL opq mnrst ((uvwxyz"
str2 = "Abc#def gh%ij mnopq klrs&&tuvwx (&yz")
STR1 = Str1.strip ()
STR2 = Str2.strip ()
Print str1
Print str2
strip()剔除首尾分隔符,默认是空格,可以自定义,自定义用‘XX‘例子见图示 ![](http://os9ep64t2.bkt.clouddn.com/17-12-20/90753838.jpg) 执行结果
C:\Users\chawn\PycharmProjects\171220\venv\Scripts\python.exe c:/users/chawn/. pycharmce2017.3/config/scratches/scratch.py
ABC defgh&IJKL opq Mnrst (uvwxyz
Abc#def gh%ij mnopq klrs&&tuvwx (&yz
还可以用替换来剔除空格、其他字符
STR1 = "ABC defgh&IJKL opq mnrst ((uvwxyz"
str2 = "Abc#def gh%ij mnopq klrs&&tuvwx (&yz")
STR1 = Str1.replace (","). Replace (' &', '). Replace (' (' (') ' (')
STR2 = Str2.replace (","). Replace (' # ', '). Replace ('% ', '). Replace (' && ', '). Replace (' (& ', ')
Print str1
Print str2
replace可以替换任意位置的空格,还有字符 - [ ] 大小写转换+切片
STR1 = "ABC defgh&IJKL opq mnrst ((uvwxyz"
str2 = "Abc#def gh%ij mnopq klrs&&tuvwx (&yz")
STR1 = Str1.replace (","). Replace (' &', '). Replace (' (' (') ' (')
STR2 = Str2.replace (","). Replace (' # ', '). Replace ('% ', '). Replace (' && ', '). Replace (' (& ', ')
STR1 = Str1.lower ()
STR1 = str1[0:12]+str1[15:17]+str1[12:15]+str1[17:]
STR2 = str2[0:10]+str2[10:15]+str2[15:17]+str2[17:]
Print str2 + str1[::-1]
执行结果
C:\Users\chawn\PycharmProjects\171220\venv\Scripts\python.exe c:/users/chawn/. pycharmce2017.3/config/scratches/scratch.py
Abcdefghijmnopqklrstuvwxyzzyxwvutsrqponmlkjihgfedcba
```
Python file types and strings