0% found this document useful (0 votes)
3 views

Python _ CoBan

Uploaded by

minhlilili
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python _ CoBan

Uploaded by

minhlilili
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python

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")

 Xuất ra: Co ay co chieu cao la 1.7m

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)

# Thang viet day du


date_homnay = date1.strftime("%B %d, %Y")
nghi_huu = 4==4
print('Ding dang Thang viet day du:')
print(date_homnay)

print(ho_ten +' co chieu cao la '+ str(thong_tin['chieu_cao']) +"m")


print(nam_hien_tai)
---
Xử lý chuỗi – Python

Trên Python Interpreter


>>> a = 'VNPRO _ vnpro _ quá _ pro'
>>> b = "VNPRO _ vnpro _ quá _ pro _ nha 123"
>>> print(a)
VNPRO _ vnpro _ quá _ pro
>>> print(b)
VNPRO _ vnpro _ quá _ pro _ nha 123
>>> c = '''VNPRO
... _vnpro
... _quá_pro'''
>>> print (c)
VNPRO
_vnpro
_quá_pro

>>> c = 'This \n is \n Cisco \n Router'

Ta có:
>>> a = “ This is Cisco router “
>>> a.split(" ")
['', 'This', 'is', 'Cisco', 'Router', '']

>>> a = " This is Cisco Router "


>>> a.split("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: empty separator
>>> a.split(" ")
['', 'This', 'is', 'Cisco', 'Router', '']
>>> word = a.split()
>>> word = a.split(" ")
>>> word[0]
''
>>> word[1]
'This'
>>> word[2]
'is'
>>> word[3]
'Cisco'
>>> word[4]
'Router'
>>> word[5]
''
>>> word[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

f = open("interface.txt", "r")
int = f.read()
print(type(int))

You might also like