0% found this document useful (0 votes)
25 views

Problems Solutions

The document contains 9 coding exercises for Python beginners. It includes examples of functions to: 1) Calculate multiplication and sum of two numbers. 2) Print the sum of the current number and previous number in a range. 3) Print characters from a string at even index numbers. 4) Remove the first n characters from a string. 5) Check if the first and last number of a list are the same. 6) Display numbers divisible by 5 from a list. 7) Return the count of a given substring from a string. 8) Check if a number is a palindrome.

Uploaded by

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

Problems Solutions

The document contains 9 coding exercises for Python beginners. It includes examples of functions to: 1) Calculate multiplication and sum of two numbers. 2) Print the sum of the current number and previous number in a range. 3) Print characters from a string at even index numbers. 4) Remove the first n characters from a string. 5) Check if the first and last number of a list are the same. 6) Display numbers divisible by 5 from a list. 7) Return the count of a given substring from a string. 8) Check if a number is a palindrome.

Uploaded by

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

https://pynative.

com/python-basic-exercise-for-
beginners/
Exercise 1: Calculate the multiplication and sum of two numbers
print("multiplication and sum of two Numbers")
def two_number(a,b):

if a*b<=1000:
product=a*b
print("product=", product)
return product
else:
sum= a+b
print("sum=", sum)
return sum
while True:
x = int(input("Enter first number:"))
y = int(input(("Enter second number:")))
a= two_number(x,y)
print("hello ", a)

multiplication and sum of two Numbers


Enter first number:2
Enter second number:3
product= 6
hello 6

Exercise 2: Print the sum of the current number and the previous number
print("Printing current and previous number sum in a range(10)")
previous_number= 0
for i in range(1,11):
sum = previous_number+i
print("Current Number is : ",i, "Previous Number is : ",
previous_number,"Total Sumation is: ",sum)
previous_number=i
Printing current and previous number sum in a range(10)
Current Number is : 1 Previous Number is : 0 Total Sumation is: 1
Current Number is : 2 Previous Number is : 1 Total Sumation is: 3
Current Number is : 3 Previous Number is : 2 Total Sumation is: 5
Current Number is : 4 Previous Number is : 3 Total Sumation is: 7
Current Number is : 5 Previous Number is : 4 Total Sumation is: 9
Current Number is : 6 Previous Number is : 5 Total Sumation is: 11
Current Number is : 7 Previous Number is : 6 Total Sumation is: 13
Current Number is : 8 Previous Number is : 7 Total Sumation is: 15
Current Number is : 9 Previous Number is : 8 Total Sumation is: 17
Current Number is : 10 Previous Number is : 9 Total Sumation is: 19
Exercise 3: Print characters from a string that are present at an even index number
Lst = [50, 70, 30, 20, 90, 10, 50]
print(Lst[::])#copy
print(Lst[0::2])#even number index# 50,30,90,50
print(Lst[::2]) #even number index
print(Lst[1::2])#odd number index# 70,20,10
print(''' One string is taken that is "BANGLADESH".
Print only even index of this string''')
take_input=input(" Enter the Word: ")#1 take input
print("Given string is:", take_input)
list_str= list(take_input)# 2 convert user input into list
keep_list=list_str[0::2]# 3 convert even indexing
print(keep_list)#4 make even indexing list
for y in keep_list:#5 make loop for verticale the list
print(y)
One string is taken that is "BANGLADESH".
Print only even index of this string
Enter the Word: BANGLADESH
Given string is: BANGLADESH
['B', 'N', 'L', 'D', 'S']
B
N
L
D
S

Exercise 4: Remove first n characters from a string


print(" Removing n characters from the given string")
take_input_for_removing_char= input("Enter the string:")#step-1 take input
def remove_char(word,n):
print("Original word: ", word)
removing_word= word[n:]
return removing_word
x=remove_char(take_input_for_removing_char,2)
print("Modified Word:", x)

Removing n characters from the given string


Enter the string:sdbangla
Original word: sdbangla
Modified Word: bangla

Exercise 5: Check if the first and last number of a list is the same
def last_and_first_name_check(numberlist):
first_number=numberlist[0]
last_number=numberlist[-1]

if first_number==last_number:
print(" first number and last number is same")
return True
else:
print(" first number and last number is Different")
return False

last_and_first_name_check([2,5,6,8,9,2])

Output:

Check if the first and last number of a list is the same


first number and last number is same

Exercise 6: Display numbers divisible by 5 from a list

Exercise 7: Return the count of a given substring from a string


given_statement= " she is good but dishonest.she is bad but honest."
print("Given Statement:",given_statement)
count_of_str=given_statement.count("she")#count method= count the str
print("'she' word is ",count_of_str,"times in the statement")

Output:
Given Statement: she is good but dishonest.she is bad but honest.
'she' word is 2 times in the statement

Exercise 9: Check Palindrome Number


****back forward same value** 121,1881,mom

def palindrome(number):
print("original number", number)
original_num = number
# reverse the given number
reverse_num = 0
while number > 0:
reminder = number % 10 #take reminder/ reverse last digit
reverse_num = (reverse_num * 10) + reminder
number = number // 10# omit reminder from original number

# check numbers
if original_num == reverse_num:
print("Given number palindrome")
else:
print("Given number is not palindrome")

palindrome(121)
palindrome(125)

You might also like