Home > Database > Mysql Tutorial > body text

Chapter 1 Getting Started with python

黄舟
Release: 2016-12-22 17:02:46
Original
974 people have browsed it

Simple use of python

hy@hy:~/Documents/py$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help" , "copyright", "credits" or "license" for more information.
>>> 1+1
2
>>> exit()

hy@hy:~/Documents/py$

Let’s take a look at how to edit the python program in the python interpreter and check some exception error messages

>>> print1 'hello' The print we entered here is wrong, and a syntax exception will be reported Error

File "", line 1
print1 'hello'
^
SyntaxError: invalid syntax
>>> print 'hello'
hello
>>> exit()

hy@hy:~/Documents/py$ vim 1.py

#!/usr/bin/python

print 'hello world'


Section 1 python file type

source code

——The python source code file has the extension "py" and is interpreted by the python program and does not require compilation;

Byte code

——The extension of the compiled python source file is "pyc "File;

——Compilation method - import py_compile

py_compile.compile("hello.py")

Optimize the code

——Optimized source file with extension “.pyo”

——python -O -m py_compile hello.py

All the above three can be run directly

Let’s use an example to illustrate the process of the latter two compilation executions:

1)

hy @hy:~/Documents/py$ vim 1.py

#!/usr/bin/python
print 'hello world'

hy@hy:~/Documents/py$ vim 2.py

#!/usr/bin/python
import py_compile

py_compile.compile('1.py')

hy@hy:~/Documents/py$ python 2.py


hy@ hy:~/Documents/py$ ls

1.py 1.pyc 2.py We see that a 1.pyc file will be generated here. Using python to execute it can get the results we need

2)

hy@hy:~/Documents/py$ python -O -m py_compile 1.py

hy@hy:~/Documents/py$ ls

1.py 1.pyo Generated .pyo binary file

hy@hy:~/Documents/py$ python 1.pyo

hello world We see that we can also output


Section 2 Python variables

A variable is an area in computer memory. Values ​​within a specified range can be stored and the values ​​can be changed.

1) Naming of variables

a. Variable names consist of letters, numbers, and underscores.

b. Numbers cannot begin with

c. Keywords cannot be used

d. a a1 a_ a_1

2) Assignment of variables

a. It is a variable declaration and definition Process

a=1

ld(a)

Through the following code we can verify the specification of variable declaration in python

hy@hy:~/Documents/py$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", " credits" or "license" for more information.
>>> a=1
>>> a
1
>>> print 1
1
>>> print a
1
>>> a1=123
>>> a_1=111
>>> _a1=234
>>>
>>> 1a=123 Above The assignments are all correct. Here we can see that a syntax error occurs when starting with a number
File "", line 1
1a=123
^
SyntaxError: invalid syntax
>>>

In python, data is mainly used when calling data. Below we can see how assigning different values ​​to a will change its memory address:

>>> a=123
>> > id(a)       The change of the memory address after the first assignment
28372288
>>> a=456
>>> id(a)      The change of the memory address after the second assignment
28652040

When we continuously assign the same value to two variables at the same time, we will see that their addresses are the same, which means that the same data can have different labels

>>> ; a=123

>>> b=123
>>> id(a)
28372288
>>> id(b)
28372288

Exercise:

1 .Calculate how many minutes there are in a week: DaysPerWeek=7 HoursPerDay=24 MinutesPerHour=60 DaysPerWeek * HoursPerDay * MinutesPerHour
10080


>>> DaysPerWeek=7
>>> HoursPerDay=24
>>> MinutesPerHour=60
>>> Day * MinutesPerHour
10080

>



a. Assignment operator


b. Arithmetic operator


c. Relational operator


d. Logical operator


2) Expression is to use operation symbols to combine different data (including variables and functions) A formula connected by certain rules


We use the following examples to learn the functions of different operators


arithmetic operators

>>> 1+1
2

>>> 3-2

1
>>> 3*4

12

>>> 4/2
2

>>> 3/2

1

>>> 3.0/2 Here we can see that python can process data according to the data type

1.5

>>> 3.0//2 Here we can compare with the above, it only takes the integer part

1.0

> >> 17%6
5
>>> 3**2 Use ** to represent the power operation, here it means square
9
>>> 3**3 Here it means cube
27


Relational operator

a.'<'less than: 1 < 2

b.'>'greater than: 2 > 3


c.'<='less than or equal to: 1 < = 1


d.'>=' Greater than or equal to: 2 >= 2


e.'!=' Not equal to: 1 != 2

f.'=='Exactly equal to: 2 == 2

In python we can use the python interpreter to compare values. The return value here is of bool type

>>> 1<2
True
>>> ; 3! =3
False


Logical operator


a.'and' logical AND: True and False


b.'or'logical or: True or False


c.'not'logical negation: not True


Operator:


Lambda


Logical operation: or


Logical operation: and


Logical operation: not


Membership test: in, not in


Identity test: is , is not


comparison: <, <=, >, >=, !=, ==


bitwise OR: |


bitwise XOR: ^


bitwise AND :&


Shift: <<, >>


Addition and subtraction: +, -


Multiplication, division and remainder: *, /, %


Positive and negative sign: + x, -x


Bitwise flip: ~ ) That is 2

>>> 1<<1

2


Exercise: Write your own four arithmetic operator


#!/usr/bin/python

import sys

running = True
While running:

try:


t = int (raw_input ()) I make a description: raw_input is to enter a character or string from the keyboard, and then use the previous int to convert this character or string to


to

p=int(raw_input())

                                                                                                                                               print 'operator + result n ', T+P Print' Operator -Result N ', T -P
Print' Operator * Result n ', T * P

Print' Operator / Result N ', T / P


is the content of Python entry. , for more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!