Python Programming
2 What is Python?
3
4
5
6
7
8
9 Constant
10 Variable
11 Variable
12 Reserved Words
13 Statement vs. expression
14 input
15 Output
16
17
18 Converting data type
19 Data types
String (str)
“Hello”
“I am 99 years old”
“99”
Integer (int)
Float (float)
20 Strings with arithmetic operators
print(“a”+”b”)
print(“a”*9)
21 Python Program for Program to find area of
a circle
Area = pi * r2
where r is radius of circle
22 Comments
Single line comment - #
Multi-line comment - #
23 Conditional Statements
if statement
if-else
if-elif-else
nested if-else
24 Logical test with conditional structure
25 ‘if’ statement
26 Logical test with conditional structure
27 Exercise
calculate the square of a number if it is greater than 5
Given two numbers, write a Python program to find the maximum of these
two numbers
eg./
Input: a=2, b=4 Output: 4
Input: a=-1, b=-4 Output: -1
28 if and else
29 Exercise
Write a python program to check whether a user entered password is
correct or not. The correct password is “Pynative@#29”
30 nested if
31 Exercise
Write a python program to check whether a user input number is positive or
negative and if the number is positive, decide even or odd.
32 Exercise
Calculate income tax for the given income by adhering to the below rules
Taxable Income Rate (in %)
First $10,000 0
Next $10,000 10
The remaining 20
Expected Output:
For example, suppose the taxable income is 45000 the income tax payable is
10000*0% + 10000*10% + 25000*20% = $6000.
33 Iterative Statements
“for” loop
“while” loop
34 Counter loop = for loops
35 “for” loop
for i in range(10)
for, in = keyword for loop
i = variable name of index/counter
range() = function
- have upto 3 parameters
- eg/ range(1st ,2nd , 3rd)
1st = start
2nd = end
3rd = increment/decrement
36 Parameters in range()
For 1 parameter, (end) start point = 0, endpoint = end-1
eg/ range (10) start point = 0, endpoint = 10-1 =9, +1
For 2 parameters, (start, end)
eg/ range(2,8) start point =2, endpoint = 8-1=7, +1
For 3 parameters, (start, end, in/de)
eg/ range(3, 11,3) start point =3, endpoint = 11-1=10, +3
index 3, 6, 9
eg/ range(11, 3, -3) start point=11, endpoint=2, -3
index 11, 8, 5
37 Exercise
Write a python program to find the cubes value from 1 to user input
number. (“The cube of 3 is 27”)
Find the prime numbers of a number range.
(All prime numbers are greater than 1. If number is less than or equal to 1, it
is not prime. A prime number cannot be divided by any other numbers
without leaving a remainder. Eg./ 13 is prime number. It can only be
divided by 1 and 13.
eg/ For start range = 25, end range = 50,
Prime numbers between 25 and 50 are: 29, 31, 37, 41, 43, 47
38 Conditional loop = while loop
39 Exercises
Print First 10 natural numbers using while loop
Write a program to accept a number from a user and calculate the sum of
all numbers from 1 to a given number. (if the user entered 10 the output
should be 55 = 1+2+3+4+5+6+7+8+9+10)
Write a program to print the following number pattern using a loop.