Basics of Python: Part II


Bikash Santra

Indian Statistical Institute, Kolkata


6. Conditional statements: if-elif-else

%%% If-else block If this happens: what is the result? Else: what is the result? %%%% Nested if-else block If this happens: what is the result? Else if this happens: what is the result? Else if this happens: what is the result? Else: what is the result? %%%% Else-if ladder If this happens: If this happens: If this happens: what is the result? Else: what is the result? Else: what is the result? Else: what is the result?

Exercise 1: Write a program that takes an integer as input, and prints whether the number is odd or even.

7. Python indentations

a) There are no braces {} in Python to demarcate blocks of code
b) Indentations (whitespaces) demarcate blocks of code
c) Changes in indentation mark where a block begins, and where it ends
d) A common convention: 4 spaces (or 1 tab, which the editor converts to 4 spaces)

8. Loops

Exercise: Do these programs using for and while loops both 1) Write a program to print the odd integers upto 10,000.

2) Then break the loop when the odd number is greater than 1000.

9. break, continue

Excercise: press 1 for add, 2 for division, 3 for multiplication, 4 for substraction

user asks that I want to execute add/division

12. Data Structures: Lists, Tuples, Sets and Dictionaries

12.1 List: Mutable array that can store any object

Copying Caution!

12.2 Tuples: Immutable arrays that can store any object

12.3 Set: No duplicates

12.4 Dictionaries: Map keys to values

Looping Time!

Exercise: Using list comprehension technique, store the squares of the numbers starting from 1 to 20 in a list

10. Functions

Exercise: Write to program to make a calculator using function. Ask users to put the name of the operation that they want to perform. Next ask them to give the numbers for performing their chosen operations. Finall dispaly the result.

Principles of Object Oriented Programming

a) Abstraction
b) Encapsulation
c) Inheritance
d) Polymorphism

11. Classes

13. File I/O

14. String functions

14. Regular functions