Python Program List 2


111.      Write a Python program to get a string and print n (non-negative integer) copies of a given string using function.

def larger_string(str, n):
   result = ""
   for i in range(n):
      result = result + str
   return result

print(larger_string('patel', 2))
print(larger_string('.py', 3))
Output:
patelpatel
                                .py.py.py

212.      Write a Python program to find whether a given number (accept from the user) is even or odd, print out an appropriate message to the user.

num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
    print("This is an odd number.")
else:
    print("This is an even number.")
Output:
Enter a number: 5                                                                                            
This is an odd number.

313.      Write a Python program to test whether a passed letter is a vowel or not.

def is_vowel(char):
    all_vowels = 'aeiou'
    return char in all_vowels
print(is_vowel('c'))
print(is_vowel('e'))

Output:
Flase
True

414.      Write a Python program to check whether a specified value is contained in a group of values.

def is_group_member(group_data, n):
    for value in group_data:
        if n == value:
            return True
    return False
print(is_group_member([1, 5, 8, 3], 3))
print(is_group_member([5, 8, 3], -1))

Output:
True
False

515.      Write a Python program to create a histogram from a given list of integers.

def histogram( items ):
    for n in items:
        output = ''
        times = n
        while( times > 0 ):
          output += '*'
          times = times - 1
        print(output)
histogram([2, 3, 6, 5])


Output:
**                                                                                                           
***                                                                                                          
******                                                                                                       
*****

616.      Write a Python program to compute the greatest common divisor (GCD) of two positive integers.

def gcd(x, y):
    gcd = 1
   
    if x % y == 0:
        return y
   
    for k in range(int(y / 2), 0, -1):
        if x % k == 0 and y % k == 0:
            gcd = k
            break 
    return gcd

print(gcd(12, 17))
print(gcd(4, 6))

Output:
1
2

717.      Write a Python program to display your details like name, age, address in three different lines.

def personal_details():
    name, age = "Piyush", 30
    address = "Rajkot, Gujarat, India"
    print("Name: {}\nAge: {}\nAddress: {}".format(name, age, address))

personal_details()

Output:
Name: Piyush
Age: 30                                                                                          
Address: Rajkot, Gujarat, India

818.      Write a Python program to get OS name, platform and release information.

import platform
import os
print(os.name)
print(platform.system())
print(platform.release())

Output:

919.      Write a Python program to sum of three given integers. However, if two values are equal sum will be zero.

def sum(x, y, z):
    if x == y or y == z or x==z:
        sum = 0
    else:
        sum = x + y + z
    return sum

print(sum(2, 1, 2))
print(sum(3, 2, 2))
print(sum(2, 2, 2))
print(sum(1, 2, 3))

0
0
0
6

120.  Write a python program to sum of the first n positive integers.

n = int(input("Input a number: "))
sum_num = (n * (n + 1)) / 2
print(sum_num)

Output:
Input number: 10
55

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes