This page serves budding programmer (who are new to Python) with several python programming snippets. You can take advantages of this small/medium program to learn the basic Python 3 syntax and apply to study/work/ or anywhere else.

**Note: **All the codes are written in Python 3.x (any sub-version will work). You can search for the sample code from the **search bar** placed at the right sidebar.

**Sample Python codes**

#### P1- Python program to compare two numbers

```
num1 = 10
num2 = 20
if num1 > num2:
print ("num1 is greater than num2")
else:
print ("num2 is greater than num1")
```

#### P2 – Python program to calculate distance when initial velocity and acceleration are given

```
# This is a comment line and in python, comment line (line starts with #) are ignored by python interpreter
# formula: s = ut + 1/2 at²
# s- distance, u - initial velocity, a- acceleration(m/s²), t- time
u = 10 # unit - m/s
t = 20 # unit - s
a = 1.2 # unit - m/s²
s = u*t + 0.5*(a*(t**2))
print ("Ditance is: ", s)
```

#### P3 – Python program to find real and imaginary part of a complex number

```
z = 3+4j # z is a complex number
print ("Real part of complex number is: ",z.real)
print ("Imaginary part is: ", z.imag)
```

#### P4 – Program to find month name by its number

```
month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
x = int(input("Enter a number to find month name: "))
if x>0 and x<= 12:
print ("The name of month is -- ", month[x-1])
else:
print ("Enter a valid month number")
```

#### P5 – Difference between 30 and ‘30’ and 30.0 in python

```
# for python number and text are NOT same
# if we write,
x = 30 # this is integer
y = 30.0 # this is float
z = '30' # this is string
# You can validate by checking the data type of each declared variable
>>> print (type(x))
< class 'int'>
>>> print (type(y))
< class 'float'>
>>> print (type(z))
<class 'str'>
```

#### P6 – Python program to print index position elements from the tuple

```
x = (10, 20, 30, 40, 60, 70) # consider this tuple
for i in range(len(x)):
print ("{} - is at position {} ".format(x[i], i))
# Output
# 10 - is at position 0
# 20 - is at position 1
# 30 - is at position 2
# 40 - is at position 3
# 50 - is at position 4
# 60 - is at position 5
# 70 - is at position 6
```

#### P7 – Program to print the factorial of a no in python

```
# factorial of a number 5 is 5*4*3*2*1 = 120
# factorial of 0 is 1
# factorial of -ve number doesn't exist.
f_num = int(input("Enter a number to find factorial: "))
fact = 1 # for storing factorial
if f_num==0:
print ("factorial of 0 is 1")
elif f_num < 0:
print ("Not valid !! Please enter a positive number")
else:
for i in range(1,f_num):
fact = fact*i
print ("Factorial of {} is : {}".format(f_num,fact))
```

#### P8 – Program to find the first ten (or n) prime number program in Python

```
num = int(input("Enter the numbet till which you want primer numbers: "))
count = 0 # start the counter to count prime number
start = 2 # starting number
prime_num = [2] # to store list of prime numbers
print ("List of 1st [{}] of prime numbers".format(num))
while count < num-1:
if (start+1) % 2 != 0:
prime_num.append(start+1)
count += 1
start += 1
print (prime_num)
```

#### P9 – Python program to calculate sum, difference, the product of two numbers and print all of them

```
num1 = 10
num2 = 20
sum_num = num1+num2
diff_num = num1-num2
prod_num = num1*num2
print ("Sum of {} and {} is : {}".format(num1, num2, sum_num))
print ("Difference of {} and {} is : {}".format(num1, num2, diff_num))
print ("Product of {} and {} is : {}".format(num1, num2, prod_num))
```

#### P10 – Write a program in Python to print counting Number 50 to 80

```
start_num = 50
end_num =80
for i in range(start_num,end_num+1): # 1 is added to end_num, because range function generates range excluding the end value
print (i, end=',')
```

#### P11 – Python program to print first 50 even number square using WHILE loop

```
count_num = 0 # to count the even numbers
start_num = 1 # starting number to count from
while count_num < 50:
if (start_num%2) != 0:
print (start_num, end=',')
count_num += 1
start_num += 1
```

#### P12- Python program to calculate the percentage marks of a student after taking input of 3 subjects

```
m1 = float(input("Enter the marks for 1st Subject: "))
m2 = float(input("Enter the marks for 2nd Subject: "))
m3 = float(input("Enter the marks for 3rd Subject: "))
m_total = float(input("Enter the total marks for all 3 subjects: "))
percent_mark = ((m1+m2+m3)/m_total)*100
print ("You obtained {:0.2f}% marks in 3 subject".format(percent_mark))
```

#### P13 – Python program to calculate mean, median and mode

```
# Numpy module has MEAN and MEDIAN method as built-in function
# Scipy (stats) module has MODE method as built-in function
import numpy as np
list_num = [10, 12, 40, 45, 67, 89, 100]
mean_val = np.mean(list_num)
median_val = np.median(list_num)
from scipy.stats import mode
mode_val = mode(list_num)[0][0]
print ("Mean value is: ", mean_val)
print ("Median value is: ", median_val)
print ("Mode value is: ", mode_val)
```

#### P14 – Python program to check whether the PIN entered by the user is a four-digit number or not

```
enter_pin = input("Please enter the PIN: ")
try:
int(enter_pin)
except:
print ("Please Enter only Numbers")
if len(enter_pin) > 4:
print ("PIN length should be 4 BUT you entered {} digit long pin".format(len(enter_pin)))
```

#### P15 – Python programs that print integers in a given range

```
start_val = 10
end_val = 30
print ("Integers between {} and {} ".format(start_val, end_val))
for i in range(start_val, end_val):
print (i, end=',')
```

#### P16 – How to identify whether the price is moving in a range using Python?

```
check_val = float(input("Enter a price to check: "))
lower_range = 200
upper_range = 400
if (check_val >= lower_range) and (check_val<=upper_range):
print ("Current value {} is in my range".format(check_val))
else:
print ("Current value {} is NOT in my range".format(check_val))
```

#### P17 – Write a Python program to convert age in seconds

```
print ("Provide your age in Year, Month, Day\n")
Y = int(input("Enter the age in Years: "))
M = int(input("Enter the age in Months: "))
if M > 12:
print ("!! Please enter a valid Month value")
M = int(input("Enter the age in Months: "))
D = int(input("Enter the age in Days: "))
if D > 31:
print ("!! Please enter a valid Day value")
D = int(input("Enter the age in Days: "))
age_in_s = (Y*31556952 + M*2630000 + D*86400)
print ("Your age in second is :" + str(age_in_s))
```

#### P18 – write a program in Python to display the distance in metres and feet for the values entered in kilometres

```
KM = float(input("Enter the Value in Kilometers: "))
FT = KM*3280.84
MT = KM*1000
print (" {} - in Feets is {}".format(KM, FT))
print (" {} - in Meters is {}".format(KM, MT))
```

#### P19 – Input a code in python to input 4 digit number and reverse its first and last digit

```
Num = int(input("Provide a Four(4) digit number: "))
num_chr = str(Num)
t1 = num_chr[0]
t4 = num_chr[3]
F = t4+num_chr[1]+num_chr[2]+t1
print ("{} after swapping became {}".format(Num, int(F)))
```

#### P20 – Write a Python program to print the five names of states using for loop

```
state = ['Karnataka','Delhi','Goa','Punjab','Bihar']
print ("printing the names of five states\n")
for names in state:
print (names)
```

#### P21 – WAP to enter digit and corresponding digit name in python

```
name = ['Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten']
i = 0
while i<= 10:
M = int(input("Enter a digit: "))
print ("Digit {} in word is : {}".format(M, name[M]))
i += 1
```

#### P22 – Write a program in Python to calculate the selling price of an item whereas cost price and profit are user define

```
# selling price = cost price + profit | seeling price = cost price - loss
CP = float(input("Enter the Cost price : "))
P = float(input("Enter the Profit : "))
SP = CP+P
print (" CP is {} and Profit is {} then SP will be : {}".format(CP, P, SP))
```

#### P23 – Python program to create variable names for each item of the list and print the same.

```
L = [10, 11, 12, 13, 14, 15]
# Create an empty var list to store variable name.
var_list = []
# Create variable name with incremental value of 1
for i in range(len(L)):
var_list.append("temp{0}".format(i))
# Print each variable name with value
for j in range(len(var_list)):
print ("Variable name is '{}' and assigned value is: [{}]".format(var_list[j],L[j]))
```

Many more coming up soon… Stay Tune !!