Skip to content

Instantly share code, notes, and snippets.

@nazmul629
Last active September 19, 2019 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazmul629/f73447f4ff549e2986809ccadf12fa9e to your computer and use it in GitHub Desktop.
Save nazmul629/f73447f4ff549e2986809ccadf12fa9e to your computer and use it in GitHub Desktop.
W3Resourse Python Basic (Part-I)All Problem Solution here

Problem 1:

Write a Python program to print the following string in a specific format

print("Twinkle, twinkle, little star")
print("\t How I wonder what you are!")
print("\t \t Up above the world so high,")
print(("\t \t Like a diamond in the sky. "))
print("Twinkle, twinkle, little star")
print("\t How I wonder what you are!")

Problem 2:

Write a Python program to get the Python version you are using

import sys
print(sys.version)

Problem 3:

Write a Python program to display the current date and time.

import datetime
now=datetime.datetime.now()
print(now.strftime("Now Time is :- %Y-%m-%d %H:%M:%S"))

Problem 4:

Write a Python program which accepts the radius of a circle from the user and compute the area

from math import pi
r = float(input("Enter The Radius: "))
Area = pi*r*r
print("The Area of the Cirle is = ",Area)

Problem 5:

Write a Python program which accepts the user's first and last name and print them in reverse order with a space between them.

FirstName =  input("Enter First Name : ")
LastName = input("Enter Last Name : ")
print("Hello "+LastName+" "+FirstName)

Problem 6:

Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.

values = input("Enter sum number with some comma : ")
lists = values.split(",")

print("List: ",lists)
print("Tuple: ",tuple(lists))

Problem 7:

Write a Python program to accept a filename from the user and print the extension of that.

filename = input("Enter a File name : ")
f_extns = filename.split(".")
extantion = (repr(f_extns[-1]))
print(extantion)

Problem 8:

Write a Python program to display the first and last colors from the following list.

color = input("Enter some color with comma: ")
list = color.split(",")
print(list[0],list[-1])

Problem 9:

Write a Python program to display the examination schedule. (extract the date from exam_st_date).

datestr =input("Enter a date: ")
date = datestr.split(",")
date=(tuple(date))
print(" %s/ %s/ %s"%tuple)

Problem 10:

Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn.

a = int(input("Enter Integer N: "))
n1 = int("%s"%(a))
n2 = int("%s%s"%(a,a))
n3 = int("%s%s%s"%(a,a,a))
print(n1+n2+n3)

Problem 11:

Print the documents of Python built-in function(s)

Problem 12:

Print the calendar of a given month and year

import calendar
year = int(input("Enter Year: "))
month = int(input("Enter Month: "))
print(calendar.month(year, month))

Problem 13:

Print a specified document

 a string that you "don't" have to escape
This
is a ....... multi-line
heredoc string --------> example """)

Problem 14:

Write a Python program to calculate number of days between two dates.

f_date = date(2019,7,2)
l_date = date(2021,5,3)
dalta = l_date - f_date
print(dalta.days)

Problem 15:

Write a Python program to get the volume of a sphere with radius User define

from math import pi

r = float(input("Enter The Sphere radius"))
V = 4/3*pi*r**3
print(V)

Problem 16:

Python: Get the difference between a given number and 17, if the number is greater than 17 return double the absolute difference

number = int(input("Enter an number: "))
if(number>17):
    grat = number-17
    print(grat*2)
elif(number<=17):
    valu = number-17
    print(abs(valu))

Problem 17:

Problem 18:

Calculate the sum of three given numbers, if the values are equal then return thrice of their sum

def sum_thrice(x, y, z):
    sum = x + y + z

    if x == y == z:
        sum = sum * 3
    return sum

print(sum_thrice(x = int(input("Enter x: ")),y = int(input("Enter y :")), z = int(input("Enter y :")) ))
I thiks Its have abetter soultion.

Problem 19:

*** Get a new string from a given string where 'Is' has been added to the front. If the given string already begins with 'Is' then return the string unchanged***

def new_string(str):
    if len(str) >=2 and str[:2] =="Is":
        return str
    else:
        return "Is "+ str
print(new_string(input("Enter A String: ")))

Problem 20:

Get a string which is n (non-negative integer) copies of a given string

def largers_str(str,n):
    result =""
    for i in range(n):
        result+=str;
    return result
print(largers_str(str=input("Enter a string: "), n =int(input("Enter a number: ")) ))

Problem 21:

Find whether a given number is even or odd, print out an appropriate message to the user

n =int(input('Enter a Number: '))

check = n % 2
if check ==0:
    print(n,' is Even Number.')
else:
    print(n,'is a Odd Number')

Problem 22:

Write a Python program to count the number 4 in a given list.

list =  [int(i) for i in input("Enter some number: ").split(",")]

count = 0;
for i in list:
    if i == 4:
        count=count+1;
print(count)

Problem 23:

n (non-negative integer) copies of the first 2 characters of a given string

    return (str[:2]*n)
print(substr_copy(str = input("Enter a string: "), n = int(input("Enter  N : "))))

Problem 24:

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

def check_vowel(str):
    all_vowel = 'aeiou'
    str = str[:1]
    return str in all_vowel
print(check_vowel(input("Enter a  character: ")))

Problem 25:

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

def check_number(list,n):
    return n in list
print(check_number([int(a) for a in input("Enter some number with comma: ").split(',')],
                   int(input("Enter check number: "))))
                   

Problem 26:

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

x=input()
for i in x :
    print('@'*int(i))

Problem 27:

Write a Python program to concatenate all elements in a list into a string and return it.

def collection_to_string(element):
    result =""
    for e in element:
        result+=str(e)
    return  result
print(collection_to_string([int(a) for a in input("Enter some valu :").split(',')]))

Problem 28:

Write a Python program to print all even numbers from a given numbers list in the same order and stop the printing if any numbers that come after 237 in the sequence

numbers = [
    386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
    399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
    815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
    958,743, 527
    ]
for n in numbers:
    if n%2==0  :
        print(n)
    if n==237:
        print(n)
        break;

Problem 29:

Write a Python program to print out a set containing all the colors from color_list_1 which are not present in color_list_2

color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])

print(color_list_1 - color_list_2)

Problem 30:

Write a Python program that will accept the base and height of a triangle and compute the area.

print("Area of triangle is ",(1/2)*(float(input("Enter base:")))*(float(input("Enter height:"))))

Problem 31:

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

num1,num2 = [int(x) for x in input("Enter two valu: ").split(",")]
n1 = num1;n2 = num2;

while(n2 != 0):
    r = n1 % n2;
    n1 = n2
    n2 = r;
print("GCD=",n1)

Problem 32:

Write a Python program to get the least common multiple (LCM) of two positive integers.

num1,num2 = [int(x) for x in input("Enter two valu: ").split(",")]
n1 = num1;n2 = num2;

while(n2 != 0):
    r = n1 % n2;
    n1 = n2
    n2 = r;
# print("GCD=",n1)

lcm =num1*num2/n1;
print("LCM = ",int(lcm))

Problem 33:

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

def sum(a,b,c):
    
    if a==b or b==c or c==a:
        sum = 0;
    else:
        sum=(a+b+c)
    return sum

x,y,z =[int(a) for a in input("Enter Three Number: ").split(",")]

print(sum(x,y,z))

Problem 34:

Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20.

def sum(a,b):
    result = a+b
    if 15<result<20:
        result =20
    else:
        result = a+b
    return  result
x,y =[int(n) for n in input("Enter Three Number: ").split(",")]

print(sum(x,y))

Problem 35:

Write a Python program that will return true if the two given integer values are equal or their sum or difference is 5.

def sum(a,b):
    result = abs(a+b)
    diffarecne= a-b;
    if a == b or result ==5 or diffarecne==5:
        return True
    else:
        return False

x,y =[int(n) for n in input("Enter Two Number: ").split(",")]

print(sum(x,y))

Problem 36:

Write a Python program to add two objects if both objects are an integer type.

def add_int(x,y):
    if type(x)==int and type(y)==int:
        return x+y
    else:
        return  "Error: not integers"
print(add_int(3,5))
print(add_int(5,"abc"))
print(add_int(12.2,3))

Problem 37:

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

name,age ="nazmul", 19
address = "Dhaka,Munshgonj Bangladesh"
print("Name:{}\nAge: {}\nAddress{}".format(name,age,address))

Problem 38:

Write a Python program to solve (x + y) * (x + y).

x,y = [int(a) for a in input("Enter  x ,y: ").split(",")]
r = x**2 + 2*x*y + y**2;
print("Name:{}+{}^2={}".format(x,y,r))

Problem 39:

Write a Python program to compute the future value of a specified principal amount, rate of interest, and a number of years.

p = 10000
r = 3.5
y = 7
c = p *((1+(r/100))**y)
print(c)

Problem 40:

Write a Python program to compute the future value of a specified principal amount, rate of interest, and a number of years.

import math
p1 = x1,y1 = [int(a) for a in input("Enter x1 and y1: ").split(',')]
p2 = x2,y2 = [int(b) for b in input("Enter x2 and y2: ").split(',')]
d = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
print(d)

Problem 41:

Write a Python program to check whether a file exists.

Problem 42:

Write a Python program to determine if a Python shell is executing in 32bit or 64bit mode on OS

Problem 43:

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

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

Problem 44:

***Write a Python program to locate Python site-packages. ***

import  site
print(site.getsitepackages(), '\n', site.getusersitepackages())

Problem 45:

Write a python program to call an external command in Python.

Problem 46:

***Write a python program to get the path and name of the file that is currently executing. ***

import os
print(os.path.realpath(__file__)

Problem 47:

Problem 48:

Write a Python program to parse a string to Float or Integer.

str =input("Enter a Number: ")
flt = float(str)
int =int(flt)
print(flt)
print(int)

Problem 49:


Problem 50:

Write a Python program to print without newline or space.

for i in range(10):
    print('z',end="")

Problem 51:

Problem 52:

Problem 53:

Problem 54:

Problem 55:

Problem 56:

Problem 57:

Write a program to get execution time for a Python method.

import time
def exuxution_time(n):
   start_time = time.time()
   sum =0
   for i in range(1,n+1):
       sum = sum + i;
   end_time = time.time()
   return sum,  end_time - start_time;
n = int(input("Enter Integer Number: "))
print("\nTime to sum of 1 to ",n," and required time to calculate is :",exuxution_time(n))

Problem 58:

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

def sumof_number(n):
   sum = n*(n+1)/2
   print("sum of result:",sum)
a =int(input("Enter number: "))
print(sumof_number(a))

Problem 59:

Write a Python program to convert height (in feet and inches) to centimeters.

f = float(input("Enter your height in inch "))
inc = f*12;
cm = int(inc) * 2.54
print(cm)

Problem 60:

Write a Python program to calculate the hypotenuse of a right angled triangle.

from math import sqrt
a = float(input("Enter Hight: "))
b = float(input("Enter base: "))
hypotenuse = sqrt(a**2 + b**2)
print("Hypotenuse of right angle Triangle: ",hypotenuse)

Problem 61:

Write a Python program to convert the distance (in feet) to inches, yards, and miles

from math import sqrt
a = float(input("Enter Hight: "))
b = float(input("Enter base: "))
hypotenuse = sqrt(a**2 + b**2)
print("Hypotenuse of right angle Triangle: ",hypotenuse)

Problem 62:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment