Home » programming » python

Category Archives: python

ABOUT SITE

  • learning code,doing coding is so interesting but sometimes we get confused to write a code,we get confused what actually the action of code is happening in a program So,don’t worry about all those things.in this website you will get code as well as its proper explanation what the code actually doing in the program,
  • so,the main motto of this site is to share knowledge to the people.

Python Program to Find the Gravitational Force Between Two bodies

How to do :

  1. Take input of masses of the body of same unit .
  2. Take input of distance between two bodies.
  3. calculate the gravitational force between them by the expression (f=G*m1*m2/d*d)

source code:

m1=float(input("Enter the mass of first body: "))
m2=float(input("Enter the mass of second body: "))
r=float(input("Enter the distance between of the masses: "))
G=6.673e-112
f=(Gm1m2)/(r**2)
print("Required gravitational force is: ",f,"N")

output:


Explanation:

  1. Program take input of masses and distance between the bodies.
  2. Program calculate the gravitational forces between the bodies.
  3. Program print the result.

Python program to merge two list.

How to start:

  1. Take two list from from the user.
  2. Then , merge two list and show the result.

Source code:


a=[]
c=[]
n1=int(input("Enter number of elements:"))
for i in range(1,n1+1):
b=int(input("Enter element:"))
a.append(b)
n2=int(input("Enter number of elements:"))
for i in range(1,n2+1):
d=int(input("Enter element:"))
c.append(d)
n=a+c
new.sort()
print("Sorted list is:",n)

Output:

Explanation:

  1. User input number of element and element for two list .
  2. Program add two list and print result.

Python Program to Print Table of a Given Number

How to do?

  1. Take input number(to form table) from the user .
  2. Process to make formation of table through lopping

Source code:

n=int(input("Enter the no.to generate mul table:"))

for i in range(1,12):

print(n,"x",i,"=",n*i)


Explanation:

  1. program ask user to input number to generate multiplication table.
  2. Program starts looping to generate table.

Output:

Simple key press event using python

we can create simple key press event using python by
binding the keys of the keyboard by using canvas.bind_all
function .
example: canvas.bind_all(‘<KeyPress-w>’,self.up)

Here is the simple example of key bind function:

from tkinter import *
import time
import random
win=Tk()
win.title("game")
canvas=Canvas(win,width=500,height=500)
canvas.pack()
t=("red","green","blue","grey","pink","black","yellow","orange")
class Bind():
       def __init__(self,color,size):
              self.canvas=canvas
              self.shape=canvas.create_oval(10,10,size,size,fill=color)
              self.x=random.randint(10,30)
             self.y=random.randint(-10,5)
             canvas.move(self.shape,250,250)
             self.canvas.bind_all('<KeyPress-d>', self.left)
             self.canvas.bind_all('<KeyPress-a> ', self.right)
             self.canvas.bind_all('<KeyPress-w> ', self.up)
             self.canvas.bind_all('<KeyPress-s> ', self.down)
       def draw(self):
       self.canvas.move(self.shape, self.x, self.y)
       po=canvas.coords(self.shape)
       if po[0]<=0 or po[2]>=500:
             self.x=-self.x
       if po[1]<=0 or po[3]>=500:
             self.y=-self.y
       def  left(self,evt):
              self.x=-10
       def  right(self,evt):
              self.x=10
       def up(self,evt):
             self.y=-10
       def down(self,evt):
             self.y=10
ball=Bind(random.choice(t),90)
while 1:
   ball.draw()
   win.update()
   time.sleep(0.05)

Python Program to Count the Number of Vowels Present in a String using Sets

To count the number of vowels in a string we need to do following steps.

  1. reading the string from the user.
  2. create a set of string containing vowels
  3. compare the given string to the set of vowels.
  4. count the number of vowels if match found.
  5. print the result.

For that source code is given below:

s=input("Enter string:") 
cnt = 0
vowels="aeiou"
for letter in s:
if letter in vowels:
cnt = cnt+1
print("Count of the vowels is:") print(cnt)

output:

Explanation:

  1. user have to input the string.
  2. program will compare the given string with (aeiou ) inside for loop.
  3. if match found cnt will increment and print the output.

Bouncing ball animation in python using tkinter(canvas)

To create a bouncing ball animation .firstly we need to import tkinter and create a canvas .

The source code is given below:

correct the spaces in the program

from tkinter import *
import time
import random
win=Tk()
canvas=Canvas(win,width=400,height=500)
canvas.pack()
class Ball():
    def __init__(self,color,size):    
 self.shape=canvas.create_oval(10,10,size,size,fill=color)
  self.xspeed=random.randint(1,6)#user can give suitable speed
    self.yspeed=random.randint(5,10)#user can give suitable speed
def Move(self):

    canvas.move(self.shape,self.xspeed,self.yspeed)
    pos=canvas.coords(self.shape)
    if pos[3]>=500 or pos[1]<=0:
         self.yspeed=-self.yspeed
    if pos[2]>=400 or pos[0]<=0:
        self.xspeed=-self.xspeed
   redball=Ball("red",60)
greenball=Ball("green",100)
orangeball=Ball("orange",60)#here user can create another ball 
while True:
    redball.Move()
    greenball.Move()
    orangeball.Move()
    #if user create annother ball then user must call the move function inside while loop
    win.update()
    time.sleep(0.05)

To learn more click here.

python program to find largest and smallest number in a list .

To find largest and smallest number in a list using python programming .

firstly, we need to input elements in a list and print the output which one is largest and which one smallest .

The code is given below:

n=int(input("Enter the number of elements to be in the list:"))
b=[]
for i in range(0,n):
a=int(input("Enter elements:")
b.append(a)
b.shot()
print("largest number= ",b[n-1])
print("smallest number= ",b[0])

Explanation:

1)user must input the number of elements and elements

2) Then elements will append(to add elements in a list from left to right) in the list a[] .inside the for loop because to read the element in each time and to append yo the list .

3) Then shorting of list happen.obviously in the list a .the 1st position and last position is the smallest and largest number in list.and lastly print the output .