Professional Documents
Culture Documents
Assignment Solved Questions
Assignment Solved Questions
# *Here, we are implementing a program, it willread age from the user and check
# whether person is eligible for voting or not.
# *This is a simple if else example in the python - Here, we will read the age
# of the person by using input() function and convert the entered age value to
# the integer by using int() function. Then we will check the condition, whether
# age is greater than or equal to 18 or not - if age is greater than or equal to
# 18, the person will be eligible for voting.
#input age
age = int(input("Enter age : "))
else:
print("Not Eligible for Voting!")
Enter age : 19
# In this program, we discuss a simple python program which finds the biggest
# number out of the three input numbers.
# Here we use elif statement to compare 3 numbers and find biggest
# numbers outof them.
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 1/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
In [8]:
# This statement ask user to enter three numbers and stores the user enteres
# value in variables num1, num2 and num3.
else:
biggest = num3
# *This program allows the user to enter the base and height of a triangle.
# By using the base and height values, it finds the area of a triangle.
# The mathematical formula to find Area of a triangle using base and height:
# Area = (base * height) / 2.
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 2/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
In [9]:
area = (base*height) / 2
In [14]:
# creating a list
list1 = [49, 19, 81, 31, 10, 99]
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 3/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
In [13]:
# creating a list
list1 = [3, 4, 2001, 31, 10, 99 ]
# Driver code
total = sumOfList(list1, len(list1))
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
In [28]:
# square pattern
for i in range(5):
for j in range(5):
print(i+1, end='')
print()
11111
22222
33333
44444
55555
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 4/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
In [29]:
factorial = 1
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)
Enter a number: 6
# Class methods are used when we are dealing with factory methods.
# Factory methods are those methods that return a class object for different
# use cases.Thus, factory methods create concrete implementations of a common
# interface.
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 5/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
In [38]:
# Creating a class
class Student:
def __init__(self, name, age): # Constructor to initialize
self.name = name # Instance variables
self.age = age
@classmethod
def calculate_age(cls, name, birth_year): # cls refer to the Class
# calculate age an set it as a age
# return new object
return cls(name, date.today().year - birth_year) # Access Class Variables
def show(self):
print(self.name + "'s age is: " + str(self.age)) # Modify Class Variables
Inheritance is categorized based on the hierarchy followed and the number of parent classes and subclasses
involved.
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchial Inheritance
5. Hybrid Inheritance
1. Single Inheritance :
This type of inheritance enables a subclass or derived class to inherit properties and
characteristics of the parent class, this avoids duplication of code and improves code reusability.
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 6/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
2. Multiple Inheritance :
This inheritance enables a child class to inherit from more than one parent class. This
type of inheritance is not supported by java classes, but python does support this kind of inheritance. It has
a massive advantage if we have a requirement of gathering multiple characteristics from different classes.
3. Multilevel Inheritance :
In multilevel inheritance, the transfer of the properties of characteristics is done to
more than one class hierarchically. To get a better visualization we can consider it as an ancestor to
grandchildren relation or a root to leaf in a tree with more than one level.
4. Hierarchical Inheritance :
This inheritance allows a class to host as a parent class for more than one child
class or subclass. This provides a benefit of sharing the functioning of methods with multiple child classes,
hence avoiding code duplication.
5. Hybrid Inheritance :
An inheritance is said hybrid inheritance if more than one type of inheritance is
implemented in the same code. This feature enables the user to utilize the feature of inheritance at its best.
This satisfies the requirement of implementing a code that needs multiple inheritances in implementation.
In Python, an exception is an object derives from the BaseException class that contains information about an
error event that occurred within a method. Exception object contains:
1. FileNotFoundException
2. ImportError
3. RuntimeError
4. NameError
5. TypeError
1. Standardized error handling: Using built-in exceptions or creating a custom exception with a more precise
name and description, you can adequately define the error event, which helps you debug the error event.
2. Cleaner code: Exceptions separate the error-handling code from regular code, which helps us to maintain
large code easily.
3. Robust application: With the help of exceptions, we can develop a solid application, which can handle error
event efficiently.
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 7/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
4. Exceptions propagation: By default, the exception propagates the call stack if you don’t catch it. For
example, if any error event occurred in a nested function, you do not have to explicitly catch-and-forward it;
automatically, it gets forwarded to the calling function where you can handle it.
5. Different error types: Either you can use built-in exception or create your custom exception and group them
by their generalized parent class, or Differentiate errors by their actual class.
Inbuilt functions int(), float() and str() shall be used for typecasting.
1. "int()" can take a float or string literal as argument and returns a value of class 'int' type.
2. "float()" can take an int or string literal as argument and returns a value of class 'float' type.
3. "str()" can take a float or int literal as argument and returns a value of class 'str' type.
We shall print both the value and type of the float and string variables.
In [40]:
#integer
n = 100
#float
f = float(n)
print(f)
print(type(f))
#string
s = str(n)
print(s)
print(type(s))
100.0
<class 'float'>
100
<class 'str'>
In the following program, we shall initialize a variable with float value. In the next
statement, we typecast this float to integer using int(). Later we typecast the float to
string.
In [41]:
#float
f = 100.05
#integer
n = int(f)
print(n)
print(type(n))
#string
s = str(f)
print(s)
print(type(s))
100
<class 'int'>
100.05
<class 'str'>
In [43]:
#string
s = '132'
#typecast to integer
n = int(s)
print(n)
print(type(n))
#typecast to float
f = float(s)
print(f)
print(type(f))
132
<class 'int'>
132.0
<class 'float'>
* *
* * *
* * * *
* * * * *
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 9/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
In [51]:
# number of spaces
k = n - 1
# printing stars
print("* ", end="")
# Driver Code
n = 5
triangle(n)
* *
* * *
* * * *
* * * * *
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 10/11
6/26/22, 2:23 AM Untitled9 - Jupyter Notebook
In [52]:
# square pattern
# implementing for loop
for i in range(5):
for j in range(5):
print(j+1, end='')
print()
12345
12345
12345
12345
12345
localhost:8888/notebooks/Untitled9.ipynb?kernel_name=python3#*-Write-a-python-program-to-print-the-following-pattern-using-loop-concept 11/11