Short python codes for practice

godviras
by godviras · 3 posts
4 years ago in Python
Posted 4 years ago · Author
Hello everyone. Python is one of widely used programming language and popular among every age groups (be it either school going or even a industry level professional). It's simplicity and compatibility with third party apps makes it easier to understand and build programs.

I am sharing some short codes that helped me as i am learning Python.

Code
print("A simple age calculator")
    name = input("whats your name?")
    age = int(input('Enter your age: '))
    days = age * 365
    hours = days * 24
    minutes = hours * 60
    seconds = minutes * 60
    print(name + ", you are alive for",days,"days,",hours,"hours,",minutes,"minutes",seconds,"seconds")




Code
#Python program to print even Numbers between start to end. Where start and end are variables containing starting and ending number.

start = input("Enter the starting number")
end = input("Enter the ending number")

 
# iterating each number in list
for num in range(start, end):
     
    # checking condition
    if num % 2 = 0:
        print(num)
        num = num + 1





Code
#Python program to print odd Numbers between start to end. Where start and end are variables containing starting and ending number.

start = input("Enter the starting number")
end = input("Enter the ending number")

 
# iterating each number in list
for num in range(start, end):
     
    # checking condition
    if num % 2 != 0:
        print(num)
        num = num + 1

Posted 4 years ago
@godviras


:tla-think:


Code
# 1. In-Place Swapping Of Two Numbers.

x, y = 10, 20
print(x, y)
x, y = y, x
print(x, y)


-- Wed Apr 01, 2020 6:24 am --

There are few instances when you want to know the path of modules. You can know the path by using print command

Code
#Print The File Path Of Imported Modules.


import os
import socket
 
print(os)
print(socket)
Posted 4 years ago · Author
Today's code for practice. </>

Code
# Program to check if a number is prime or not

num = 407

# To take input from the user
#num = int(input("Enter a number: "))

# prime numbers are greater than 1
if num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is a prime number")
       
# if input number is less than
# or equal to 1, it is not prime
else:
   print(num,"is not a prime number")


-- Wed Apr 01, 2020 7:07 am --

Code
# Python program to find the factorial of a number provided by the user.

# change the value for a different result
num = 7

# To take input from the user
#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

-- Wed Apr 01, 2020 7:08 am --

Code
# Taking kilometers input from the user
kilometers = float(input("Enter value in kilometers: "))

# conversion factor
conv_fac = 0.621371

# calculate miles
miles = kilometers * conv_fac
print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))

-- Wed Apr 01, 2020 7:09 am --

Code
# Python Program to convert temperature in celsius to fahrenheit

# change this value for a different result
celsius = 37.5

# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))


-- Sat Apr 04, 2020 8:10 am --

Python program to create pyramid structures using characters.

Code
# Create using print function
print("     *     ")
print("    ***    ")
print("   *****   ")
print("  *******  ")
print(" ********* ")
print("***********")



Creating with Print function needs to write long codes. And we are using programming to make our work easier, so we will see some other ways to create pyramid.

Code
# Creating full pyramid
def full_pyramid(rows):
    for i in range(rows):
        print(' '*(rows-i-1) + '*'*(2*i+1))
       
full_pyramid(6)



Code
# Creating full inverted pyramid
def inverted_pyramid(rows):
    for i in reversed(range(rows)):
        print(' '*(rows-i-1) + '*'*(2*i+1))
       
inverted_pyramid(6)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here

SIGN IN NOW

Create an account

Sign up for a new account in our community. It's easy!

REGISTER A NEW ACCOUNT