Month: March 2021

Python Example – Write a Python program to display all the member name of an enum class ordered by their values.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to display all the member name of an enum class ordered by their values.   Sample Solution: Python Code: import enum class Country(enum.IntEnum): Afghanistan = 93 Albania = 355 Algeria = 213 Andorra = 376 Angola = 244 Antarctica = 672 …

Python Example – Write a Python program to iterate over an enum class and display individual member and their value.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to iterate over an enum class and display individual member and their value.   Sample Solution: Python Code: from enum import Enum class Country(Enum): Afghanistan = 93 Albania = 355 Algeria = 213 Andorra = 376 Angola = 244 Antarctica = …

Python Example – Write a Python program to create an Enum object and display a member name and value

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create an Enum object and display a member name and value.   Sample Solution: Python Code: from enum import Enum class Country(Enum): Afghanistan = 93 Albania = 355 Algeria = 213 Andorra = 376 Angola = 244 Antarctica = 672 …

Python Example – Write a Python function to create and print a list where the values are square of numbers between 1 and 30 (both included)

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python function to create and print a list where the values are square of numbers between 1 and 30 (both included).   Sample Solution: Python Code: def printValues(): l = list() for i in range(1,21): l.append(i**2) print(l) printValues() Sample Output: [1, 4, 9, …

Python Example – Write a Python function that checks whether a passed string is palindrome or not.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python function that checks whether a passed string is palindrome or not. Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.   Sample Solution: Python Code: def isPalindrome(string): left_pos = 0 …

Python Example – Write a Python function that takes a number as a parameter and check the number is prime or not.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python function that takes a number as a parameter and check the number is prime or not. Note : A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself. …

Python Example – Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters.   Sample Solution: Python Code: def string_test(s): d={“UPPER_CASE”:0, “LOWER_CASE”:0} for c in s: if c.isupper(): d[“UPPER_CASE”]+=1 elif c.islower(): d[“LOWER_CASE”]+=1 else: pass print (“Original String : …

Python Example – Write a Python function to check whether a number is in a given range

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python function to check whether a number is in a given range.   Sample Solution: Python Code: def test_range(n): if n in range(3,9): print( ” %s is in the range”%str(n)) else : print(“The number is outside the given range.”) test_range(5) Sample Output: 5 …

Python Example – Write a Python function to calculate the factorial of a number (a non-negative integer)

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.   Sample Solution: Python Code: def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) n=int(input(“Input a number to compute …