Data Types and Variables
Value
Dictionary
Value : A value is one of the fundamental things in any program. A value may be a letter or a number that a program manipulates.
For example,
2
is of numeric data type.Hello, World!
is of sequence data type.
To determine the type of a value, we use a built-in python function type().
1 2 3 4 |
|
Result
1 2 3 4 |
|
In the 4th statement, we have enclosed 3.2 in quotation marks '. Thus the floating-point number 3.2 is treated as a string.
Variable
Dictionary
Variable : a name to which a value can be assigned.
Variables allow us to give a meaningful name to a value(data).
The assignment statement creates new variables and gives them values. The simplest way to assign a value to a variable is through an =
operator.
A big advantage of variables is that they allow us to store data and can later be used to perform operations or manipulate the data of the variable in the code.
Info
Variables are mutable(something that is changeable or has the ability to change).
In Python, the value of a variable can always be updated or replaced.
Info
In Python, everything is treated as an object.
Every object has these three attributes:
- Identity - refers to the address of an object in a computer’s memory.
- Type - refers to the kind of object that is created. For example – integer, dictionary, list, string, etc.
- Value - refers to the data stored in the object. For example – num_list=[1,2,3] would hold the numbers 1,2 and 3.
While identity
and type
cannot be changed once it’s created, values
can be changed for mutable objects.
Naming Convention
There are some rules we need to follow when picking the name for a variable:
- The first character must start with an UPPERCASE or LOWERCASE letter of alphabet or UNDERSCORE(_)
- Rest of the name can consist of UPPERCASE or LOWERCASE letter of alphabet or UNDERSCORE(_) or DIGITS(0-9)
- Spaces are not allowed. Instead, use
snake_case
to make variable names readable. - Names are case-sensitive.
myworld
andmyWorld
are not the same. - The name of the variable should be something meaningful that describes the value it holds, instead of being random characters.
Data Type
Dictionary
Data Type : The data type of a value defines the type and range of values that data can have.
Python language provides 3 main data types:
- Numeric type
- Booleans
- Sequence type
Numeric Data Type
The numeric data type represents the data that has a numeric value.
Integers
The integer data type is comprised of all the positive and negative whole numbers.
Info
0 takes up 24 bytes whereas 1 occupies 28 bytes.
1 2 3 4 5 6 7 8 |
|
Result
1 2 3 4 |
|
Floating Point Numbers
Floating-point numbers, or floats, refer to positive and negative decimal numbers.
Info
A float point occupies 24 bytes of memory.
1 2 3 4 5 |
|
Result
1 2 3 |
|
Complex Numbers
Python supports numbers made up of real part and imaginary part, which is specified as \((real\_part) + (imaginary\_part)j\).
Built-in function complex()
is used to create complex numbers.
Info
A complex number usually takes up 32 bytes of memory.
1 2 3 4 5 6 7 |
|
Result
1 2 3 4 |
|
Complex numbers are useful for modeling physics and electrical engineering models in Python.
Booleans
The Boolean(bool) data type allows us to choose between two values: true
and false
. It is used to determine whether the logic of an expression or a comparison is correct.
Info
The first letter of a bool needs to be capitalized in Python.
1 2 3 4 |
|
Result
1 2 |
|
Compare two values, the expression is evaluated and Python returns the Boolean answer.
1 2 3 |
|
Result
1 2 3 |
|
0 is considered as False and 1 as True.
1 2 3 4 5 |
|
Result
1 2 |
|
Sequence Data Type
A string is a collection of characters or a sequence of bytes representing Unicode characters enclosed within single '
or double "
quotation marks.
Tip
In python there is no character data type. A character is a string of length 1.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Result
1 2 3 4 5 |
|
Length of string
We can use built-in function len()
to get the length of a string.
1 2 |
|
Result
1 |
|
Indexing and Accessing Characters
In a string, every character is given a numerical index based on its position.
A string in Python is indexed from 0 to n-1 where n is its length.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Result
1 2 3 4 5 6 |
|
If we try to execute the 13th statement, we would get an error because the maximum index is len(cap_america) - 1 which is 11, and we are trying to access the 12th index of the variable.
We can also use negative indices. For example, -1
will corrresponds to the last index.
1 2 3 4 |
|
Result
1 2 |
|
String Slicing
Dictionary
Slicing : is the process of obtaining a portion or substring of a string by using its indices.
To obtain a substring we can use: \(string[start:end]\)
1 2 3 4 5 |
|
Result
1 2 3 |
|
Slicing using Step : We can also define a step to skip characters in the string using \(string[start:end:step]\)
1 2 3 4 5 |
|
Result
1 2 3 |
|
Reverse Slicing : Strings can also be sliced to return a reversed substring.
1 2 3 4 |
|
Result
1 2 |
|
Partial Slicing : Specifying the start and end are optional.
1 2 3 4 5 6 |
|
Result
1 2 3 4 |
|
Type Conversions
There may be times when we need to change data from one type to another. When the data type is manually changed by the user as per their requirement we call it Explicit Type Conversion.
Info
There are built-in functions that allow us to perform such explicit type conversions. More about functions in the Functions module.
int()
Convert data into an integer.
1 2 3 4 5 |
|
Result
1 2 3 4 5 6 |
|
ord()
Character to Unicode value. ord()
expects character value.
1 2 |
|
Result
1 2 |
|
float()
Converts data into floating-point numbers.
1 2 3 |
|
Result
1 2 3 |
|
str()
Converts data into a string.
1 2 3 |
|
Result
1 2 3 |
|
bool()
Converts data into a boolean value.
Tip
Strings are always converted to True
, except if it is empty.
Floats and integers with a value of zero are considered to be False
.
1 2 3 4 |
|
Result
1 2 3 4 |
|
There are other conversions as well, which we will cover in later series.
In the next section, we will start with Operators.