0% found this document useful (0 votes)
22 views2 pages

Python Practice Exercises for Beginners

Uploaded by

mf2744805
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)
22 views2 pages

Python Practice Exercises for Beginners

Uploaded by

mf2744805
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

PF: Assignment

Exercise 1: Calculate the multiplication and sum of two numbers.


Given two integer numbers, return their product only if the product is equal to or lower than
1000. Otherwise, return their sum.

Exercise 2: Print the sum of the current number and the previous number.

Write a program to iterate the first 10 numbers, and in each iteration, print the sum of the current
and previous numbers.

Exercise 3: Print characters from a string that are present at an even index number

Write a program to accept a string from the user and display characters that are present at an
even index number.

For example, str = "pynative" so you should display ‘p’, ‘n’, ‘t’, ‘v’.

Exercise 4: Remove first n characters from a string


Write a program to remove characters from a string starting from zero up to n and return a new
string.

For example:

remove_chars("pynative", 4) so output must be tive. Here, we need to remove the first four
characters from a string.
remove_chars("pynative", 2) so output must be native. Here, we need to remove the first two
characters from a string.
Note: n must be less than the length of the string.

Exercise 5: Check if the first and last number of a list is the same.

Write a program to print “First and Last Numbers are same” if the first and last numbers of a
given list are the same. If the numbers are different then print “Not Same”.

Exercise 6: Display numbers divisible by 5 from a list.

Iterate the given list of numbers and print only those numbers which are divisible by 5

Exercise 7: Print the following pattern.

1
22
333
4444
55555

Exercise 8: Check Palindrome Number

Write a program to check if the given number is a palindrome number.

A palindrome number is a number that is the same after reverse. For example, 545, is the
palindrome numbers

Exercise 9: Create a new list from two list using the following condition

Create a new list from two list using the following condition

Given two list of numbers, write a program to create a new list such that the new list should
contain odd numbers from the first list and even numbers from the second list.

Exercise 10: Write a Program to extract each digit from an integer in the reverse order.

For example, If the given int is 7536, the output shall be “6 3 5 7“, with a space separating the
digits.

Common questions

Powered by AI

Using loops to sum the current and previous numbers helps in understanding data trends, like in cumulative totals or running averages. This can be applicable in financial time-series analysis or moving average calculations, where cumulative changes over periods are meaningful .

For the pair (50,20), the product is 1000, which is equal to the threshold, so the output is 1000 because the condition for returning the product is satisfied. For the pair (60,20), the product is 1200, which exceeds 1000, so the output is the sum of the numbers, which is 80. This demonstrates how the rule applies based on the threshold condition .

Using Python's slicing feature, `string[n:]`, is an efficient strategy to remove the first n characters because it performs the operation in a single line without explicit loops, reducing both code complexity and execution time by leveraging native sub-setting capabilities .

The program must account for lists with at least two elements, as comparing first and last elements in a list with fewer elements may lead to incorrect assumptions. The edge case where a single-element list automatically satisfies the condition must also be considered .

The approach involves repeatedly dividing the number by 10 and capturing the remainder, which represents the last digit. This process is continued until the number becomes zero. This method is effective as it directly targets the least significant digit, however, compared to converting the number to a string and reversing it, it might be less intuitive but uses less memory .

Printing characters at even indexes involves iterating through half of the length of the string but checking each index. The computational complexity is O(n), where n is the length of the string. Although the problem may seem simple, performance implications are minimal due to the direct indexing method employed .

This pattern printing technique uses nested loops focusing on writing numbers in increasing sequences. The utility lies in learning control structures such as loops, yet its limitations include lack of flexibility for other types of patterns. Such techniques might need modifications to accommodate larger or complex patterns without expanding effort .

To identify numbers divisible by 5, iterate through the list and use a modulo operation: if a number modulo 5 equals zero, it is divisible by 5. Edge cases to consider include handling negative numbers or zero, as both are divisible by 5 .

This program would be useful in data filtering scenarios, such as sorting users into different categories based on preferences or filtering datasets to have only odd items from one dataset and even from another, allowing for organized data management .

Checking palindrome numbers involves ensuring the number reads the same forward and backward, which requires reversing the number. Unlike other checks which might look for specific properties (like divisibility), this requires a transformation (reverse) and comparison, thus making it a more complex operation conceptually .

You might also like