You are on page 1of 8

Remarks!

This is an introductory class to ipython notebook and Python


Programming cannot be taught 100%. You will have to learn by yourself, for example, by
searching the internet.
Programming involves a lot of Math.
There are always multiple solutions to one answer.

Name Type Details

Integers int Whole numbers: 1 20 23 56 2020

Floating point float Decimal points: 1.2 4.56 2020.0

Complex numbers complex Complex numbers: 3.0+2.0j, -1.0j, where j = √−1


Name Type Details

Strings str sequence of charaters or sentences: 'A', 'hello world', "Bangkok KMUTT"

Lists list Order sequence of anything: [10, 'Non, str ,float, function]

Tuples tup immutable sequence of anything (10, 'Non, str ,float, function)

Dictionaries dict Key:Values pairs: {'key1':'values1', 'key2':'values2', ...}

Sets set Unordered non-repeatable set {10, 'Non, str ,float, function}

Booleans bool Logical True or False

ตัวแปรที่เป็ นการเรียงลำดับของข้อมูลประเภทลิสต์ (List)


ข้อมูลภายใน list สามารถเป็ นข้อมูประเภทใด ๆ ก็ได้ รวมถึงฟังก์ชันต่าง ๆ ในไพธอน

In [ ]: list1 = [1,2,3,4,5]

list2 = [6,7,8,9,"Aj.Non"] #[6,7,8,9,list1]

print(list1)

print(list2)

[1, 2, 3, 4, 5]

[6, 7, 8, 9, 'Aj.Non']

In [ ]: print(len(list1))

print(len(list2))

การบวกตัวแปรประเภท list คือการนำข้อมูลของทั้งสอง list มาเรียงลำดับต่อกัน

In [ ]: list1+list2

[1, 2, 3, 4, 5, 6, 7, 8, 9, [1, 2, 3, 4, 5]]


Out[ ]:

ดัชนี (index) และการเลือกชุดของข้อมูลย่อยในลิสต์ (slicing) จะมีลักษณะเช่นเดียวกันกับ index และ


slicing ในตัวแปรประเภท string

In [ ]: list2[0]

6
Out[ ]:

In [ ]: list2[4]

[1, 2, 3, 4, 5]
Out[ ]:

In [ ]: list2[4][3]

4
Out[ ]:

ตัวแปรลิสต์สามารถเป็ นข้อมูลในหนึ่งลำดับของตัวแปรลิสต์อีกหนึ่งตัวแปรได้ ทำให้เกิดลักษณะของลิสต์


ซ้อนลิสต์ (Nested list)

In [ ]: #nested list

complex_list = [1,2,3, [4,5,['I', 'love', 'KMUTT'],6]]

print(len(complex_list))

In [ ]: a = complex_list[3]

print(a)

[4, 5, ['I', 'love', 'KMUTT'], 6]

In [ ]: b = a[2]

print(b)

['I', 'love', 'KMUTT']

In [ ]: c = b[2]

print(c)

KMUTT

In [ ]: complex_list[3][2][2]

'KMUTT'
Out[ ]:

In [ ]: print(complex_list[3][2][2][3:])

print(complex_list[3][2][2][::-1])

TT

TTUMK

In [ ]: email1 = 'non.tho@kmutt.ac.th'

email2 = 'n.thongprong@gmail.com'

In [ ]: print(email1.split('@')[0])

print(email2.split('@')[0])

non.tho

n.thongprong

In [ ]: print(email1.split('@')[1])

print(email2.split('@')[1])

kmutt.ac.th

gmail.com

In [ ]: email1.split('@')[1].split(".")[1]

'ac'
Out[ ]:

In [ ]: list3 = ['a','e','i','o','u']

In [ ]: print(list3[0])

print(list3[1])

print(list3[2])

print(list3[3:4])

print(list3[3:5])

print(list3[:3])

print(list3[2:])

print(list3[-1]) #grab the last

print(list3[::-1])

['o']

['o', 'u']

['a', 'e', 'i']

['i', 'o', 'u']

['u', 'o', 'i', 'e', 'a']

Methods พิ้นฐานของตัวแปรประเภทลิสต์
In [ ]: list3 = ['a','e','i','o','u']

การแทรกข้อมูลเข้าไปในลำดับที่กำหนด

In [ ]: list3.insert(2, 'Hello')

print(list3)

['a', 'e', 'Hello', 'i', 'o', 'u']

การเพิ่มข้อมูลไปที่ตำแหน่งท้ายสุดของลิสต์

In [ ]: list3.append("World!")

print(list3)

['a', 'e', 'i', 'o', 'u', 'World!']

การขยายตัวแปรลิสต์ตัวยข้อมูลหรือลิสต์ของข้อมูล

In [ ]: list3.extend(["KMUTT", 'Aj.Non'])

list3

['a', 'e', 'i', 'o', 'u', 'KMUTT', 'Aj.Non']


Out[ ]:

In [ ]: list3.extend('Cat')

list3

['a', 'e', 'i', 'o', 'u', 'KMUTT', 'Aj.Non', 'C', 'a', 't']
Out[ ]:

In [ ]: list3.remove('Hello')

print(list3)

['a', 'e', 'i', 'o', 'u', 'World!']

การนำข้อมูลในลำดับหนึ่ง ๆ ของลิสต์ออกไปจากลิสต์

In [ ]: print(list3.pop(5))

print(list3)

World!

['a', 'e', 'i', 'o', 'u']

การนับว่าในลิสต์นั้น ๆ มีข้อมูลที่จำเพาะอยู่กี่ตำแหน่ง

In [ ]: list3.count('a')

1
Out[ ]:

In [ ]: list3.index("o")

3
Out[ ]:

การเรียงลำดับของข้อมูลในลิสต์ใหม่

In [ ]: list4 = [12,2,41,37,19,32]

list4.sort()

print(list4)

[2, 12, 19, 32, 37, 41]

ตัวแปรประเภท Dictionaries
Dictionary คือตัวแปรที่เป็ นชุดของข้อมูลแบบไม่เรียงลำดับ ในการเรียกใช้ข้อมูลย่อยหนึ่ง ๆ จะต้องมี Key
ในการเรียกข้อมูลนั้นออกมาก ใน Python จะมีไวยกรณ์ในการประกาศตัวแปรประเภท dictionary ดังนี้

Dict = {key1:value1, key2:value2, ...}


ยกตัวอย่างเช่น

In [1]: data = {'name':'Non', 'job':'lecturer', 'money':1000.0, 'courses':['Phys', "Nano", 'Co

In [2]: data['name']

'Non'
Out[2]:

In [3]: data['courses']

['Phys', 'Nano', 'Computational']


Out[3]:

In [ ]: data['courses'][1]

'Nano'
Out[ ]:

การเพิ่มข้อมูลใหม่เข้าไปใน dictionary หรือการเปลี่ยนค่าข้อมูลหนึ่ง ๆ ใน dictionary

In [ ]: data['money']=5000

data

{'courses': ['Phys', 'Nano', 'Computational'],

Out[ ]:
'job': 'lecturer',

'money': 5000,

'name': 'Non'}

Key ในการเรียกข้อมูลของ dictionary สามารถเป็ นได้ทั้ง string, ตัวเลขจำนวนเต็ม, จำนวนจริง หรือแม้


กระทั่งชื่อของตัวแปร

In [ ]: pi = 3.14

data2 = {1:"Value1", 2.5:1.67e-7, pi:"Number", }

In [ ]: print(data2[1])

print(data2[2.5])

print(data2[pi])

Value1

1.67e-07

Number

Methods พื้นฐานในการทำงานกับ dictionary

In [ ]: print(data.items()) #เรียกดูข้อมูลหรือวัตถุ (items) ทั้งหมดใน dictionary

print(data.keys()) #เรียกดู key ทั้งหมด

print(data.values()) #เรียกดูค่าของข้อมูลทั้งหมด

data.update({'email':"non.tho@kmutt.ac.th"}) #การเปลี่ยนแปลงค่าของข้อมูลหนึ่ง ๆ

data['city'] = 'Bangkok' #การเปลี่ยนแปลงค่าของข้อมูลหนึ่ง ๆ

data

dict_items([('name', 'Non'), ('job', 'lecturer'), ('money', 5000), ('courses', ['Phy


s', 'Nano', 'Computational']), ('email', 'non.tho@kmutt.ac.th')])

dict_keys(['name', 'job', 'money', 'courses', 'email'])

dict_values(['Non', 'lecturer', 5000, ['Phys', 'Nano', 'Computational'], 'non.tho@kmu


tt.ac.th'])

{'city': 'Bangkok',

Out[ ]:
'courses': ['Phys', 'Nano', 'Computational'],

'email': 'non.tho@kmutt.ac.th',

'job': 'lecturer',

'money': 5000,

'name': 'Non'}

การนำเอาข้อมูลหนึ่ง ๆ ออกไปจาก dictionary

In [ ]: data.pop("job")

'lecturer'
Out[ ]:

In [ ]: data

{'city': 'Bangkok',

Out[ ]:
'courses': ['Phys', 'Nano', 'Computational'],

'email': 'non.tho@kmutt.ac.th',

'money': 5000,

'name': 'Non'}

การลบข้อมูลหนึ่ง ๆ ใน dictionary โดยไปลบออกจากหน่วยความจำของเครื่อง

In [ ]: del data['city']

data

{'courses': ['Phys', 'Nano', 'Computational'],

Out[ ]:
'email': 'non.tho@kmutt.ac.th',

'money': 5000,

'name': 'Non'}
ตัวแปรประเภทตรรกะ (ประพจน์จริง หรือ ประพจน์เท็จ)
(Booleans)
In [4]: a = True

b = False

# การเชื่อมประพจน์และนิเสธของประพจน์

print(not a) # นิเสธ

print(a and b) # เชื่อมด้วย and

print(a or b) # เชื่อมด้วย or

print(not(a and b))

False

False

True

True

In [ ]: print(a & b) # เชื่อมด้วย and

print(a | b) # เชื่อมด้วย or

print(not(a | b))

False

False

เราสามารถใช้ค่าของข้อมูลประเภทจำนวนเต็มหรือจำนวนจริงที่เท่ากับ 0 ในการแทนประพจน์ที่เป็ นเท็จได้


และจำนวนที่มีค่าใด ๆ ที่ไม่เท่ากับ 0 ทดแทนประพจน์ที่เป็ นจริง

In [ ]: a = True

b = False

c = 1

d = 0

In [ ]: print(a and b)

print(a or b)

print(not b)

print(a & b)

print(a | b)

False

True

True

False

True

In [ ]: print(2 and 0)

print(0 or 1)

print(not 0)

print(not 3)

print(not 11.5)

True

False

False

In [ ]: print(bool(10.0))

print(bool(0.0))

True

False

การตรวจสอบประเภทของข้อมูล (Types) และการแปลงข้อมูล


จากประเภทหนึ่งไปยังอีกประเภทหนึ่ง (conversion)
In [5]: a = "25"

print(a, type(a))

# แปลง a ให้เป็ นจำนวนเต็ม

a = int(a)

print(a, type(a))

# แปลง a ให้เป็ นจำนวนจริง

a = float(a)

print(a, type(a))

# แปลง a ให้เป็ น boolean

a = bool(a)

print(a, type(a))

# แปลง a ให้เป็ น string

a = str(a)

print(a, type(a))

25 <class 'str'>

25 <class 'int'>

25.0 <class 'float'>

True <class 'bool'>

True <class 'str'>

การรับต่าตัวแปรจากคีย์บอร์ด (Keyboard input)


In [ ]: a = input('Please input number')

print(a)

Please input number25

25

จงรับตัวเลขจากคีย์บอร์ดแล้วแสดงรากที่สองของตัวเลขนั้น ๆ

In [ ]: num = float(input("Please put a number:"))

print(num**0.5)

Please put a number:64

8.0

You might also like