Python learning section 2

 

 1. Learn Python data types (strings, numbers, lists, tuples, dictionaries)

Python has 5 main data types:

1. String: define using single quotes or double quotes, for example: 'hello' or "world".
You can use the plus sign (+) to concatenate strings, for example: 'hello' + 'world' The result is 'helloworld'.

2. Numbers: including integers (int), floating point numbers (float) and imaginary numbers (complex).
For example: 10, 3.14, 1+2j.

3. List: The definition uses square brackets [], and the list elements are separated by commas. The contents of the list can be changed.
For example: [1, 2, 3], ['a', 'b', 'c'].

4. Tuple: Use parentheses () for definition. Once a tuple is defined, it cannot be modified.
For example: (1, 2, 3), ('a', 'b', 'c').

5. Dictionary (dict): The definition uses curly brackets {}. The dictionary consists of key-value pairs, separated by colons (:).

First explain the key-value pairs:

A key-value pair is a data structure consisting of two elements: key and value. Common implementations of key-value pairs include:

1. Dictionary (dict):
A dictionary consists of key-value pairs. The keys must be of immutable type (string, number, or tuple), and the values ​​can be of any type.
For example:

python
d = {'name': 'Tom', 'age': 12}

You can use keys to find values:

python
d['name'] # Tom

You can also modify the values ​​in the dictionary:

python 
d['age'] = 13

2. Hash table:
A hash table is also composed of key-value pairs, and the keys must be of a hashable type. Hash by key to locate the value.

3. Associative array:
Simply understood as an abstract data type of key-value pairs, the stored content is the key-value pair.

4. Object: Object-
oriented properties and methods can also be regarded as key-value pairs, where properties are keys and property values/methods are values.
For example:

python
class Person:
    def __init__(self, name, age):
        self.name = name    # 属性名(键)为name,属性值(值)为name的值 
        self.age = age      # 属性名(键)为age,属性值(值)为age的值

p = Person('Tom', 12)
print(p.name)   # Tom  通过键(属性)查找值

So, in general, key-value pairs are a mechanism for using keys to look up values. This mechanism is reflected in dictionaries, hash tables, and object-oriented properties.
Keys can be used to quickly locate related values, which is why key-value pairs are often used to implement mapping relationships.


For example: {'name':'Tom', 'age':12}. Here are some simple examples to illustrate various data types: String:

python 
s = 'hello'
s2 = "world"
s3 = s + s2   # s3 为'helloworld'

number:

python
a = 10   # 整型
b = 3.14 # 浮点型
c = 1 + 2j  # 虚数

List:

python
fruits = ['apple', 'orange', 'cherry']
fruits[0] # apple, 索引从0开始
fruits[-1] # cherry, 可以使用负索引,从后向前取

tuple:

python
t = (1, 2, 3) 

dictionary:

python 
d = {'name':'Tom', 'age':12}
d['name'] # Tom, 通过键取值

In addition to the five main data types mentioned above (strings, numbers, lists, tuples, and dictionaries), Python also has several other data types:

1. Set: defined using curly brackets {}, the elements of the set are unordered and not repeated. Operations such as intersection, union, and difference can be performed.
For example: {1, 2, 3}, {'a', 'b', 'c'}.

2. Boolean: has only two values: True and False.
For example: True, False.

3. Range: Define using the range() function to represent a sequence of continuously increasing integers.
For example: range(5) results in [0, 1, 2, 3, 4].

4. None: Indicates empty or missing values. There is only one None object.

5. File object (file): Represents an open file. You can use the open() function to open the file to obtain the file object.
For example: f = open('test.txt'). 

6. Type: You can use type() to determine the type of a variable.
For example: type(1) is <class 'int'>, type([]) is <class 'list'>.

7. Module: A Python file is a module, and you can use import to import the module.
For example: import math, random. In addition, Python also has bytes, bytearray, memoryview, frozenset, Decimal and other data types.

Here are some simple examples to illustrate other data types:

gather:

python
s = {1, 2, 3}
s.add(4)   # s 为 {1, 2, 3, 4}
s.remove(2) # s 为 {1, 3, 4}

Boolean:

python
t = True
f = False 

scope:

python
r = range(5)   # r 为 [0, 1, 2, 3, 4]

none:

python
n = None 

type:

python 
type(1)         # <class 'int'>
type([])        # <class 'list'>
type(lambda x:x) # <class 'function'> 

2. Install Python and configure the development environment

Take a look at Daniel’s tutorial and quickly download, install and deploy it! 

http://t.csdn.cn/MUuv7
3. Write the first Python program to output "Hello World"

print("Hello World")

 211046d7d87f48cc9b3fa44746102bab.png

 

 

 

Guess you like

Origin blog.csdn.net/weixin_46468815/article/details/130475031