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

Python Level 2 Programming Basics

The document contains various Python programming examples demonstrating different concepts such as loops, string manipulation, pattern printing, and mathematical calculations. Key examples include printing numbers in various formats, calculating sums, generating Fibonacci series, checking for prime numbers, and manipulating strings and lists. Each example is accompanied by sample code and expected output.
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 views10 pages

Python Level 2 Programming Basics

The document contains various Python programming examples demonstrating different concepts such as loops, string manipulation, pattern printing, and mathematical calculations. Key examples include printing numbers in various formats, calculating sums, generating Fibonacci series, checking for prime numbers, and manipulating strings and lists. Each example is accompanied by sample code and expected output.
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

1.

Print numbers from 1 to 6 using while loop:


(i)Printing in new line:
i=1
while i<7:
print(i)
i+=1
Output:
1
2
3
4
5
6
(ii)Printing in straight line:
i=1
while i<7:
print(i,end=" ")
i+=1
Output:
123456
(iii)Printing in straight line with commas:
i=1
while i<7:
if i<6:
print(i,end=",")
else:
print(i,end=" ")
i+=1
Output:
1,2,3,4,5,6
(iii)Printing in straight line with commas in square bracket:
i=1
print("[",end="")
while i<7:
if i<6:
print(i,end=",")
else:
print(i,end="]")
i+=1
Output:
[1,2,3,4,5,6]
(iii)Printing in straight line with commas in square bracket:
i=1
print("[",end="")
while i<7:
if i<6:
print(f"'{i}'",end=",")
else:
print(f"'{i}'",end="]")
i+=1
Output:
['1','2','3','4','5','6']

[Link] to calculate the sum of numbers 1 to 100


n=int(input())
sum=0
for count in range(1,n+1):
sum=sum+count
print(sum)
Output:
100
5050

[Link] program to print different patterns:


n=int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=" ")
print()
Output:
5
1
12
123
1234
12345

n=int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print(i,end=" ")
print()
Output:
5
1
22
333
4444
55555
n=int(input())
num=1
for i in range(1, n + 1):
for j in range(i):
print(num, end=" ")
num+=1
print()
Output:
5
1
23
456
7 8 9 10
11 12 13 14 15

[Link] of even and odd digits in an integer:


num=int(input())
even=0
odd=0
while num!=0:
if(num%2==0):
even+=num%10
else:
odd+=num%10
num//=10
print(even)
print(odd)
Output:
1234
6
4

[Link] to count the number of vowels and consonants in an string:


string = input("Enter a string: ")
vowels = "aeiouAEIOU"
vow=0
cons=0
if [Link]():
for char in string:
if char in vowels:
vow+= 1
else:
cons+=1
print("Number of vowels:", vow)
print("Number of consonants:", cons)
else:
print("Invalid input")
Output:
Enter a string: Balakrishnan
Number of vowels: 4
Number of consonants: 8

[Link] series upto n terms:


n=int(input())
f1=0
f2=1
print(f1,f2,end=" ")
for i in range(2,n):
fib=f1+f2
f1=f2
f2=fib
print(fib,end=" ")
Output:
10
0 1 1 2 3 5 8 13 21 34

[Link] to print prime numbers upto n terms:


num=int(input())
for num in range(2,num+1):
is_prime=True
for i in range(2,int(num**0.5)+1):
if num%i==0:
is_prime=False
break
if is_prime:
print(num,end=" ")
Output:
12
2 3 5 7 11

[Link] to check whether the given number is palindrome or not:


num=int(input())
org_num=num
rev=0
while num!=0:
rem=num%10
rev=rev*10+rem
num//=10
if(org_num==rev):
print("The given number is Palindrome.")
else:
print("The given number is not a Palindrome.")
Output:
121
The given number is Palindrome.

[Link] to reverse a string:


string=input()
reversed_string=""
for char in string:
reversed_string=char+reversed_string
print(reversed_string)
OR
input_string = input("Enter a string to reverse: ")
reversed_string = input_string[::-1]
print("Reversed string:", reversed_string)

Output:
harini
inirah

[Link] to remove a duplicate character from a string:


string=input()
result=""
for char in string:
if char not in result:
result +=char
print(result)
Output:
bala
bal

[Link] to print different patterns:


rows = int(input("Enter the number of rows: "))
for i in range(rows, 0,-1):
for j in range(i):
print('* ',end="")
print()
Output:
Enter the number of rows: 5
*****
****
***
**
*

rows = int(input("Enter the number of rows: "))


for i in range(1,rows+1,1):
for j in range(i):
print('* ',end="")
print()
Output:
Enter the number of rows: 5
*
**
***
****
*****

rows = int(input("Enter the number of rows: "))


ch=65
for i in range(1,rows+1,1):
for j in range(i):
print(chr(ch),end=" ")
print()
ch+=1
Output:
Enter the number of rows: 5
A
BB
CCC
DDDD
EEEEE

rows = int(input("Enter the number of rows: "))


ch=65
for i in range(1,rows+1,1):
for j in range(i):
print(chr(ch),end=" ")
ch+=1
print()
Output:
Enter the number of rows: 5
A
BC
DEF
GHIJ
KLMNO

rows = int(input("Enter the number of rows: "))


ch=65
for i in range(1,rows+1,1):
ch=65
for j in range(i):
print(chr(ch),end=" ")
ch+=1
print()
Output:
Enter the number of rows: 5
A
AB
ABC
ABCD
ABCDE

[Link] to reverse a list:


user_input=input().split
rev=user_input[::-1]
print(rev)
Output:
12345
['5', '4', '3', '2', '1']

[Link] to check the given list is palindrome or not:


user_input=input().split()
lst=user_input
is_palindrome=lst==lst[::-1]
if(is_palindrome):
print("Palindrome")
else:
print("Not Palindrome")
Output:
121
Palindrome

[Link] to count the number of digits:


num=int(input())
count=0
if num<0:
num=-num
if num==0:
count=1
else:
while(num!=0):
digit=num%10
count+=1
num//=10
print(count)
Output:
12345
5

[Link] to check whether the given number is prime or not:


n=int(input())
if n<=1:
is_prime=False
else:
is_prime=True
for i in range(2,int(n**0.5)+1):
if n%i==0:
is_prime=False
break
if is_prime:
print("Prime Number")
else:
print("Not a Prime Number")
Output:
5
Prime Number

[Link] to print a range of prime numbers:


n1=int(input())
n2=int(input())
for i in range(n1,n2):
if i<2:
continue
for j in range(2,int(i**0.5)+1):
if i%j==0:
break
else:
print(i)
Output:
5
10
5
7

string = input("Enter a string: ")


char_count = {}
for char in string:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
print("Character counts:")
for char, count in char_count.items():
print(f"{char}: {count}")

You might also like