form & aggregate & Selection structure & Loop structure
Pay attention to the comments when looking at the code :
# Tabular data is stored using dictionaries and lists , And achieve access
r1 = {
'name': ' test 1', 'age': 18}
r2 = {
'name': ' test 2', 'age': 19}
r3 = {
'name': ' test 3', 'age': 17}
tb = [r1, r2, r3]
# The result is :19
# print(tb[1].get("age"))
# The result is :
# test 1 18
# test 2 19
# test 3 17
for i in range(len(tb)):
print(tb[i].get("name"), tb[i].get("age"))
# aggregate : Sets are unordered and mutable , Elements cannot be repeated . actually , At the bottom of the collection is the dictionary implementation , All elements of the collection are Dictionary “ Key object ”, Therefore, it is not repeatable and unique
# Collection creation and deletion
# 1. Use {} Create a collection object , And use add() Method add element , Do not repeat
a = {
3, 5, 7}
a.add(10)
# The result is :{10, 3, 5, 7}
print(a)
# 2. Use set(), Will list , Turn iteratable objects such as tuples into collections , If there is duplicate data in the original data , Only one
a = ['a', 'b', 'c']
b = set(a)
# The result of printing is :{'c', 'b', 'a'}
print(b)
# remove() Deletes the specified element ;clear() Empty the whole assembly
b.remove("a")
# The result is :{'c', 'b'}
print(b)
b.clear()
# The result of printing is :set()
print(b)
# Set related operations : Like concepts in Mathematics ,Python Intersections are also provided for collections , Combine , Subtraction and so on
a = {
1, 3, 'sex'}
b = {
3, 4, 'he'}
# intersection
print(a | b)
# Combine
print(a & b)
# Difference set
print(a - b)
# Combine
print(a.union(b))
# intersection
print(a.intersection(b))
# Difference set
print(a.difference(b))
# Selection structure
# Select the structure by judging whether the condition is true , To decide which branch to take . There are many forms of selection structures , It is divided into : Single branch , Double branch , Multiple branches .
# Ternary operator
# Ternary operator syntax format :
# Value when condition is true if ( Conditional expression ) else The value when the condition is false
# num = input(" Please enter a number :")
# print(num if int(num) < 10 else " The number is too big ")
# Multi branch structure The structure is as follows
su = input(" Please enter a number :")
if int(su) < 10:
print(su)
elif 10 < int(su) < 20:
print(" middle ")
else:
print(" Greater than ")
print(" The score is :{0}".format(su))
# Select nesting of structure
if int(su) > 100 or int(su) < 0:
print(" Please enter a 0-100 The number of ")
else:
if int(su) < 10:
print(su)
elif 10 < int(su) < 20:
print(" middle ")
else:
print(" Greater than ")
# Loop structure : A loop is used to repeatedly execute one or more pieces of code
# while Loop structure :
num = 0
while num <= 10:
print(num)
num += 1