Woody's Python Study Notes 2, woodypython

Source: Internet
Author: User
Tags python list

Woody's Python Study Notes 2, woodypython

Python multi-line statements

In a Python statement, a new line is generally used as the statement Terminator. However, you can use a slash (\) to split a line of statements into multiple lines for display, as shown below:

Total = item_one + \

Item_two ++ \

Item_three

The statement contains [], {}, () and does not need to use multiline connectors, as follows:

Days = ['monday', 'tuesday', 'wednesday ','

Thursday ', 'Friday']

Python quotation marks

Python receives single quotation marks ('), double quotation marks ("), and three quotation marks (''',") to represent strings. The three quotation marks can be composed of multiple rows, A convenient syntax for writing multi-line text. It is often used as a document string and is used as a comment at a specific location of the file.

Word = 'word'

Sentence = "this is a sentence"

Paragraph = "" this is a paragraph. It is

Made u of multiple lines and sentences ."""

Empty Python lines

Functions or class methods are separated by blank lines to identify the beginning of a new code. The class and function entry are also separated by a blank line to highlight the beginning of the function entry.

Remember: Empty lines are part of the program code.

Waiting for user input

The following program waits for user input after pressing the Enter key:

#! /Usr/bin/python

Raw_input ("\ n \ npress the enter key to exit .")

In the above Code, '\ n \ n' will output two new blank lines before the result output. Once the user presses the key, the program will exit.


Multiple statements are displayed in the same row.

Python can use multiple statements in the same row. The statements are separated by semicolons, as shown below:

Importsys; x = 'foo'; sys. stdout. write (x + '\ n ')


Multiple statements form a code group

A group of statements with the same indentation constitute a code block, which is called a code group.

For a composite statement such as if, while, def, and class, the first line starts with a keyword and ends with a colon (:). One or more lines of code after the row form a code group.

We call the first line and the subsequent code group a clause ).

As follows:

If expression:

Suite

Elif expression:

Suite

Else:

Suite

Python variable assignment

Variables in Python do not need to be declared. Variable assignment is a process of variable declaration and definition.

Each variable created in the memory contains the variable identifier, name, and data.

Each variable must be assigned a value before it is used. The variable is created only after it is assigned a value.

The equal sign (=) is used to assign values to variables. The equal sign operator is a variable name on the left and the value stored in the variable on the right, as shown below:

Count = 100 # an integer assignment

Miles = '2017. 0 # a floating point

Name = "woody" # a string

Printcounter

Printmiles

Printname


Multi-variable assignment

Python allows assigning values to multiple variables at the same time, as shown below:

A = B = c = 1

For the above instance, create an integer object with a value of 1. The three variables are allocated to the same memory space.

You can also specify multiple variables for multiple objects, such:

A, B, c = 1, 2, 'woody'

In the preceding example, two integer objects 1 and 2 are allocated to a, B, and the string object 'woody 'are allocated to the variable c.

Python has five standard data types:

Numbers (number), string (string), list (list), tuple (tuples), dictionary (dictionary)

Python numbers

The numeric data type is an unchangeable data type, which means that a new object will be allocated when you change the numeric data type. When you specify a value, the number object will be created, for example:

Var1 = 1

You can use the del statement to delete a single active object, for example:

Delvar

Delvar1, var2

Python string

The python string list has two values:

1. The left-to-right index starts from 0 by default, and the maximum value is 1.

2. The index from right to left starts from-1 by default, and the maximum value is the start of the string.

If you want to extract a substring, you can use the variable name [header Subscript: tail subscript]. The subscript starts from 0 and can be a negative number, the subscript can be null to indicate that the header or tail is obtained, for example:

S = 'ilove python'

S [] The result is love.

When a string is separated by a colon, python returns a new object. The start on the left contains the lower boundary. The above result contains the value of s [1, the maximum obtained range does not include the upper boundary, that is, the value of s [5.

The plus sign is a String concatenation operator, and the asterisks are repeated operators, such:


Python list

List is the most frequently used data type in python.

The list can implement the data structure of most collection classes. It supports characters, numbers, strings, and even lists (I .e. nesting), such:


Python tuples

Tuples are identified by commas (). Internal elements are separated by commas, but elements cannot be assigned values twice (elements cannot be updated), which is equivalent to read-only lists.


Python dictionary

A dictionary is the most flexible data structure type in python except the list. A list is an ordered object set, and a dictionary is a disordered object set.

The difference between the two is that the elements in the dictionary are accessed through keys, rather than offset access.

The dictionary is identified by {}. The dictionary consists of an index (key) and its corresponding value.


Who can recommend the best PYTHON learning material?

We recommend that you use python2.X for python learning notes and learning python in pidaqing.

I just learned Python and used Python30. I recommend a book for beginners.

The release of python3.0 is not long before. We recommend that you use more than 2.7, with less information and less answers to problems.
First look at the python simple tutorial, and then look at the python core programming.
After learning, you can easily develop a crawler tool. If you want to research the web, you can study django, which will make you excited!

This is my learning path.

I have always been helpful!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.