0% found this document useful (0 votes)
15 views5 pages

Programs On Intro To Python

Uploaded by

Ishika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Programs On Intro To Python

Uploaded by

Ishika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 1: Variables and Data Types

Aim: To declare variables of different data types and display their types.​

Code:​
a = 10​
b = 3.14​
c = 'Hello'​
d = True​

print(a, type(a))​
print(b, type(b))​
print(c, type(c))​
print(d, type(d))​

Sample Output:​
10 <class 'int'>​
3.14 <class 'float'>​
Hello <class 'str'>​
True <class 'bool'>

Program 2: Type Casting


Aim: To convert a string number into integer and float.​

Code:​
s = '25'​
i = int(s)​
f = float(s)​

print('Integer:', i)​
print('Float:', f)​

Sample Output:​
Integer: 25​
Float: 25.0
Program 3: Check Positive Number
Aim: To check whether a number entered by the user is positive using if statement.​

Code:​
n = int(input('Enter a number: '))​
if n > 0:​
print('The number is positive')​

Sample Input/Output:​
Enter a number: 5​
The number is positive

Program 4: Check Even or Odd


Aim: To check whether a number is even or odd using if-else statement.​

Code:​
n = int(input('Enter a number: '))​
if n % 2 == 0:​
print('Even')​
else:​
print('Odd')​

Sample Input/Output:​
Enter a number: 4​
Even

Program 5: Grades using Marks


Aim: To input marks and display grade using if-elif-else statement.​

Code:​
marks = int(input('Enter marks: '))​
if marks >= 90:​
grade = 'A+'​
elif marks >= 75:​
grade = 'A'​
elif marks >= 60:​
grade = 'B'​
elif marks >= 40:​
grade = 'C'​
else:​
grade = 'Fail'​

print('Grade:', grade)​

Sample Input/Output:​
Enter marks: 82​
Grade: A

Program 6: First 10 Natural Numbers


Aim: To print the first 10 natural numbers using for loop with range().​

Code:​
for i in range(1, 11):​
print(i)​

Sample Output:​
1 2 3 4 5 6 7 8 9 10

Program 7: Sum of First 20 Natural Numbers


Aim: To find the sum of the first 20 natural numbers using for loop.​

Code:​
total = 0​
for i in range(1, 21):​
total += i​

print('Sum:', total)​

Sample Output:​
Sum: 210

Program 8: Multiplication Table


Aim: To print the multiplication table of a number using while loop.​

Code:​
n = int(input('Enter a number: '))​
i = 1​
while i <= 10:​
print(n, 'x', i, '=', n*i)​
i += 1​

Sample Input/Output:​
Enter a number: 5​
5 x 1 = 5​
5 x 10 = 50

Program 9: Reverse a Number


Aim: To reverse a number using while loop.​

Code:​
n = int(input('Enter a number: '))​
rev = 0​
while n > 0:​
digit = n % 10​
rev = rev * 10 + digit​
n //= 10​

print('Reversed Number:', rev)​

Sample Input/Output:​
Enter a number: 1234​
Reversed Number: 4321

Program 10: Check Prime Number


Aim: To check whether a number entered by the user is prime or not.​

Code:​
n = int(input('Enter a number: '))​
is_prime = True​
if n <= 1:​
is_prime = False​
else:​
for i in range(2, int(n**0.5)+1):​
if n % i == 0:​
is_prime = False​
break​

if is_prime:​
print('Prime')​
else:​
print('Not Prime')
Sample Input/Output:​
Enter a number: 7​
Prime

You might also like