Python QuickStart (1)

Source: Internet
Author: User
Tags logical operators

No punctuation after Python statement

Backslash (/): The statement is very long, we can use the backslash (\) to implement the multi-line statement

String: Single and double quotation marks in Python are used exactly the same
Use three quotation marks ("' or" ") to specify a multiline string (still multi-line state)

Inputs: Input ("Please enter:")

Variables in Python do not need to be declared. Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

Six standard data types: number, String, List, Tuple, sets, Dictionary

A numeric division (/) always returns a floating-point number to get an integer using the//operator

Truncated syntax format for strings: variable [head subscript: Tail subscript]
Note: print (str[0:-1]) # outputs all characters from the first to the penultimate

If you do not want the backslash to escape, you can add an R to the string before it, representing the original string:
Print (R ' Ru\noob ')

A list is a list of elements written in square brackets ([]) separated by commas, such as:
list = [' ABCD ', 786, 2.23, ' Runoob ', 70.2]
Syntax format for list interception: variable [head subscript: Tail subscript]
Unlike a python string, the elements in the list can be changed.
Note: A[2:5] = [] # Delete

Tuple (tuple) is similar to a list, except that the elements of a tuple cannot be modified. Tuples are written in parentheses (), and the elements are separated by commas.
Constructing tuples that contain 0 or 1 elements is special, so there are some additional syntax rules:
Tup1 = () # Empty tuple
Tup2 = (20,) # An element that needs to be added with a comma after the element

You can think of a string as a special tuple. (elements cannot be changed)
String, list, and tuple all belong to sequence (sequence).

A collection (set) is a sequence of unordered, non-repeating elements.
The basic function is to test the membership and remove duplicate elements.
You can use braces ({}) or set () functions to create a collection, note: Creating an empty collection must be set () instead of {},
Because {} is used to create an empty dictionary.

A list is an ordered combination of objects, and a dictionary is a collection of unordered objects.
The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.
tinydict = {' name ': ' Runoob ', ' Code ': 1, ' site ': ' www.runoob.com '}

Logical operators:
X and Y Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of Y.
X or Y Boolean "or"-if x is true, it returns true, otherwise it returns the computed value of Y.

Member operators: in, not in
Identity operator: is, is not

The variables in Python have 3 properties: Name, ID, value.
Name can be understood as variable name, ID can be combined with memory address to understand, value is a variable.
The IS operator is judged by this ID, which returns true as the ID, otherwise false.
= = In the comparison operator is used to compare the values of 2 objects for equality

The keyword end can be used to output the result to the same line, or to add different characters at the end of the output
A, b = 0, 1
While B < 1000:
Print (b, end= ', ')
A, B = B, a+b

There are no switch–case statements in Python

If condition_1:
Statement_block_1
Elif condition_2:
Statement_block_2
Else
Statement_block_3

While judging condition:
.......

There is no do in Python. While loop.

While judging condition:
.......
Else
.......

For <variable> in <sequence>:
<statements>
Else

Iterators:
The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without backing back.
Iterators have two basic methods: ITER () and next ().
list=[1,2,3,4]
it = iter (list) # Create iterator Object
The next element of the print (next (IT)) # Output iterator

Generator:
In Python, a function that uses yield is called a generator (generator).
Unlike a normal function, a generator is a function that returns an iterator that can be used only for iterative operations.
A simpler understanding of the generator is an iterator.
In the process of calling the generator to run, the function pauses and saves all current run information each time the yield is encountered, returning the value of yield. and continue running from the current location the next time you execute the next () method

def function name (parameter list):
function body

In Python, all parameters (variables) are passed by reference.

anonymous function lambda
sum = lambda arg1, arg2:arg1 + arg2;


Python QuickStart (1)

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.