Python Basics¶
1. Constants¶
To do anything in a program, we need constants. Any value that remains the same throughout the execution of the program is known as a constant. Quite literally, it is a constant. For example, 10
is a constant. Every number is a constant.
2. Variables¶
Variables are like boxes that are used to store constants. Variables are values that can change during the execution of the program! Think about it this way, if a variable is a box that stores a constant, that constant can be taken out and another one can be put in. Quite literally, it is a variable (it may change!). For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
You can also store sentences, words:
1 2 |
|
You can store lists as well:
1 2 |
|
3. Data Types¶
Remember, variables are like boxes/containers that are used to store constants. For every different kind of constant, a different type of contanier/box is required! Think about it this way, you cannot store water in a paper bag. you need a water bottle to store water. Similarly, in python, each different kind of constant must be stored in a different type of variable. The type of the variable is known as its data type. So how many kinds of boxes, or data types are there? The following are the most commonly used data types in python:
3.1. Integer (int
)¶
An int
quantity may be negative, positive, or zero. It never has any fractional or decimal parts (for example, an integer never has 0.5
).
Syntax: a = 10
This declares a variable of type int
called a
with a value of 10
3.2. Floating Point (float
)¶
A float
is a data type that can store values with fractional parts or decimals. For example 1.5
is a floating point value.
Syntax: a = 3.14159
This declares a variable of type float
called a
with a value of 3.14159
3.3. Boolean (bool
)¶
A bool
is a data type, that can only store one of two different values, True
or False
. Any yes or no question is a boolean question in some sense, because there are only two answers, yes or no (true or false). Boolean variables are super important for conditions, which we will be looking at later.
Syntax: a = True
This declares a variable of type bool
called a
with a value of True
Info
A value of True
can also be indicated by 1
and a value of False
can also be indicated by 0
.
3.4. String (str
)¶
An str
is a word, a phrase or a sentence. A string is always enclosed by double quotes.
Syntax: a = "this is a string! yay"
This declares a variable of type str
called a
with a value of "this is a string! yay"
.
You can also use single quotes for declaring strings:
a = 'this is a string! yay'
String with multiple lines?
You might be tempted to do something like this:
1 2 |
|
But there is a better way:
1 2 3 4 5 6 7 8 9 10 11 |
|
what if you want to obtain the letter in a particular position in a string?
1 2 3 4 5 6 7 8 9 10 |
|
What if you want to obtain a particular part of the string?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
You can also join two or more strings togethere:
1 2 3 4 |
|
What would happen if you tried to access a poition of the string that is greater than its length?
1 2 3 4 |
|
3.5. List (list
)¶
A list
is an ordered collection of different constants. Each member of a list is formally called an element of that list.
Example:
1 |
|
This declares a variable of type list
called a
with the following values:
Index | Value |
---|---|
0 | 2 |
1 | 3.4 |
2 | "this is a list" |
3 | True |
4 | ["this list is inside the first one", ":O"] |
Also, in general when initialising a list, if the list is too long to fit in one line, it is common to break it up over multiple lines to increase visibility, like so:
1 2 3 4 5 6 7 |
|
To obtain an element from a list, the same syntax as a string is used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
you can combine lists as follows:
1 2 3 4 5 |
|
you can add an element to a list using the append function:
1 2 3 |
|
you can extend a list using another list by using the extend function:
1 2 3 4 |
|
What would happen if you tried to access an element of the list whoes index is greater than the length?
1 2 3 4 |
|
3.6. Tuple (tuple
)¶
A tuple
is exactly like a list. The only difference is that once a tuple is initialised, its elements cannot be altered. Technically, we say that a tuple is immutable.
Example:
1 |
|
Similar to a list, it is common to break up a long tuple over multiple lines:
1 2 3 4 5 6 7 8 |
|
This declares a variable of type tuple
called a
with the following values:
Index | Value |
---|---|
0 | 2 |
1 | 3.4 |
2 | "this is a tuple" |
3 | True |
4 | ("this tuple is inside the first one", ":O") |
5 | ["this is a list inside the tuple", "o.O"] |
To obtain an element from a tuple, the same syntax as a list/string is used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
3.7. Dictionary (dict
)¶
A dict
is a data type that can store "key-value" pairs. It essentially creates a map between specifed values.
The constants to the left are called keys, and the constants to the right are called values.
Example:
1 2 3 4 5 6 |
|
Best Practice
like a list or a tuple, you could write the above out in one line:
1 |
|
But that is so much worse for readability! Dicts are conventionally almost always never written like that, its always a good idea to write each key-value pair of a dict out on a separate line
So how do you actually use a dictionary?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Remember that your variable names can be almost anything! However, this does not mean that you should just use single letters or alphabets for variable names. Variable names should be chosen such that they represent, or hint to what the purpose of the variable is. Naming your variables such that they make intuitive sense is a good programming practise.
4. Variable Naming Rules and Conventions:¶
There are some rules that must be followed when naming a variable. They are:
-
You can only use letters, numbers and underscores to name your variables.
-
A variable name cannot start with a number.
-
Variable names are
CaSe SeNsItIvE
. This means thata
andA
are two different variable names! -
Variable names must not be keywords. A keyword is a reserved word in any programming language that has a special meaning! For example,
int
is a keyword in Python because it is the name of a data type.
While these are the only laws that you absolutely must follow, there are some conventions or unwritten rules that all programmers agree to follow to make their code more readable, and more clear.
-
When you are writing a variable name that is just one word, it is conventional to write it in all small letters. For example
radius = 10
orname = "John Cena"
. -
When you are writing a variable name that consists of more than one word, then it is conventional to write it in a special case known as "snake case". Snake case is where the every word is written in small letters separated by underscores. For Example:
gear_ratio = 2.2
orfirst_name = "Bruce"
orlast_name = "Wayne"
. -
When you are writing a variable that is supposed to just store the value of a constant, one which you never intend to change, it is conventional to use capital letters and words are separated by underscores. For example:
PI = 3.14159
orGOLDEN_RATIO = 1.61803
. -
Variable names should be precise and mnemonic. That is, they should indicate to a casual programmer their purpose. Usage of single letter variable names is discouraged unless it is a throwaway or temporary variable.