Skip to content

Commit 1780a43

Browse files
committed
add py3 source for basic
1 parent feb9b03 commit 1780a43

11 files changed

Lines changed: 105 additions & 0 deletions

File tree

py3/basic/do_for.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# 打印list:
5+
names = ['Michael', 'Bob', 'Tracy']
6+
for name in names:
7+
print(name)
8+
9+
# 打印数字 0 - 9
10+
for x in range(10):
11+
print(x)

py3/basic/do_if.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# 注意:
5+
# input()返回的是字符串
6+
# 必须通过int()将字符串转换为整数
7+
# 才能用于数值比较:
8+
age = int(input('Input your age: '))
9+
10+
if age >= 18:
11+
print('adult')
12+
elif age >= 6:
13+
print('teenager')
14+
else:
15+
print('kid')

py3/basic/do_input.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
name = input()
5+
print('Hello,', name)

py3/basic/do_print.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
print('The quick brown fox', 'jumps over', 'the lazy dog')
5+
print(300)
6+
print(100 + 200)
7+
print('100 + 200 =', 100 + 200)

py3/basic/do_while.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# 计算1+2+3+...+100:
5+
sum = 0
6+
n = 1
7+
while n <= 100:
8+
sum = sum + n
9+
n = n + 1
10+
print(sum)

py3/basic/hello.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
print('Hello, world')

py3/basic/the_dict.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
d = {
5+
'Michael': 95,
6+
'Bob': 75,
7+
'Tracy': 85
8+
}
9+
print('d[\'Michael\'] =', d['Michael'])
10+
print('d[\'Bob\'] =', d['Bob'])
11+
print('d[\'Tracy\'] =', d['Tracy'])
12+
print('d.get(\'Thomas\', -1) =', d.get('Thomas', -1))

py3/basic/the_list.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
classmates = ['Michael', 'Bob', 'Tracy']
5+
print('classmates =', classmates)
6+
print('len(classmates) =', len(classmates))
7+
print('classmates[0] =', classmates[0])
8+
print('classmates[1] =', classmates[1])
9+
print('classmates[2] =', classmates[2])
10+
print('classmates[-1] =', classmates[-1])
11+
classmates.pop()
12+
print('classmates =', classmates)

py3/basic/the_set.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
s1 = set([1, 1, 2, 2, 3, 3])
5+
print(s1)
6+
s2 = set([2, 3, 4])
7+
print(s1 & s2)
8+
print(s1 | s2)

py3/basic/the_string.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
s = 'Python-中文'
5+
print(s)
6+
b = s.encode('utf-8')
7+
print(b)
8+
print(b.decode('utf-8'))

0 commit comments

Comments
 (0)