A brief talk on Python (i.)

Source: Internet
Author: User

Install the Configuration Python environment

Print Hello world! After the configuration is successful

>>> print (' Hello world! ')
Hello world!

I. Identifiers

Usually the variable name, method name, class name, etc., consists of numbers, underscores, letters, the first character must be a number or underscore, the case is different.

Two. Keywords

Can not be used for identifier names, if you are not clear what the key words, may query, as follows

>>> Import keyword
>>> keyword.kwlist
[' False ', ' None ', ' True ', ' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' fi Nally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' nonlocal ', ' not ', ' or ', ' Pass ', ' raise ', ' return ', ' Try ', ' while ', ' with ', ' yield ']

Three. Multi-line statements

Python is usually a line to complete a statement, but if the statement is long, we can use a backslash (\) to implement a multiline statement, for example:

>>> total=item_one+\
Item_two

In [], {}, or () a multiline statement, you do not need to use a backslash (\), for example:

>>> total=[' Item_one ', ' item_two '
, ' Item_three ',]

Note: Character input needs to be changed in order, followed by parentheses last input

Four. Data type

1. Integer: if 1

2. Long integers: larger integers

3. Floating-point numbers: such as 1.2, 3E-1

4. Plural: 1+1j, 2+2.2j

Five. String

The string is the most commonly used data type in Python. We can use quotation marks (' or ') to create a string.

Creating a string is simple, as long as you assign a value to the variable. For example:

>>> var1= ' Hello world! '
>>> var2= ' Runoob '

Python does not support single character types, and one character is also used as a string in Python.

Python accesses the string by using square brackets to intercept the string, as in the following example:

>>> var1= ' Hello world! '
>>> var2= ' Runoob '
>>> print (' var1[0]: ', var1[0])
VAR1[0]: H
>>> print (' var2[1:5]: ', Var2[1:5])
Var2[1:5]: Unoo

Python modifies an existing string and assigns a value to another variable.

>>> var1= ' Hello World '
>>> print (' Update: ', var1[:6]+ ' dada! ')
Update:hello dada!

Note: Python can use multiple statements in the same row, with semicolons (;) split between statements , such as

>>> str1= ' Fenglei '
>>> str2= ' Xiaohai '
>>> print (str1+ ' not already ');p rint (STR2)
Fenglei is no longer
Xiaohai

  Print default output is newline, if you want to implement no line break need to add end= "" at the end of the variable

>>> str1= ' Fenglei '
>>> str2= ' Xiaohai '
>>> print (str1+ "No", end= "");p rint (STR2)
Fenglei is no longer xiaohai.

Python allows you to assign values to multiple variables at the same time. For example:

>>> a=b=c=d=0
>>> print (a);p rint (b);p rint (c);p rint (d)
0
0
0
0

>>> a=b=c=d=0,1,2,3
>>> print (a);p rint (b);p rint (c);p rint (d)
(0, 1, 2, 3)
(0, 1, 2, 3)
(0, 1, 2, 3)
(0, 1, 2, 3)

It is also supported to specify multiple variables for multiple objects. For example:

>>> a,b,c,d=0,1,2,3
>>> print (a);p rint (b);p rint (c);p rint (d)
0
1
2
3

Note: Four variables must be assigned to a four value

The plus sign (+) is the string's connector, and an asterisk (*) indicates that the current string is copied, followed by the number of copies. Such as

>>> a= ' good '
>>> Print (A * 2)
Goodgood

There are two ways to index a string in Python, starting from left to right and starting at 0 from right to left with-1. Such as

>>> a= ' World '
>>> print (a[0],a[4])
W D

>>> a= ' World '
>>> print (a[-1],a[-4])
D o

Six. List of lists

The list is the most frequently used data type in Python.

A list can accomplish the data structure implementation of most collection classes. The types of elements in a list can be different, it supports numbers, and strings can even contain lists (so-called nesting).

A list is a comma-delimited list of elements written between square brackets [].

As with strings, lists can also be indexed and truncated, and a new list containing the required elements is returned when the list is truncated.

The syntax format for the list interception is as follows:

Variable [head subscript: Tail subscript]
List= [ ' ABCD ', 786 , 2.23, ' Runoob ', 70.2 ]Tinylist= [123, ' Runoob ']Print (List) # Output Full listPrint (List[0]) # The first element of the output listPrint (List[1:3)  # from the second start output to the third element print (list[2:])  # output all elements starting from the third element print   (tinylist *  2)  # output two-time list print  (list + tinylist   # connection list          

The result of the above example output:
[' ABCD ', 786, 2.23, ' Runoob ', 70.2]Abcd[786, 2.23][2.23, ' Runoob ', 70.2][123, ' Runoob ', 123, ' Runoob '][' ABCD ',  786, 2.23, ' Runoob ', 70.2, 123,  ' Runoob ']                 
Unlike a python string, the elements in the list can be changed:

>>> a=[1,2,3,4,5,6]
>>> a[0]=9
>>> a[2:5]=[13,14,15]
>>> Print (a)
[9, 2, 13, 14, 15, 6]

Or

>>> a=[1,2,3,4,5,6]
>>> a[0]=9
>>> a[2:5]=[13,14,15]
>>> a[2:5]=[] #删除
>>> A
[9, 2, 6]

Seven. Tuple tuples

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. In fact, a string can be thought of as a special tuple.

The elements in a tuple can also be of different types:

Tuple= ( ' ABCD ', 786 , 2.23, ' Runoob ', 70.2 )Tinytuple= (123, ' Runoob ')Print (Tuple) # Output Full tuplePrint (Tuple[0]) # The first element of an output tuplePrint (Tuple[1:3)  # output starting from the second element to the third element print   (tuple[2 :])  # output all elements starting from the third element print   (tinytuple * 2)  # output two tuples  print  (tuple + Tinytuple)  # join tuple      

The result of the above example output:
(' ABCD ', 786, 2.23, ' Runoob ', 70.2)Abcd(786, 2.23)(2.23, ' Runoob ', 70.2 (123,  ' Runoob ' , 123,  ' Runoob ' )  (  ' ABCD ' , 786 2.23,  ' Runoob ' , 70.2,  123,  ' Runoob '       

Although the elements of a tuple cannot be changed, it can contain mutable objects, such as list lists.

Constructing tuples that contain 0 or 1 elements is special, so there are some additional syntax rules:

=()    # empty tuple =(A,)# An element that needs to be added with a comma after the element      

String, list, and tuple all belong to sequence (sequence).

Attention:

    • 1. As with strings, elements of tuples cannot be modified.
    • 2. Note the special syntax rules that construct tuples that contain 0 or 1 elements.

Eight. Set set

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 create a collection using the curly braces {} or the set () function, note: Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary.

>>> a = {' Tom ', ' Jim ', ' Mary ', ' Tom ', ' Jack ', ' Rose '}
>>> A
{' Tom ', ' Mary ', ' Jack ', ' Jim ', ' Rose '} #输出集合, duplicate elements are removed

>>> a = {' Tom ', ' Jim ', ' Mary ', ' Tom ', ' Jack ', ' Rose '}
>>> if (' Rose ' in a):
Print ("Rose in the Collection")
Else
Print (' Rose is not in the collection ')


Rose in the collection

# set can perform set operationsA= Set(' Abracadabra ')B= Set(' Alacazam ')Print(A)print (a -  B)  # A and B difference set  print (a | b )  # A and B in the same set print (a & B)  # A and B intersection print (a ^ B)  # Elements in A and b that do not exist at the same time 
The output is:
{' B ', A, C, ' R ', ' d '}{' B ', ' d ', ' R '}{L, ' R ', A, C,  ' z ' ,  ' m '    ' B ' ,  ' d ' }{ ' a ' ,  ' C ' }{ ' l ' ,  ' R ' ,  ' Z ' ,  ' m ' ,  ' B ' ,  ' d ' } 

Nine. Dictionary (dictionary)

is another very useful built-in data type in Python.

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.

A dictionary is a type of mapping that is identified by the {}, which is an unordered key (key): The value pair collection.

The key (key) must use the immutable type.

In the same dictionary, the key must be unique

A brief talk on Python (i.)

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.