Bài 1: Hello World

File lesson1.py

1
2
3
4
print "Hello World"
print "My name is John"
print "I'm Studying "
print "Python! Python! Python!"

Bài 2: Tính toán trong Python

File lesson2.py

1
2
3
4
5
6
7
8
print "Bat dau lam tinh:"
print "Tong 56+23:", 56+23
print "Tich 25x26:", 25*26
print "So sanh 3+2 > 5-1:"
print 3+2 > 5-1
print "Vi sao nhu the:"
print "3 + 2 =",3+2
print "5 - 1 =",5-1

Kết quả:

1
2
3
4
5
6
7
8
Bat dau lam tinh:
Tong 56+23: 79
Tich 25x26: 650
So sanh 3+2 > 5-1:
True
Vi sao nhu the:
3 + 2 = 5
5 - 1 = 4

Lưu ý: Ở đây ta nối chuỗi và phép tính bằng dấu “,” ngoài ra ta còn có thể nối chuỗi với nhau bằng dấu “+”

Bài 3: Sử dụng biến trong Python

File lesson3.py

1
2
3
4
5
6
7
cars=4
boys=6
string="in the house"
string2="in the garage"
print "There are", boys ,"boys in the house"
print "There are", cars ,"cars in the garage"
print "She is "+ string + " or " +string2

Kết quả:

1
2
3
There are 6 boys in the house
There are 4 cars in the garage
She is in the house or in the garage

File lesson4.py

1
2
3
4
5
name="John"
age=25
height=168
print "My name is %s"% name
print "I'm %s, %d years old, height %d cm"% (name,age,height)

Kết quả:

1
2
My name is John
I'm John, 25 years old, height 168 cm

File lesson5.py

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
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
"""
print "==========================================="
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat

Kết quả:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
===========================================
    I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
    * Cat food
    * Fishies
    * Catnip
    * Grass
Bài 4: Prompt for Input in Python

File lesson6.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
######### Asking Questions
print "Ten?",
name = raw_input()
print "Tuoi?",
age = raw_input()
print "Chieu cao?",
height = raw_input()
print "Can nang?",
weight = raw_input()
print "Toi ten %r, %r tuoi, cao %r cm, nang %r kg." % (name, age, height, weight)
######### Asking Questions 2
age2=raw_input("How old are you? ")
print "Toi %s tuoi" % age2

Kết quả:

1
2
3
4
5
6
7
Ten? John
Tuoi? 25
Chieu cao? 168
Can nang? 72
Toi ten 'John', '25' tuoi, cao '168' cm, nang '72' kg.
How old are you? 30
Toi 30 tuoi
Bài 5: Argv in Python

File: lesson7.py

1
2
3
4
5
6
7
#! /usr/bin/python
from sys import argv
script,first,second,thirth=argv
print "Running: ", script
print "First Arguement: ", first
print "Second Arguement: ", second
print "Thirth Arguement: ", thirth

Kết quả:

1
2
3
4
5
$ python lesson7.py a b c
Running:  lesson7.py
First Arguement:  a
Second Arguement:  b
Thirth Arguement:  c

File lesson8.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! /usr/bin/python
from sys import argv
script, name=argv
prompt="> "
print "Hi %s, I'm %s" % (name,script)
print "Can I ask you a lot of questions?"
ask=raw_input(prompt)
print "Do you like me?"
like=raw_input(prompt)
print "Where do you live %s?" % name
where=raw_input(prompt)
print """
So, you answer %s about liking me. And You live in %s.
""" % (like,where)

Kết quả:

1
2
3
4
5
6
7
8
9
10
$ python lesson8.py John
Hi John, I'm lesson8.py
Can I ask you a lot of questions?
> yes
Do you like me?
> yes
Where do you live John?
> HCM City
So, you answer yes about liking me. And You live in HCM City.
Print Friendly

Comments

comments

Bài viết liên quan