[Python Notes (2) - Basic Statement Variable Type String Sequence List and Tuple Dictionary and Collection]

basic sentence

In Python, basic statements can help us complete some basic operations, such as controlling the flow, defining functions, and so on. The following are several basic statements in Python:

if statement

The if statement is used to determine whether a certain condition is true, and if the condition is true, the corresponding code block is executed.

num = 6
if num > 5:
    print("num > 5")
else:
    print("num <= 5")

for statement

The for statement is used to traverse the elements in the sequence and execute the corresponding code blocks in sequence.

list = ["apple", "banana", "cherry"]
for item in list:
    print(item)

while statement

The while statement is used to execute a block of code until a specified condition is not met.

i = 1
sum = 0
while i <= 10:
    sum += i
    i += 1
print(sum)

break statement

The break statement is used to break out of the current loop.

for i in range(1, 11):
    if i == 5:
        break
    print(i)

continue statement

The continue statement is used to skip an element in the current loop.

for i in range(1, 11):
    if i == 5:
        continue
    print(i)

variable type

In Python, numbers are a primitive data type, including integers, floating point numbers, and complex numbers. The numeric types in Python support basic arithmetic and logical operations.

integer

a = 10
b = 4
print(a + b)  # 加
print(a - b)  # 减
print(a * b)  # 乘
print(a / b)  # 除

floating point number

c = 3.1415926
d = 2.71828
print(c + d)  # 加
print(c - d)  # 减
print(c * d)  # 乘
print(c / d)  # 除

plural

e = 2 + 3j
f = 4 + 5j
print(e + f)  # 加
print(e - f)  # 减
print(e * f)  # 乘
print(e / f)  # 除

string

In Python, string is a basic data type that represents text content. Strings in Python support basic operations like slicing, concatenating, formatting, etc.

string slice

str = "Hello, world!"
print(str[0:5])  # 切片

string concatenation

str1 = "Hello"
str2 = "world"
str3 = str1 + " " + str2
print(str3)

string formatting

name = "Bob"
age = 25
print("My name is %s, and I am %d years old." % (name, age))

sequence

In Python, a sequence refers to a data type consisting of an ordered set of elements, including lists, tuples, and strings. Sequences in Python support basic operations like indexing, slicing, concatenation, repetition, etc.

list manipulation

list = ["apple", "banana", "cherry"]
print(list[0])  # 索引
print(list[1:3])  # 切片
list2 = ["orange", "grape"]
list3 = list + list2  # 拼接
list4 = list * 2  # 重复

tuple operation

tuple = ("apple", "banana", "cherry")
print(tuple[0])  # 索引
print(tuple[1:3])  # 切片

string manipulation

str = "Hello, world!"
print(str[0])  # 索引
print(str[0:5])  # 切片
str2 = "Python"
print(str + str2)  # 拼接
print(str * 2)  # 重复

Lists and Tuples

In Python, lists and tuples are two common sequence types that can be used to store an ordered set of data. While they have many similarities, there are also differences.

the list

The list is mutable, and the elements in it can be modified; the list is represented by square brackets [], and the elements are separated by commas.

list1 = ["apple", "banana", "cherry"]
list1.append("orange")  # 添加元素
list1.pop(1)  # 删除元素
list1[2] = "grape"  # 修改元素
print(list1)

tuple

Tuples are immutable and cannot be modified once created; tuples are denoted by parentheses () and elements are separated by commas.

tuple1 = ("apple", "banana", "cherry")
print(tuple1[0])  # 访问元素
len(tuple1)  # 获取长度
tuple2 = tuple(list1)  # 列表转元组
print(tuple2)

Dictionaries and collections

In Python, dictionaries and sets are two common non-sequence types. Dictionaries are used to store key-value pairs, and sets are used to store an unordered set of unique elements.

dictionary

Dictionaries are represented by curly braces {}, each key-value pair is separated by a colon: , and the key and value are separated by a comma, .

dict1 = {
    
    "apple": 1, "banana": 2, "cherry": 3}
print(dict1["apple"])  # 访问元素
dict1["banana"] = 4  # 修改元素
dict1["orange"] = 5  # 添加元素
del dict1["cherry"]  # 删除元素
print(dict1)

gather

Collections are represented by curly braces {}, and commas are used to separate elements.

set1 = {
    
    1, 2, 3}
set2 = {
    
    2, 3, 4}
print(set1.union(set2))  # 并集
print(set1.intersection(set2))  # 交集
print(set1.difference(set2))  # 差集
set1.add(4)  # 添加元素
set1.remove(2)  # 删除元素
print(set1)

Guess you like

Origin blog.csdn.net/muzillll/article/details/130948202