Skip to content

Commit 4295278

Browse files
committed
python learning if,for,break and all
1 parent 958ee96 commit 4295278

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

learning/helloWorld.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
1+
# Hello World
12
print("Hello World!");
23

4+
print("variable...")
5+
# variable
36
name = input("please enter your name: ");
4-
print("Hello,",name,"!");
7+
print("Hello,",name,"!");
8+
9+
print("if statements...")
10+
# if statements
11+
x = int(input("Please enter an integer: "))
12+
if x < 0:
13+
x = 0
14+
print("Negative changed to zero!")
15+
elif x == 0:
16+
print("Zero")
17+
elif x == 1:
18+
print("Single")
19+
else:
20+
print("More")
21+
22+
print("for statements...")
23+
# for statements
24+
a = ["cat", "dog", "pig"]
25+
for x in a:
26+
print(x, len(x))
27+
28+
print("break and 循环中的else...")
29+
# break and 循环中的else
30+
# 循环可以有一个 else 子句;它在循环迭代完整个列表(对于 for )或执行条件为 false (对于 while )时执行,
31+
# 但循环被 break 中止的情况下不会执行。
32+
for n in range(2, 10):
33+
for x in range(2, n):
34+
if n % x == 0:
35+
print(n, "equal", x, "*", n//x)
36+
break
37+
# 这个else语句对应的第二个for循环(不是最近的if语句),它会在第二个for循环执行完时执行,
38+
# 但循环被break中止的情况下不会执行。
39+
else:
40+
print(n, "is a prime number")
41+
42+
print("continue...")
43+
# continue
44+
for num in range(2, 10):
45+
if num % 2 == 0:
46+
print("Found an even number", num)
47+
continue
48+
print("Found a number", num)
49+
50+
print("pass statements...")
51+
print("pass 语句什么也不做。它用于那些语法上必须要有什么语句,但程序什么也不做的场合,",
52+
"另一方面, pass 可以在创建新代码时用来做函数或控制体的占位符。")
53+
# pass statements
54+
while True:
55+
pass

0 commit comments

Comments
 (0)