Week 2 Python
Week 2 Python
Lecture 2.2
Variables :-
For example :
Lecture 2.3
Dynamic Typing :-
In python, if you declare a variable as integer, it does not stay as integer ; you can
change the variable’s data type to whatever you want.
Lecture 2.4
In Python, there are some words like and, or, not, if, else, for, while,... which are referred
to as 'Keywords'.
For example :
Even though the spellings are the same, the computer treats all three variables as
unique variables. This shows that in Python variable names are case sensitive.
Multiple Assignment :-
Note that the order matters. The following code assigns 1 to the variable x and 2 to the
variable y.
Swapping the values of the variables :-
Deleting a Variable :-
Shorthand Operators :-
Chaining operators :-
Lecture 2.5
Escape characters :-
Backslash (\) :-
There is a way to escape the quotes, (\) This can come in handy when using the
apostrophe symbol in strings with single quotes.
Backslash n (\n) :-
Triple quotes :-
String methods :-
For now, We can assume these ‘methods' as some commands which we can execute
on STRINGS.
‘alpha’ considers both upper as well as lower case letters.
Lecture 2.7
It adds a key to the string and prints the encrypted string as the output -
Code :
Output :
Lecture 2.8
‘if’ statement :-
Code :
In line 7,
If the condition in the ‘if’ block is true then only the statements in line 8 and 9 are
executed.
But if the condition in the ‘if’ block is not true then the statements in line 11 and 12 of
the else-block are evaluated.
Line 14 will always execute.
Output 1:
Output 2:
Lecture 2.9
Code :
Output 1 :
Output 2 :
2. Find whether the given number ends with 0 or 5 or any other number -
Code :
Output 1 :
Output 2 :
Output 3 :
3. Find the grade of the student based on the given marks from 0 to 100 -
Output 1 :
Output 2 :
Python code :
Lecture 2.10
Lecture 2.11
First way :
It imports the whole ‘calendar’ library and to use any function from it we will have to write
‘calender.function_name’
Second way :
Asterisk(*) brings everything inside the calendar library into this python program, now we don't
have to write ‘calendar .’ before any function to use it.
It imports only the ‘month’ function from the ‘calendar’ library
Third way :