Python Basic Part 1 : Understanding Data Types and Variables
Data Types
A data type is a classification of data that determines the operations that can be performed on it and the types of values it can store. Python has several built-in data types, including:
a. Numeric Data Types : Numeric data types are used to store numeric values. Python has three numeric data types: integers, floating-point numbers, and complex numbers.
a = 19 #integer
b = 40.8 #floating-point number
c = 6 + 23b #complex number
- Integers: Integers are whole numbers that can be positive, negative, or zero. In Python, integers are represented by the
int
data type. For example:x = 19
. - Floating-Point Numbers: Floating-point numbers are decimal numbers that can be represented with a fractional part. In Python, floating-point numbers are represented by the
float
data type. For example:x = 40.8
. - Complex Numbers: Complex numbers are used to represent numbers with real and imaginary parts. In Python, complex numbers are represented by the
complex
data type. For example:x = 6 + 23b
.
b. Text data type : Text data type is used to store text values. In Python, text data is represented by the str
data type. For example: x = "ringo starr"
.
c. Boolean Data Type : Boolean data type is used to represent True or False values. In Python, boolean data is represented by the bool
data type. For example: x = True
.
d. Sequence Data Types : Sequence data types are used to store a collection of values. Python has three sequence data types: lists, tuples, and range objects.
- Lists: Lists are ordered collections of values that can be of any data type. In Python, lists are represented by the
list
data type. For example :
fruits = ['pineapple', 'banana', 'strawberry']
- Tuples : Tuples are ordered collections of values that are immutable, meaning they cannot be changed once created. In Python, tuples are represented by the
tuple
data type. For example :x = (4, 5, 6)
. - Range Objects : Range objects are used to represent a sequence of numbers. In Python, range objects are represented by the
range
data type. For example:x = range(0, 15)
. - Set Data Type: A set is an unordered collection of unique elements. Sets can have elements of different data types.
myset = {4, 5, 6, 7, 8}
e. Mapping Data Types : Mapping data types are used to store key-value pairs. Python has one mapping data type: dictionaries.
- Dictionaries: Dictionaries are unordered collections of key-value pairs. In Python, dictionaries are represented by the
dict
data type. For example:x = {"name": "Johanes", "age": 65}
.
mydict = {'name': 'Johanes', 'age': 65}
Variables
A variable is a named location in memory that is used to store data. In Python, you don’t need to declare a variable before using it. You can simply assign a value to a variable, and Python will automatically create the variable and assign the value to it. For example:
x = 17
y = "Ringo"
print(x)
print(y)
In this example, we’ve created two variables: x
and y
. We've assigned the value 17
to x
, and the value "Ringo"
to y
. Now, we can use these variables in our code. For example :
z = x + 8
print(y + " starr")