Chapter 1 Getting Started with python
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 "
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 "
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
1
>>> 3*4
>>> 4/2
2
1
>>> 3.0/2 Here we can see that python can process data according to the data type
>>> 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
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
2
Exercise: Write your own four arithmetic operator
#!/usr/bin/python
running = True
While running:
to
p=int(raw_input())
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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.
