文章

python练习-Hello World

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Python demo for lihz
# 1. Hello World
print("Hello World Python")

# 2. 输入输出
print("The quick brown fox", "jumps over the lazy dog")
print("100", "+", "200", "=", 100 + 200)
# name = input("What is your name ? \r\n")
# print("My name is",name)
print('''Line1
Line2
Line3''')

# 3. 字符串
print(chr(66))
print(chr(22334))
# bytes 数据类型
x = b'abc'
print(x)
print("中文".encode("utf-8"))
print(b'\xe4\xb8\xad\xe6\x96\x87'.decode("utf-8"))
print("My name is %s" % "OneCoder")
age = 38
print(f"My age is {age}")

# 4. list  tuple
# list 有序集合
numbers = ["8676", "8616", "8385"]
print("The length of list numbers is %s" % len(numbers))
numbers.append("8528")
numbers.pop(1)
numbers.insert(1, "8448")
print(numbers)

# tuple 不可变元组
families = ("Dad", "Mum", "Son")
print(len(families))
one = (1,)
print(one)
print((1))

# 5. 判断和循环
number = 20
if int(number) > 18:
    print(number)
elif int(number) > 6:
    print("less than 18")

sum = 0
for x in range(101):
    sum += x
print(sum)

# 6. dict即map和Set
d = {"Dad": 38, "Mum": 37}
print(d["Mum"])
print(d.get("Son", "Yummy"))
# set 元素不重复
s1 = set([1, 2, 3, 3])
print(s1)
s2 = set([2, 3, 4])
s1.add(5)
print(s1 & s2)
print(s1 | s2)

# 7. 函数
h = hex
print(h(12))


def print_name(name):
    print("My name is", name)


def return_tuple():
    return 12, 34


x, y = return_tuple()
print(x)
print(y)
print_name("Yummy")


def power(x, n=2):
    s = 1
    while n > 0:
        s = s * x
        n -= 1
    return s


print(power(5))
print(power(5, 3))


def sums(*nums):
    result = 0
    for i in nums:
        result += i
    return result


print(sums(1, 2, 3, 4, 5))


def person(name, age, **others):
    print("Name:", name, "Age:", age, "Others: ", others)


extra = {"city": "DaLian", "gender": "Male"}
# 获得的室extra的拷贝,修改不会影响extra的值
person("Yummy", 8, **extra)


def person_two(name, age, *, city):
    print("Name:", name, "Age:", age, "City:", city)


person_two("Yummy", 8, city="DaLian")


def f1(a, b, c=1, *args, **kwargs):
    print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kwargs)


f1(1, 2, 4, 4, 4, 4, kwargs={"5": 5})
本文由作者按照 CC BY 4.0 进行授权