Outline Introduction Data Types Operators and Expressions Control Flow Problems

Computing Laboratory Basics of Python - I

Malay Bhattacharyya

Assistant Professor

Machine Intelligence Unit Indian Statistical Institute, Kolkata December, 2020

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems

1 Introduction

2 Data Types

3 Operators and Expressions

4 Control Flow

5 Problems

Malay Bhattacharyya Computing Laboratory Python is an interpreted language. Python is not a free-form language. Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language. Python is a portable and cross-platform language. Python is an extensible language.

Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in .

Malay Bhattacharyya Computing Laboratory Python is not a free-form language. Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language. Python is a portable and cross-platform language. Python is an extensible language.

Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language.

Malay Bhattacharyya Computing Laboratory Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language. Python is a portable and cross-platform language. Python is an extensible language.

Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language. Python is not a free-form language.

Malay Bhattacharyya Computing Laboratory Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language. Python is a portable and cross-platform language. Python is an extensible language.

Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language. Python is not a free-form language. Python is a strongly typed language.

Malay Bhattacharyya Computing Laboratory Python is a high level language. Python is a portable and cross-platform language. Python is an extensible language.

Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language. Python is not a free-form language. Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming.

Malay Bhattacharyya Computing Laboratory Python is a portable and cross-platform language. Python is an extensible language.

Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language. Python is not a free-form language. Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language.

Malay Bhattacharyya Computing Laboratory Python is an extensible language.

Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language. Python is not a free-form language. Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language. Python is a portable and cross-platform language.

Malay Bhattacharyya Computing Laboratory Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language. Python is not a free-form language. Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language. Python is a portable and cross-platform language. Python is an extensible language.

Malay Bhattacharyya Computing Laboratory Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language. Python is not a free-form language. Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language. Python is a portable and cross-platform language. Python is an extensible language.

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Basic characteristics

Python is a free and open source language, first written in C. Python is an interpreted language. Python is not a free-form language. Python is a strongly typed language. Python is an object-oriented language but it also supports procedural oriented programming. Python is a high level language. Python is a portable and cross-platform language. Python is an extensible language.

Note: The reference implementation CPython is now available on GitHub (see: https://github.com/python/cpython). This is no more updated.

Malay Bhattacharyya Computing Laboratory Looking into the source code:

import inspect inspect.getfullargspec() inspect.getsource()

Note: The official Python source releases are available at: https://www.python.org/downloads/source.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Knowing Python in a better way

Looking into the help manual:

help() help()

Malay Bhattacharyya Computing Laboratory Note: The official Python source releases are available at: https://www.python.org/downloads/source.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Knowing Python in a better way

Looking into the help manual:

help() help()

Looking into the source code:

import inspect inspect.getfullargspec() inspect.getsource()

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Knowing Python in a better way

Looking into the help manual:

help() help()

Looking into the source code:

import inspect inspect.getfullargspec() inspect.getsource()

Note: The official Python source releases are available at: https://www.python.org/downloads/source.

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems The data types in Python

Malay Bhattacharyya Computing Laboratory Output: fun

Splitting a string:

str = "I don’t love Python, the snake." print(str.split(" "), str.split(", "))

Output: [’I’, ”don’t”, ’love’, ’Python,’, ’the’, ’snake.’] [”I don’t love Python”, ’the snake.’]

Outline Introduction Data Types Operators and Expressions Control Flow Problems Strings

Substring of a string:

str = "malfunctioning" # Indexing starts with 0 print(str[3:6]) # Elements indexed 3 through 5

Malay Bhattacharyya Computing Laboratory Splitting a string:

str = "I don’t love Python, the snake." print(str.split(" "), str.split(", "))

Output: [’I’, ”don’t”, ’love’, ’Python,’, ’the’, ’snake.’] [”I don’t love Python”, ’the snake.’]

Outline Introduction Data Types Operators and Expressions Control Flow Problems Strings

Substring of a string:

str = "malfunctioning" # Indexing starts with 0 print(str[3:6]) # Elements indexed 3 through 5

Output: fun

Malay Bhattacharyya Computing Laboratory Output: [’I’, ”don’t”, ’love’, ’Python,’, ’the’, ’snake.’] [”I don’t love Python”, ’the snake.’]

Outline Introduction Data Types Operators and Expressions Control Flow Problems Strings

Substring of a string:

str = "malfunctioning" # Indexing starts with 0 print(str[3:6]) # Elements indexed 3 through 5

Output: fun

Splitting a string:

str = "I don’t love Python, the snake." print(str.split(" "), str.split(", "))

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Strings

Substring of a string:

str = "malfunctioning" # Indexing starts with 0 print(str[3:6]) # Elements indexed 3 through 5

Output: fun

Splitting a string:

str = "I don’t love Python, the snake." print(str.split(" "), str.split(", "))

Output: [’I’, ”don’t”, ’love’, ’Python,’, ’the’, ’snake.’] [”I don’t love Python”, ’the snake.’]

Malay Bhattacharyya Computing Laboratory Do not use commas as thousand-separators. At times behavior may be counter-intuitive.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Numbers

Real values including integers

Examples: 1.2345 3.45e67 1. +3.45e67 .1 -3.45e-67 -0.6789 .00345e-32 +.4560 1e-15 -.1234 1e+15

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Numbers

Real values including integers

Examples: Do not use commas 1.2345 3.45e67 as 1. +3.45e67 thousand-separators. .1 -3.45e-67 At times behavior -0.6789 .00345e-32 may be +.4560 1e-15 counter-intuitive. -.1234 1e+15

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists

Length of the list = n. n linked memory locations that gets dynamically reallocated. The elements are homogeneous but the type is generic. Elements can be mapped to each of the n memory locations. Elements are indexed 0 through n − 1 (≡ −n through −1).

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists – Adding elements

ls = [] ls.append() # Inserts at the end ls.insert(, ) # Inserts at the

Malay Bhattacharyya Computing Laboratory [1, 2, 3, 20, 4, 5] [1, 2, 3, 20, 4, 5, 3]

Note: If the list insertion index is out of range then the maximum possible range is taken.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists – Adding elements

What will be the output of the following program?

ls = [1, 2, 3, 4, 5] ls.insert(3, 20) # Inserts 20 at index 3 print(ls) ls.insert(20, 3) # Inserts 3 at index 6 (not at index 20) print(ls)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists – Adding elements

What will be the output of the following program?

ls = [1, 2, 3, 4, 5] ls.insert(3, 20) # Inserts 20 at index 3 print(ls) ls.insert(20, 3) # Inserts 3 at index 6 (not at index 20) print(ls)

[1, 2, 3, 20, 4, 5] [1, 2, 3, 20, 4, 5, 3]

Note: If the list insertion index is out of range then the maximum possible range is taken.

Malay Bhattacharyya Computing Laboratory [1, 2, 3, 10, 5] Error

Note: List assignment index cannot be out of range.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists – Adding elements

What will be the output of the following program?

ls = [1, 2, 3, 4, 5] ls[3] = 10 # Assigns 10 at index 3 print(ls) ls[10] = 3 # Assigns 3 at index 10 print(ls)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists – Adding elements

What will be the output of the following program?

ls = [1, 2, 3, 4, 5] ls[3] = 10 # Assigns 10 at index 3 print(ls) ls[10] = 3 # Assigns 3 at index 10 print(ls)

[1, 2, 3, 10, 5] Error

Note: List assignment index cannot be out of range.

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists – Removing elements

ls = [] = ls.pop()

Malay Bhattacharyya Computing Laboratory [[10], [20], [], 30] [[10, 20], [10, 20], [10, 20], 30]

Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists of sublists

What will be the output of the following program?

ls1 = [[] for i in range(3)] ls1[0].append(10) ls1[1].append(20) ls1.append(30) print(ls1) ls2 = [[]]*3 ls2[0].append(10) ls2[1].append(20) ls2.append(30) print(ls2)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Lists of sublists

What will be the output of the following program?

ls1 = [[] for i in range(3)] ls1[0].append(10) ls1[1].append(20) ls1.append(30) print(ls1) ls2 = [[]]*3 ls2[0].append(10) ls2[1].append(20) ls2.append(30) print(ls2)

[[10], [20], [], 30] [[10, 20], [10, 20], [10, 20], 30]

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Converting Strings to Lists

Strings are just like Lists. One can convert a String into the List by passing the String as an argument to the List as follows.

str = "Python 3" list(str)

Output: [’P’, ’y’, ’t’, ’h’, ’o’, ’n’, ’ ’, ’3’]

Malay Bhattacharyya Computing Laboratory Note: The expressions like “x = 0” or “x = 1” will exhibit error.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Boolean values

Any non-zero value is treated as True and zero is treated as False.

Examples: 0 False 0e10 False 1 True ’A’ True 6 - 2 * 3 False ”A” True (6 - 2) * 3 True ’\0’ True 0.0075 True (0, 0) True

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Boolean values

Any non-zero value is treated as True and zero is treated as False.

Examples: 0 False 0e10 False 1 True ’A’ True 6 - 2 * 3 False ”A” True (6 - 2) * 3 True ’\0’ True 0.0075 True (0, 0) True

Note: The expressions like “x = 0” or “x = 1” will exhibit error.

Malay Bhattacharyya Computing Laboratory A mutable object can be changed to a different type after it is created, but an immutable object cannot be changed.

Objects of built-in types like int, float, bool, str, tuple, unicode are immutable Objects of built-in types like list, set, dict are mutable.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Mutable and immutable objects

Everything in Python is an object and it is either mutable or immutable.

Malay Bhattacharyya Computing Laboratory Objects of built-in types like int, float, bool, str, tuple, unicode are immutable Objects of built-in types like list, set, dict are mutable.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Mutable and immutable objects

Everything in Python is an object and it is either mutable or immutable.

A mutable object can be changed to a different type after it is created, but an immutable object cannot be changed.

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Mutable and immutable objects

Everything in Python is an object and it is either mutable or immutable.

A mutable object can be changed to a different type after it is created, but an immutable object cannot be changed.

Objects of built-in types like int, float, bool, str, tuple, unicode are immutable Objects of built-in types like list, set, dict are mutable.

Malay Bhattacharyya Computing Laboratory Note: Modulo division operator works on any type of numbers (including floating point values and negatives!!!) and returns the sign of the denominator.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Arithmetic operators

Summation (+) Subtraction (−) Multiplication (∗) Power (∗∗) Float Division (/) Floor Division (//) Modulo division (%)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Arithmetic operators

Summation (+) Subtraction (−) Multiplication (∗) Power (∗∗) Float Division (/) Floor Division (//) Modulo division (%)

Note: Modulo division operator works on any type of numbers (including floating point values and negatives!!!) and returns the sign of the denominator.

Malay Bhattacharyya Computing Laboratory 1.2999999999999998 0.5 0.7999999999999998 1

Note: x%y returns r (following IEEE 754, the IEEE Standard for floating-point arithmetic) if and only if x = n ∗ y + r, where n is an integer, r has the same sign as y, and |r| < |y|.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Modulo division on floating point values

What will be the output of the following program?

print(4.3 % 3) print(4 % 3.5) print(4.3 % 3.5) print(4 % 3)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Modulo division on floating point values

What will be the output of the following program?

print(4.3 % 3) print(4 % 3.5) print(4.3 % 3.5) print(4 % 3)

1.2999999999999998 0.5 0.7999999999999998 1

Note: x%y returns r (following IEEE 754, the IEEE Standard for floating-point arithmetic) if and only if x = n ∗ y + r, where n is an integer, r has the same sign as y, and |r| < |y|.

Malay Bhattacharyya Computing Laboratory 0 Error 0 Error

Outline Introduction Data Types Operators and Expressions Control Flow Problems Modulo division on Boolean values

What will be the output of the following program?

var1 = True var2 = False print(var1 % var1) print(var1 % var2) print(var2 % var1) print(var2 % var2)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Modulo division on Boolean values

What will be the output of the following program?

var1 = True var2 = False print(var1 % var1) print(var1 % var2) print(var2 % var1) print(var2 % var2)

0 Error 0 Error

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Relational operators

Less than (<) Less than equals to (<=) Greater than (>) Greater than equals to (>=) Equals to (==) Not equals to (! =)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Logical operators

Logical and (and) Logical or (or) Logical not (not)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Assignment operators

Assignment (=) Addition and assignment (+ =) Subtraction and assignment (− =) Multiplication and assignment (∗ =) Power and assignment (∗∗ =) Float Division and assignment (/ =) Floor Division and assignment (// =) Modulo division and assignment (% =)

Malay Bhattacharyya Computing Laboratory 10 20 0x72fca540 0x72fca8e0 20 10 0x72fca8e0 0x72fca540 # Only the pointers will change!!!

Outline Introduction Data Types Operators and Expressions Control Flow Problems Assignment operators

What will be the output of the following program?

a = 10 b = 20 print(a, b) print(hex(id(a)), hex(id(b))) a, b = b, a # Swapping values print(a, b) print(hex(id(a)), hex(id(b)))

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Assignment operators

What will be the output of the following program?

a = 10 b = 20 print(a, b) print(hex(id(a)), hex(id(b))) a, b = b, a # Swapping values print(a, b) print(hex(id(a)), hex(id(b)))

10 20 0x72fca540 0x72fca8e0 20 10 0x72fca8e0 0x72fca540 # Only the pointers will change!!!

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Bitwise operators

Bitwise and (&) Bitwise or (|) Bitwise not (∼) Bitwise xor (∧) Left shift (<<) Right shift (>>)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Efficiency of bitwise operators

Verifying whether a number is a power of 2:

n = 32 if n & n-1: print(n, "is not a power of 2") else: print(n, "is a power of 2")

Output: 32 is a power of 2

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Efficiency of bitwise operators

Verifying similarity of signs:

m = 10 n = -20 if m ^ n < 0: print(m, " and ", n, " have different signs"); else: print(m, " and ", n, " have the same signs");

Output: 10 and -20 have different signs

Malay Bhattacharyya Computing Laboratory Note: Two variables that are equal does not imply that they are identical. For being identical, they must be located on the same part of the memory.

Outline Introduction Data Types Operators and Expressions Control Flow Problems Identity operators

Identical (is) Not identical (is not)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Identity operators

Identical (is) Not identical (is not)

Note: Two variables that are equal does not imply that they are identical. For being identical, they must be located on the same part of the memory.

Malay Bhattacharyya Computing Laboratory True False False

Outline Introduction Data Types Operators and Expressions Control Flow Problems Identity operators

What will be the output of the following program?

var1 = 123 var2 = 123 print(var1 is var2) var1 = ‘Python’ var2 = ‘Python’ print(var1 is not var2) var1 = [1, 2, 3] # List of immutable objects var2 = [1, 2, 3] # List of immutable objects print(var1 is var2)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Identity operators

What will be the output of the following program?

var1 = 123 var2 = 123 print(var1 is var2) var1 = ‘Python’ var2 = ‘Python’ print(var1 is not var2) var1 = [1, 2, 3] # List of immutable objects var2 = [1, 2, 3] # List of immutable objects print(var1 is var2)

True False False

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Some comments

The following popular operators are not available in Python: Increment (++) Decrement (- -) Comma (,)

Malay Bhattacharyya Computing Laboratory (20, 30, 40) (50, 60, 70)

Outline Introduction Data Types Operators and Expressions Control Flow Problems Use of comma

What will be the output of the following program?

var = 20, 30, 40 print(var) var = (50, 60, 70) print(var)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Use of comma

What will be the output of the following program?

var = 20, 30, 40 print(var) var = (50, 60, 70) print(var)

(20, 30, 40) (50, 60, 70)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Operator precedence (highest to lowest)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Conditional – if-else

if : statement 1 statement 2 else: statement 3 # Execute if Condition fails

Note: Boundary of the conditional block is demarcated by indentation.

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Iterative – if-elif-else

if : statement 1 elif : Statement 2 else: statement 3 # Execute if Condition 1 and 2 fails

Note: Boundary of the conditional block is demarcated by indentation.

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Iterative –

for in : statement 1 statement 2

Note: Boundary of the iterative block is demarcated by indentation.

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Iterative – for loop

We can create a list of consecutive integers using the range() function as follows.

for in range(): statement 1 statement 2

range(x) returns a list whose items are consecutive integers from [0, x). range(x, y) returns a list (feasible when x < y) whose items are consecutive integers from [x, y). range(x, y, step) returns a list of integers from [x, y), such that the difference between each two adjacent items in the list is step. If step is less than 0, it counts down from x to y. If step equals 0, it raises an exception.

Malay Bhattacharyya Computing Laboratory Output: [0, 2, 4, 6, 8, 10]

list = [i*2 for i in range(6)] print(list)

Output: [0, 2, 4, 6, 8, 10]

Outline Introduction Data Types Operators and Expressions Control Flow Problems Efficient for loop

list = [] for i in range(6): list.append(i*2) print(list)

Malay Bhattacharyya Computing Laboratory list = [i*2 for i in range(6)] print(list)

Output: [0, 2, 4, 6, 8, 10]

Outline Introduction Data Types Operators and Expressions Control Flow Problems Efficient for loop

list = [] for i in range(6): list.append(i*2) print(list)

Output: [0, 2, 4, 6, 8, 10]

Malay Bhattacharyya Computing Laboratory Output: [0, 2, 4, 6, 8, 10]

Outline Introduction Data Types Operators and Expressions Control Flow Problems Efficient for loop

list = [] for i in range(6): list.append(i*2) print(list)

Output: [0, 2, 4, 6, 8, 10]

list = [i*2 for i in range(6)] print(list)

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Efficient for loop

list = [] for i in range(6): list.append(i*2) print(list)

Output: [0, 2, 4, 6, 8, 10]

list = [i*2 for i in range(6)] print(list)

Output: [0, 2, 4, 6, 8, 10]

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Iterative – while loop

while : statement 1 statement 2

Note: Boundary of the iterative block is demarcated by indentation.

Malay Bhattacharyya Computing Laboratory i = 0 for i in range(1, 100): while i < 100: print(i) i = i+1 if i%10 != 0: if i%10 != 0: break continue print(i) Prints only 1 Prints 10, 20, ..., 100

Outline Introduction Data Types Operators and Expressions Control Flow Problems break and continue

break: Immediately jump to the next operation after the loop continue: Do the operation, if applicable, and continue with the next iteration of the loop

Malay Bhattacharyya Computing Laboratory i = 0 while i < 100: i = i+1 if i%10 != 0: continue print(i)

Prints 10, 20, ..., 100

Outline Introduction Data Types Operators and Expressions Control Flow Problems break and continue

break: Immediately jump to the next operation after the loop continue: Do the operation, if applicable, and continue with the next iteration of the loop

for i in range(1, 100): print(i) if i%10 != 0: break

Prints only 1

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems break and continue

break: Immediately jump to the next operation after the loop continue: Do the operation, if applicable, and continue with the next iteration of the loop

i = 0 for i in range(1, 100): while i < 100: print(i) i = i+1 if i%10 != 0: if i%10 != 0: break continue print(i) Prints only 1 Prints 10, 20, ..., 100

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Problems – Day 2

1 Given a positive integer n as user input, find out the number of trailing zeros in n!. Note: This can be done with log5 n number of divisions. 2 Suppose the version number of a software is denoted as x1.x2. ··· .xn where x1, x2, ..., and xn denote the version number, subversion number, and so on, respectively. E.g., GCC 9.1 is a version of GNU C Compiler. Consider that xi ∈ [0, 9] for all i. Given the lower and upper bound of version numbers of a software and n (the number of components of a version number) as user inputs, list up all the possible versions that might exist within the given range (inclusive).

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Problems – Day 2

3 An n-digit number is SPECIAL if the addition of its sum of the digits and the product of its digits equals to the original number. E.g., 19 is a SPECIAL 2-digit number. Write a program to verify whether a given number is SPECIAL or not. Extend this program to verify whether there exists any SPECIAL number for a given value of number of digits n. 4 Consider an n-digit number. Square it and add the right n digits to the left n or n − 1 digits. If the resultant sum is same as the original number, then it is called a Kaprekar number. E.g., 45 is a Kaprekar number. Write a program to verify whether a given number is Kaprekar or not.

Malay Bhattacharyya Computing Laboratory Outline Introduction Data Types Operators and Expressions Control Flow Problems Problems – Day 2

5 Let us define a string, comprising English alphabets, as NICE if each vowel within it are equidistant from its successor and predecessor vowel, if applicable. E.g., “rhythm”, “cool”, “malayalam” are NICE strings. Write a program to verify whether a given string is NICE or not. You are required to take the string as a direct input without asking for its length. 6 Write a program to print the following pattern using generic controls over print. Let the line number be user input. ** ** ** * ** ** **

Malay Bhattacharyya Computing Laboratory