python learning notes

catalogue

1, Assign values to multiple variables

2, Standard data type

1.Number

2. String

3.List

4. Tuple (tuple)

5.Set

6.Dictionary

1, Assign values to multiple variables

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

a = b = c = 1

In the above example, create an integer object with a value of 1, assign values from back to front, and the three variables are given the same value.

You can also specify multiple variables for multiple objects. For example:

a, b, c = 1, 2, "runoob"

In the above example, two integer objects 1 and 2 are assigned to variables a and b, and the string object "runoob" is assigned to variable c.

2, Standard data type

There are six standard data types in python

Number

String (string)

List

Tuple (tuple)

Set

Dicyionary (Dictionary)

Among the six standard data types of Python 3:

  • Immutable data (3): Number, String, Tuple;
  • Variable data (3): List, Dictionary, Set.

1.Number

Python 3 supports int, float, bool and complex (complex).

In Python 3, there is only one integer type, int, expressed as a Long integer, and there is no Long in Python 2.

Like most languages, the assignment and calculation of numeric types are intuitive.

The built-in type() function can be used to query the object type that the variable refers to.

a,b,c,d=20,5.5,True,4+3j
print(type(a),type(b),type(c),type(d))

In addition, isinstance can be used to judge:

>>> a = 111
>>> isinstance(a, int)
True

2. String

 1. Single quotation marks and double quotation marks are used exactly the same in Python.

str1 = '123'
str2 = "123"
print(str1)
print(str2)

2. Use three quotation marks ('' 'or' ') to specify a multiline string.

str1 = '''This is a
 Multiline string'''
str2 = """This is a
 Multiline string"""
print(str1)
print(str2)

3. Escape character\

4. The backslash can be used to escape. Using r can prevent the backslash from escaping. For example, r"this is a line with\n" will be displayed instead of line breaking.

str1 = r"this is a line with\n"
str2 = "this is a line with\n"
print(str1)
print(str2)

5. Concatenate strings literally, such as "this" "is" "string" will be automatically converted to this is string

#str1 = r"this is a line with\n"
#str2 = "this is a line with\n"
print("this ""is ""string")
#print(str2)

6. Strings can be connected by + operator and repeated by * operator.

str1 = "123"
str2 = "456"
print(str1 + str2)
print(str2*3)

7. There are two indexing methods for strings in Python, starting with 0 from left to right and - 1 from right to left.

8. The string in Python cannot be changed.

9.python has no separate character type. A character is a string with a length of 1

10. The syntax format of string interception is as follows: variable [header subscript: tail subscript: step size]

str = "123456789"
print(str)         #Output string
print(str[0:-1])   #Output all characters from the first to the penultimate
print(str[0])      #The first character of the output string
print(str[2:5])    #Output characters from the third to the fifth
print(str[2:])     #Output all characters starting from the third
print(str[1:8:2])  #The output starts from the second to the ninth, one every two outputs

3.List

List is the most frequently used data type in Python.

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

The list is a comma separated list of elements written between square brackets [].

Like strings, lists can also be indexed and intercepted. After being intercepted, a new list containing the required elements is returned.

Syntax format of list interception: variable [header subscript: tail subscript]

list = ['abcd','786',2.23,'runoob',70.2]
tinylist = [123,'runnoob']

print(list)                 #Output full list
print(list[0])              #First element of output list
print(list[1:3])            #Output from the second to the third element
print(list[2:])             #Outputs all elements starting with the third element
print(tinylist*2)           #Output list twice
print(list + tinylist)      #Connection list

Unlike python strings, the elements in the list can be changed:

a = [1,2,3,4,5,6]
a[0] = 9          #Change the first element to 9
print(a)

a = [1,2,3,4,5,6]
a[2:5] = [13,14,15]  #Change from the third element to 13, 14 and 15
print(a)

a = [1,2,3,4,5,6]
a[2:5] = []       #Set null from the third element to the fifth element
print(a)

4. Tuple (tuple)

Tuples are similar to lists, except that the elements of tuple s cannot be modified. Tuples are written in parentheses (), and elements are separated by commas.

tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2  )
tinytuple = (123, 'runoob')

print (tuple)             # Output full tuple
print (tuple[0])          # The first element of the output tuple
print (tuple[1:3])        # The output starts from the second element to the third element
print (tuple[2:])         # Outputs all elements starting with the third element
print (tinytuple * 2)     # Output tuple
print (tuple + tinytuple) # Join tuple

Output results of the above examples:

('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 element of tuple cannot be changed, it can contain variable objects, such as list.

5.Set

A set is composed of one or several large and small entities with different shapes. The things or objects constituting the set are called elements or members.

The basic function is to test membership and delete duplicate elements.

You can use braces {} or the set() function to create a set. Note: to create an empty set, you must use set() instead of {}, because {} is used to create an empty dictionary.

Create format:

parame = {value01,value02,...}
perhaps
set(value)
sites = {'Google', 'Taobao', 'Runoob', 'Facebook', 'Zhihu', 'Baidu', 'Taobao'}

print(sites)  #Duplicate elements are automatically removed when the collection is output

if 'Zhihu' in sites:
    print('Zhihu In collection')
else :
    print('Zhihu Not in collection')

#python also implements various operations on sets
a = set('abc')
b = set('ad')

print(a - b)   #Difference set of a and b
print(a | b)   #Union of a and b
print(a & b)   #Intersection of a and b
print(a ^ b)   #Elements in a and b that do not exist at the same time

6.Dictionary

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

A list is an ordered collection of objects, and a dictionary is an unordered collection of objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

A dictionary is a mapping type. The dictionary is identified by {}. It is an unordered set of {keys): values.

The key must be of immutable type.

In the same dictionary, the key must be unique.

dict = {}
dict['one'] = "Obey the party's command"
dict[2] = "Can win the war"

tinydict = {3:'Excellent style','four':'Be loyal to the motherland','five':'Be loyal to the people'}

print(dict['one'])                 #The output key is the value of 'one'
print(dict[2])                     #The output key is a value of 2
print(tinydict)                    #Output complete dictionary
print(tinydict.keys())             #Output all keys
print(tinydict.values())           #Output all values

 

 

Keywords: Python

Added by Mick33520 on Mon, 24 Jan 2022 13:46:13 +0200