You are on page 1of 20

Week 2

Lecture 2.2

Variables :-

Instructions for writing reader-friendly variables -

1. Make the variable self-explanatory.


2. Add comments.

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.

Python provides us flexibility to use variables.


Another example of dynamic typing :-

Lecture 2.4

More on Variables, Operators and Expressions :-

In Python, there are some words like and, or, not, if, else, for, while,... which are referred
to as 'Keywords'.

We can not use these keywords as variable names.

For example :

Rules for writing variable names :-


1. Variable names can contain alphanumeric (all alphabets from A-Z in lower and
uppercase and numbers 0-9) characters and underscores.
2. We can start a variable name with an alphabet or an underscore but we cannot
start it with a number.
3. Variable names are case sensitive.

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 :-

We can use shorthand operators with other arithmetic operators as well.


in Operator :-

It checks if the string 'alpha' is in the next string or not.

The result of the ‘ in’ operator is always a boolean value.

Chaining operators :-
Lecture 2.5

Escape characters :-

Backslash (\) :-

We want to print ‘It's a beautiful day.’ We can do this in following ways :-

In [20], the backslash (\) is called the escape character.

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) :-

\n is a newline character. Its effect is to introduce a new line


Backslash t (\t) :-

Triple quotes :-

We can use triple quotes to :


1. Store multiple line strings.
2. To comment out more than 1 line.
Lecture 2.6

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

Caesar-Cipher : More on strings

It adds a key to the string and prints the encrypted string as the output -

Code :
Output :

Lecture 2.8

‘if’ statement :-

Let us consider the movie 'Avengers'. This is a 13+ movie.

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

if,else and else-if(elif) conditions :-

1. Find whether the given number is even or odd -

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 :

4. convert the given flowchart into python code -

Python code :
Lecture 2.10

Library functions in python :-

A library is a collection of functions that share a common theme.


It will import the whole library of ‘math’ and we can use any ‘function’ from it.

It imports the ‘random’ library

Let us simulate a coin toss -

Let us simulate a six faced dice -


Let us simulate the sum of 2 six faced die -

Lecture 2.11

Different ways to import library : -

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 :

Using ‘as’ while importing libraries

You might also like