0% found this document useful (0 votes)
32 views3 pages

Python Programming Exercises

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)
32 views3 pages

Python Programming Exercises

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

Question 1 :

Write a Program to input 2 numbers & print their sum.

Question 2:

Write a program to input side of a square & print its area.

Question 3:

Write a program to input 2 floating point numbers & print their average.

Question 4:

Write a program to input 2 int numbers, a and b.


Print True if a is greater than or equal to b. If not print False. ////////

Question 5:

Write a program to input user’s first name & print its length.

Question 6:

Write a program to find the occurrence of ‘$’ in a String.

Question 7:

Write a program to check if a number entered by the user is odd or even.


Question 8:

Write a program to find the greatest of 3 numbers entered by the user.:

Question 9:

Write a program to check if a number is a multiple of 7 or not.

Question 10:

Write a program to ask the user to enter names of their 3 favourite movies & store them in a list.

Question 11:

WAP to check if a list contains a palindrome of elements. (Hint: use copy( ) method)

[1, 2, 3, 2, 1] [1,”abc”,”abc”,1]

Question 12:

Write a program (WAP) to count the number of students with the “A” grade in the following tuple.
[“c”,”d”,”a”,”a”,”b”,”b”,”a”]
Store the above values in a list & sort them from “A” to “D”
Question 13:

Store following word meanings in a python dictionary:


table : “a piece of furniture”,“list of facts & figures”
cat : “a small animal”
You are given a list of subjects for students. Assume one classroom is required for 1
subject. How many classrooms are needed by all students.
”python”,“java”,“C++”,“python”,“javascript”“java”,“python”,“java”,“C++”,“C”

Question 14:

WAP to enter marks of 3 subjects from the user and store them in a dictionary. Start with
an empty dictionary & add one by one. Use subject name as key & marks as value.
Figure out a way to store 9 & 9.0 as separate values in the set.
(You can take help of built-in data types)

Common questions

Powered by AI

A syntactical way to determine if a user-entered number is even or odd is to use the modulus operator. Specifically, if a number modulo 2 equals zero, then the number is even; otherwise, it is odd. This can be implemented as: 'if number % 2 == 0: print("Even") else: print("Odd")' in a program .

To store user input dynamically in a dictionary for subjects and marks, begin with an empty dictionary and use a loop to continuously prompt user input using 'input()'. Assign the user-provided subject as the key and the mark as its value in the dictionary using 'dictionary[subject] = mark'. This method ensures that each subject is accurately mapped to its corresponding marks, and can handle updates easily .

To differentiate between 9 and 9.0 in a Python set, one can use a data structure that supports both integer and floating-point as distinct entities. For instance, storing these values as keys in a dictionary or as elements in a list will maintain their individuality, unlike a set which treats them as the same due to Python's handling of numeric types. The key is avoiding sets or implementing an additional identifier for such numbers .

To store students' subjects and compute the number of classrooms required, one can create a dictionary to track subject frequencies and determine unique entries representing the number of classrooms needed. First, create an empty dictionary and add subjects as keys and frequency as values by iterating over the input list of subjects. The number of unique keys in the dictionary indicates the number of separate classrooms needed .

To store and sort word meanings in Python using dictionaries, create a dictionary with words as keys and their meanings as values: {'table': 'a piece of furniture', 'cat': 'a small animal'}. To maintain order or sort them, use the 'sorted()' function to obtain a sorted list of keys or consider using 'OrderedDict' from collections, which maintains order as items are inserted .

The technique to verify if a number is a multiple of seven involves using the modulo operator. If a number modulo 7 equals zero, the number is a multiple of seven. Implement this check with 'if number % 7 == 0:', which will determine if the number is divisible by seven without a remainder .

A program can determine the greatest among three user-input numbers by comparing them using conditional statements. Initially, take the three inputs and then use nested 'if' or 'elif' statements to compare each number, e.g., 'if a > b and a > c:...'. The greatest number can then be identified and printed based on the condition that evaluates to true .

A program can be constructed to check if a given list contains a palindrome of elements by first creating a copy of the list and then comparing the list to its reversed version. The 'copy()' method is used to make a shallow copy of the list. If the copied list is equal to its reverse, then the list is a palindrome. For example, with lists like [1, 2, 3, 2, 1] or [1, 'abc', 'abc', 1], this method will return True since the elements form a palindrome pattern .

To accurately count 'A' grades in a given tuple, the tuple should first be converted into a list to leverage the count() method, which helps count the occurrences of a specific element. By converting the grades tuple ['c', 'd', 'a', 'a', 'b', 'b', 'a'] into a list, one can then call list.count('a') to find the frequency of 'A' grades. Sorting the list from 'A' to 'D' can further help in displaying an ordered structure .

An effective way to find the occurrence of a specific character in a string in Python is to use the string method count(). For instance, to find the occurrence of '$' in a string, the syntax is 'string.count("$")'. This method returns the number of times '$' appears in the given string .

You might also like