Python Notes Typed
Python Notes Typed
io
password-taashi@1511
VARIABLES
A variable is simply a name to which a value can be assigned.
Variables allow us to give meaningful names to data.
The simplest way to assign a value to a variable is through the = operator.
Yariables are mutable. Hence, the value of variable can always be updated or
replaced.
NAMING CONVENTION
There are certain rules we have to follow when picking the name for a variable:
Spaces are not allowed. Instead, we must use snake_case to make variable names
readable.
The name of the variable should be something meaningful that describes the value it
holds, instead of being random characters.
Numbers
This lesson provides an in-depth discussion about numbers in Python.
It is equipped with support for several types of numbers, along with utilities for
performing computations on them.
svg viewer
Integers
The integer data type is comprised of all the positive and negative whole numbers.
The amount of memory an integer occupies depends on its value. For example, 0 will
take up 24 bytes whereas 1 would occupy 28 bytes.
FLOATING POINT
COMPLEX NUMBERS
Python also supports complex numbers, or numbers made up of a real and an imaginary
part.
Just like the print() statement is used to print values, complex() is used to
create complex numbers.
It requires two values. The first one will be the real part of the complex number,
while the second value will be the imaginary part.
complex(real, imaginary)
BOOLEAN
The Boolean (also known as bool) data type allows us to choose between two values:
true and false.
In Python, we can simply use True or False to represent a bool:
A Boolean is used to determine whether the logic of an expression or a comparison
is correct. It plays a huge role in data comparisons.
STRINGS
A string is a collection of characters closed within single or double quotation
marks.
The length of a string can be found using the len statement. This length indicates
the number of characters in the string:
INDEXING
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. This means that
the index of the first character in a string is 0.
ACCESSING CHARACTERS
Each character in a string can be accessed using its index. The index must be
closed within square brackets, [], and appended to the string.
REVERSING INDEXING
We can also change our indexing convention by using negative indices.
Negative indices start from the opposite end of the string. Hence, the -1 index
corresponds to the last character: