Академический Документы
Профессиональный Документы
Культура Документы
import math
a=input(“Enter the coefficient of x2”)
b=input(“Enter the coefficient of x”)
c=input(“Enter the constant term”)
root1=math.pow((-b+(math.pow(b,2))-4*a*c),0.5)/(2*a)
root2=math.pow((-b-(math.pow(b,2))-4*a*c),0.5)/(2*a)
x=math.pow(b,2)-4*a*c
if x==0:
print(“roots are positive and equal”)
elif x>0:
print(“roots are positive and distinct”)
elif c<0:
print(“no real rootsl”)
Iterated Statements
• While loop
• For loop
1. WHILE loop
It provides a mechanism to repeat one or more statements
while a particular statement is true.
Syntax : statement x #initialisation
while(condition):
statement
updation
statement
Write a programme to find the sum of first 15 natural number
a=0
x=1
while(x<=15)
print(x,’\t’)
x=x+1
2. FOR loop
It is used similar to while loop to iterate a task until a given
condition is true. For loop is best in case of knowing exactly the
no.of iterations that is a definite no.of steps.
Syntax : for <control variable> in sequence:
Statement
Statements
range()
Built-in function that is used to iterate over a sequence of a
number.
Syntax : range(beginning, end,[step])
Break Statement
Keyword used to terminate the loop execution of the current
loop.
Continue Statement
Keyword used to skip an iteration of the current loop.
Pass Statement
Used when a statement is required syntactically not logically. It
specifies a null operation.
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
Also, a list can even have another list as an item. This is called
nested list.
# nested list
tuple
Example
my_tuple = (1, 2, 3)
print(my_tuple)
(1,2,3)
String
A string is a sequence of characters.
Example
my_string = 'Hello'
print(my_string)
output
‘Hello’
Sets
my_set = {1, 2, 3}
print(my_set)
output
{1,2,3}
Dictionary
Example
def function_name(parameters):
"""docstring"""
statement(s)
example
def greet(name):
parameter"""
greet('Paul')
def calc_factorial(x):
if x == 1:
return 1
else:
return (x * calc_factorial(x-1))
num = 4
output
The factorial of 4 is 24
object-oriented programming
Python is an object-oriented programming language.
What this means is we can solve a problem in Python by
creating objects in our programs.
Object
An object is an entity that has attributes and behaviour. For
example, Ram is an object who has attributes such as height,
weight, color etc. and has certain behaviours such as walking,
talking, eating etc.
Class
A class is a blueprint for the objects. For example, Ram, Shyam,
Steve, Rick are all objects so we can define a template
(blueprint) class Human for these objects. The class can define
the common attributes and behaviours of all the objects.
Methods
As we discussed above, an object has attributes and behaviours.
These behaviours are called methods in programming.
class Human:
# instance attributes
def __init__(self, name, height, weight):
self.name = name
self.height = height
self.weight = weight
Height of Ram is 6
Weight of Ram is 60
Ram is eating Pizza
Weight of Steve is 5.9
Weight of Steve is 56
Steve is eating Big Kahuna Burger
Inheritance
Polymorphism
Encapsulation
Data Abstraction
Data Structure
Bubble Sort
This simple sorting algorithm iterates over a list, comparing
elements in pairs and swapping them until the larger elements
"bubble up" to the end of the list, and the smaller elements stay
at the "bottom".
Insertion Sort
Like Selection Sort, this algorithm segments the list into sorted
and unsorted parts. It iterates over the unsorted segment, and
inserts the element being viewed into the correct position of the
sorted list.
Heap Sort
This popular sorting algorithm, like the Insertion and Selection
sorts, segments the list into sorted and unsorted parts. It
converts the unsorted segment of the list to a Heap data
structure, so that we can efficiently determine the largest
element.
Merge Sort
This divide and conquer algorithm splits a list in half, and keeps
splitting the list by 2 until it only has singular elements.
Binary Search