Chapter 3
Python Data Types
Data types in Python are a way to classify data items. They represent the kind of value, which determines what operations can be performed on that data. Python data types are classes and variables are instances (objects) of these classes. The following are standard or built-in data types in Python:
https://www.geeksforgeeks.org/python/python-data-types/
Example
1. Numeric Data Types
Python numbers represent data that has a numeric value. A numeric value can be an integer, a floating number or even a complex number. These values are defined as int, float and complex classes
Integers:value is represented by int class. It contains positive or negative whole numbers (without fractions or decimals). There is no limit to how long an integer value can be.
Float: value is represented by float class. It is a real number with a floating-point representation. It is specified by a decimal point. Optionally, character e or E followed by a positive or negative integer may be appended to specify scientific notation.
Complex Numbers: It is represented by a complex class. It is specified as (real part) + (imaginary part)j. For example 3+4j
2. Sequence Data Types
A sequence is an ordered collection of items, which can be of similar or different data types. Sequences allow storing of multiple values in an organized and efficient fashion. There are several sequence data types of Python:
2. Sequence Data Types
Python Strings are arrays of bytes representing Unicode characters. In Python, there is no character data type, a character is a string of length one. It is represented by str class.
Strings in Python can be created using single quotes, double quotes or even triple quotes. We can access individual characters of a String using index.
https://www.geeksforgeeks.org/python/python-data-types/
List Data Type
ลิสต์มีความคล้ายคลึงกับอาร์เรย์ที่พบในภาษาโปรแกรมอื่นๆ มันเป็นชุดของรายการที่เรียงลำดับและเปลี่ยนแปลงได้ มีความยืดหยุ่นสูงเนื่องจากรายการในลิสต์ไม่จำเป็นต้องเป็นชนิดเดียวกัน
Creating a List in Python
ในการสร้างลิสต์ใน Python สามารถทำได้โดยการใส่ลำดับไว้ภายในวงเล็บเหลี่ยม [] (square brackets)
Tuple Data Type
ทูเพิล (Tuple) คือกลุ่มของอ็อบเจ็กต์ในภาษา Python ที่เรียงลำดับ ความแตกต่างเพียงอย่างเดียวระหว่างทูเพิลกับลิสต์ (List) คือ ทูเพิลไม่สามารถเปลี่ยนแปลงได้ ไม่สามารถแก้ไขทูเพิลได้หลังจากสร้างขึ้นแล้ว
Creating a Tuple in Python
ในภาษา Python นั้น ทูเพิลถูกสร้างขึ้นโดยการเรียงลำดับค่าต่างๆ โดยคั่นด้วยเครื่องหมายจุลภาค (,) โดยอาจใช้หรือไม่ใช้เครื่องหมายวงเล็บเพื่อจัดกลุ่มข้อมูลก็ได้ ทูเพิลสามารถมีองค์ประกอบได้จำนวนเท่าใดก็ได้ และมีชนิดข้อมูลได้หลากหลาย (เช่น สตริง จำนวนเต็ม รายการ ฯลฯ)
Note:ทูเพิลสามารถสร้างได้ด้วยองค์ประกอบเพียงหนึ่งเดียว แต่ค่อนข้างยุ่งยาก การมีองค์ประกอบเพียงหนึ่งเดียวในวงเล็บไม่เพียงพอ ต้องมีเครื่องหมายจุลภาคปิดท้ายเพื่อให้เป็นทูเพิล
Access Tuple Items
ในการเข้าถึงรายการในทูเปิล ให้ใช้หมายเลขดัชนี ใช้ตัวดำเนินการดัชนี [ ] เพื่อเข้าถึงรายการในทูเปิล
3.Boolean Data Type
ประเภทข้อมูลบูลีนใน Python คือค่าพื้นฐานสองค่า ได้แก่ จริง (True) หรือเท็จ (False) อ็อบเจ็กต์บูลีนที่มีค่าเท่ากับ True เรียกว่า truthy (จริง) และอ็อบเจ็กต์บูลีนที่มีค่าเท่ากับ False เรียกว่า falsy (เท็จ) อย่างไรก็ตาม อ็อบเจ็กต์ที่ไม่ใช่บูลีนก็สามารถประเมินค่าในบริบทของบูลีนได้เช่นกัน และกำหนดว่าเป็นจริงหรือเท็จ โดยใช้สัญลักษณ์คลาส bool แทน