0% found this document useful (0 votes)
6 views10 pages

Python Programs

The document provides an overview of Python programming concepts including indentation, variables, comments, and basic arithmetic operations. It explains how to create variables, use input functions, and implement control structures like loops and conditionals. Additionally, it covers functions for calculating factorials, Fibonacci numbers, and the area of a circle, along with examples of basic list operations and class creation.

Uploaded by

badmashboy723421
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
6 views10 pages

Python Programs

The document provides an overview of Python programming concepts including indentation, variables, comments, and basic arithmetic operations. It explains how to create variables, use input functions, and implement control structures like loops and conditionals. Additionally, it covers functions for calculating factorials, Fibonacci numbers, and the area of a circle, along with examples of basic list operations and class creation.

Uploaded by

badmashboy723421
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

print("Hello, World!

")

Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.

if 5 > 2:
print("Five is greater than two!")

Python will give you an error if you skip the indentation:


if 5 > 2:
print("Five is greater than two!")

The number of spaces is up to you as a programmer, the most common


use is four, but it has to be at least one.

if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")

You have to use the same number of spaces in the same block of
code, otherwise Python will give you an error:

if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")

Python Variables
In Python, variables are created when you assign a value to it:
x = 5
y = "Hello, World!"

_
Comments
Python has commenting capability for the purpose of in-code
documentation.
Comments start with a #, and Python will render the rest of the
line as a comment:
#This is a comment.
print("Hello, World!")

print("Hello, World!") #This is a comment

#print("Hello, World!")
print("Cheers, Mate!")

#This is a comment
#written in
#more than just one line
print("Hello, World!")

"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")

Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and
can even change type after they have been set.

x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)

# This program adds two numbers

num1 = 1.5
num2 = 6.3

# Add two numbers


sum = num1 + num2

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

# Store input numbers


num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2) #float datatype use to store number
+ or -

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
We use the built-in function input() to take the input.
Since, input() returns a string, we convert the string into number
using the float() function. Then, the numbers are added.

# Python program to check if the input number is odd or even.


# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

num = int(input("Enter a number: "))


if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

# Sum of natural numbers up to num

num = 16

if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
_

Find the Factorial of a Number Using Recursive approach

# Python 3 program to find


# factorial of given number
def factorial(n):

# single line to find factorial


return 1 if (n==1 or n==0) else n * factorial(n - 1)

# Driver Code
num = 5
print("Factorial of",num,"is",factorial(num))

Find the Factorial of a Number Using Iterative approach

# Python 3 program to find


# factorial of given number
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
# Driver Code
num = 5
print("Factorial of",num,"is",
factorial(num))

Python Program for n-th Fibonacci number Using recursion

# Function for nth Fibonacci number

def Fibonacci(n):
if n<= 0:
print("Incorrect input")
# First Fibonacci number is 0
elif n == 1:
return 0
# Second Fibonacci number is 1
elif n == 2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)

# Driver Program

print(Fibonacci(10))

_
Python Program to Find Area of a Circle
def findArea(r):
PI = 3.142
return PI * (r*r);

# Driver method
print("Area is %.6f" % findArea(5));

Factorial Program

# import math
# def fact(num):
# i=num
# z=1
# while i<=num and i !=0:
# print(i)
# z *= i
# i -= 1
# return z
# def factorial(n):
# if n == 0:
# return 1
# else:
# return n * factorial(n - 1)
# print(fact(10))
# print(math.factorial(10))
# print(factorial(10))
# def sumOfNaturalNumber(num):
# z=0
# while 0 < num:
# z += num
# num -= 1
# return z
# print(sumOfNaturalNumber(3))
# def sumOfNaturalNumberWithForLoop(num):
# total=0
# for i in range(1, num + 1):
# print(i)
# total += i
# return total
# print(sumOfNaturalNumberWithForLoop(3))

fruits = ['apple', 'cherry', 'banana', 'cherry']


# fruits.append("orange")
# fruits.clear()
# newFruits= fruits.copy()
# # fruits.count("cherry")
# cars = ['Ford', 'BMW', 'Volvo']
# fruits.extend(cars)
# print(fruits.index("cherry"))
# fruits.insert(1, "orange")
# fruits.pop(1)
# fruits.remove("banana")
# fruits.reverse()
# fruits.sort()
fruits.sort(reverse=True)
print(fruits)

# print(newFruits)

def add(P, Q):

# This function is used for adding two numbers


return P + Q
def subtract(P, Q):
# This function is used for subtracting two numbers
return P - Q
def multiply(P, Q):
# This function is used for multiplying two numbers
return P * Q
def divide(P, Q):
# This function is used for dividing two numbers
return P / Q
# Now we will take inputs from the user
print ("Please select the operation.")
print ("a. Add")
print ("b. Subtract")
print ("c. Multiply")
print ("d. Divide")

choice = input("Please enter choice (a/ b/ c/ d): ")

num_1 = int (input ("Please enter the first number: "))


num_2 = int (input ("Please enter the second number: "))

if choice == 'a':
print (num_1, " + ", num_2, " = ", add(num_1, num_2))

elif choice == 'b':


print (num_1, " - ", num_2, " = ", subtract(num_1, num_2))

elif choice == 'c':


print (num_1, " * ", num_2, " = ", multiply(num_1, num_2))
elif choice == 'd':
print (num_1, " / ", num_2, " = ", divide(num_1, num_2))
else:
print ("This is an invalid input")

__________________
# create a class
class Room:
length = 0.0
breadth = 0.0

# method to calculate area


def calculate_area(self):
print("Area of Room =", self.length * self.breadth)

# create object of Room class


study_room = Room()

# assign values to all the attributes


study_room.length = 42.5
study_room.breadth = 30.8

# access method inside class


study_room.calculate_area()

You might also like