Python _ CoBan
Python _ CoBan
I. Cơ Bản:
Tạo file: helloworld.py
print ‘hello world’
(màn hình hiển thị hello world)
_ Chạy chương trình hello world: (Command line)
$ python helloworld.py
_ Biến variable:
a=1
a = ‘ Hello World ‘
a = [1, 2, 3]
a = [1.2, ‘Hello’, ‘W’, 2]
Bài tập:
main1.py
nam_sinh = 1992
ho_ten = ‘Co ay’
thong_tin = { 95, 58, 88, 1.65, ‘Japan’ }
print(ho_ten + ‘ co chieu cao la ’ + thong_tin[‘chieu_cao’])
main2.py
nam_sinh = 1992
ho_ten = ‘Co ay’
thong_tin = {"so_do": [95, 58, 88], "chieu_cao": 1.65, "quoc_tich": 'Japan'}
print(ho_ten + 'co chieu cao la' + thong_tin['chieu_cao'])
Báo lỗi!
ERROR!
Traceback (most recent call last):
File "<string>", line 6, in <module>
TypeError: can only concatenate str (not "float") to str
Main3.py
nam_sinh = 1992
ho_ten = ‘Co ay’
thong_tin = {"so_do": [95, 58, 88], "chieu_cao": 1.7, "quoc_tich": 'Japan'}
print(ho_ten + ' co chieu cao la ' + str(thong_tin['chieu_cao']) +"m")
Main4.py
import time
from datetime import date
nam_sinh = 1992
ho_ten = ‘Co ay’
thong_tin = {
"so_do": [95, 58, 88],
"chieu_cao": 1.7,
"quoc_tich": 'Japan'}
nam_hien_tai = str(time.asctime( time.localtime(time.time()) ))
date1 = date.today()
# dd/mm/yyyy
date_homnay = date1.strftime("%d/%m/%Y")
nghi_huu = 4==4
print('Dinh dang dd/mm/yyyy:')
print(date_homnay)
Ta có:
>>> a = “ This is Cisco router “
>>> a.split(" ")
['', 'This', 'is', 'Cisco', 'Router', '']
f = open("interface.txt", "r")
int = f.read()
print(type(int))