Data Types and Variables
By: Mustafif Khan
Language: Python
Data Types and Variables
Python has several built-in data types that can store different kinds of values. Some of the most common data types are:
-
str
: a string is a sequence of characters enclosed in quotes.- For example,
name = "Alice"
assigns a string value to the variablename
.
- For example,
-
int
: an integer is a whole number without a decimal point.- For example,
age = 21
assigns an integer value to the variableage
.
- For example,
-
float
: a float is a number with a decimal point.- For example,
pi = 3.14
assigns a float value to the variablepi
.
- For example,
-
bool
: a boolean is a logical value that can be either True or False.- For example, flag = True assigns a boolean value to the variable flag.
-
list
: a list is an ordered collection of values that can be of different types.- For example,
colors = ["red", "green", "blue"]
assigns a list value to the variablecolors
.
- For example,
-
tuple
: a tuple is an immutable ordered collection of values that can be of different types.- For example,
point = (1, 2)
assigns a tuple value to the variablepoint
.
- For example,
-
dict
: a dictionary is an unordered collection of key-value pairs that can be of different types.- For example,
person = {"name": "Bob", "age": 25}
assigns a dictionary value to the variableperson
.
- For example,
-
set
: a set is an unordered collection of unique values that can be of different types.- For example,
letters = {"a", "b", "c"}
assigns a set value to the variableletters
.
- For example,
-
Python does not require explicit declaration of variables. A variable is created when a value is assigned to it using the
=
operator. For example,x = 5
creates a variable namedx
and assigns it the value5
. -
Python variables are dynamically typed, which means that their data type is determined at runtime based on the type of the value assigned to them. For example, if you assign a string value to a variable that previously had an integer value, the data type of the variable will change from
int
tostr
. For example,x = 5
makesx
anint
, butx = "hello"
makesx
astr
. -
You can check the data type of any variable or value by using the built-in function
type()
. For example,type(x)
will return the data type ofx
. -
You can also convert one data type to another by using the built-in functions that correspond to each data type. For example,
int(x)
will convertx
to anint
, andstr(x)
will convertx
to astr
.