Python Programming
Grade 8
1. Python code to print string characters
# declaring & initializing two strings
str1=”IncludeHelp”
str2=”.com”
print (str1) # printing complete str1
print (str1[0]) # printing 0th (first) elements of str1
print (str1[0], str1[1]) # printing first & second elements
print (str1[2:5]) # printing elements from 2nd to 5th index
print (str1[1:]) # printing all elements from 1st index
print (str2 * 2) # printing str2 two times
print (str1 + str2) # printing concatenated str1 & str2
Output:
IncludeHelp
I
In
clu
ncludeHelp
.com.com
IncludeHelp.com
2. Python code to create number variables, print types and values
# creating number variables and assigning values
a = 10 #integer
b = 10.23 #float
c = “Hello” #string
# printing types
print("type(a): ", type(a))
print("type(b): ", type(b))
print("type(c): ", type(c))
# printing values
print("value of a: ", a)
print("value of b: ", b)
print("value of c: ", c)
Output:
type(a): <class 'int’>
type(a): <class 'float’>
type(a): <class 'string’>
value of a: 10
value of a: 10.23
value of a: Hello
3. Python program for using formulas
(a) # Python program to find simple interest
p = float(input("Enter the principle amount :”))
r = float(input("Enter the rate of interest : "))
t = float(input("Enter the time in the years: "))
# calculating simple interest
si = (p*r*t)/100
# printing the values
print("Principle amount: ", p)
print("Interest rate: ", r)
print("Time in years: ", t)
print("Simple Interest : ", si)
Output:
First run:
Enter the principle amount : 10000
Enter the rate of interest : 3.5
Enter the time in the years: 1
Principle amount: 10000.0
Interest rate : 3.5
Time in years : 1.0
Simple Interest: 350.0
Second run:
Enter the principle amount : 250000
Enter the rate of interest : 36
Enter the time in the years: 1
Principle amount: 250000.0
Interest rate : 36
Time in years : 1.0
Simple Interest: 90000.0
(b) ) # Python program to find compound interest
p = float(input("Enter the principle amount :”))
r = float(input("Enter the rate of interest : "))
t = float(input("Enter the time in the years: "))
# calculating compound interest
ci = p * (pow((1 + r / 100), t))
# printing the values
print("Principle amount: ", p)
print("Interest rate: ", r)
print("Time in years: ", t)
print("Compound Interest : ", ci)
Output:
First run:
Enter the principle amount : 10000
Enter the rate of interest : 3.5
Enter the time in the years: 1
Principle amount: 10000.0
Interest rate : 3.5
Time in years : 1.0
Compound Interest: 10350.0
Second run:
Enter the principle amount : 250000
Enter the rate of interest : 36
Enter the time in the years: 1
Principle amount: 250000.0
Interest rate : 36
Time in years : 1.0
Compound Interest: 339999.99999999994
4. Find the Leap year (conditional statement)
# input the year
y=int(input('Enter the value of year: '))
# To check for non-century year
if y%400==0 or y%4==0 and y%100!=0:
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output:
Run 1:
Enter the value of year: 2020
The given year is a leap year
Run 2:
Enter the value of year: 2022
The given year is a non-leap year
Questions:
1. Write a python program to Input age and check eligibility for voting
2. Write a python program to print values with different data types (int, float, Boolean, etc)
3. Write a python program to input three integers and output the greatest of three