Asm - Artificial Intelligence - 130470
Asm - Artificial Intelligence - 130470
WHAT IS PYTHON?
VARIETY OF
OPEN SOURCE
USAGE
PYTHON - NEGATIVES
• To write and run Python program, we need to have Python interpreter installed in our
computer. IDLE (GUI integrated) is the standard, most popular Python development
environment.
• IDLE is an acronym of Integrated Development Environment. It lets edit, run,
browse and debug Python Programs from a single interface. This environment makes it
easy to write programs.
• Python shell can be used in two ways, viz., interactive mode and script mode.
• Now, we will first start with interactive mode. Here, we type a Python statement and the
interpreter displays the result(s) immediately.
INTERACTIVE MODE SCRIPT MODE
❖ Allows us to interact with OS. ❖ Let us create and edit python source file.
❖ Helpful when your script is extremely short ❖ In script mode, You write your code in a
and you want immediate results. text file then save it with a .py extension
❖ Faster as you only have to type a command which stands for "Python". Note that you
and then press the enter key to get the can use any text editor for this, including
results. Sublime, Atom, notepad++, etc.
❖ Good for beginners who need to ❖ It is easy to run large pieces of code.
understand Python basics. ❖ Editing your script is easier in script mode.
❖ Good for both beginners and experts.
STARTING IDLE
NOTE :
PYTHON IS A CASE-
SENSITIVE LANGUAGE
PRACTICAL QUESTIONS
v) vi)
PRACTICAL #1
QUES 1) WRITE A PYTHON PROGRAM TO ACCEPT TWO NUMBERS FROM THE USER AND ADD THEM.
num3=num1+num2
a = 10
b = 20
print(“Before swapping\na=", a, " b=", b)
temp = a
a=b
b = temp
print("\n After swapping\n a=", a, " b=", b)
a=45
b=78
a,b=b,a
print("\n After swapping\n a=", a, " b=", b)
QUES 3) WRITE A PYTHON PROGRAM TO PRINT A STRING 10 TIMES.
X="PYTHON ! "
print (X*10)
QUES 4) WRITE A PYTHON PROGRAM TO DEMONSTRATE ARITHMETIC OPERATORS.
print('x + y =',x+y)
print('x - y =',x-y)
print('x * y =',x*y)
print('x / y =',x/y)
print('x // y =',x//y)
print('x ** y =',x**y)
QUES 5) WRITE A PYTHON PROGRAM TO CALCULATE AND DISPLAY SIMPLE INTEREST TAKING
PRINCIPLE,RATE AND TIME AS USER INPUT.
#simple interest
P = float(input("enter amount"))
R = float(input("enter rate"))
T = float(input("enter time"))
# Calculates simple interest
SI = (P * R * T) / 100
# Print the value of SI
print("simple interest is", SI)
QUES 6) WRITE A PYTHON PROGRAM TO DEMONSTRATE VARIOUS RELATIONAL OPERATORS.
#to show the various relational operators
x=int(input("enter first number"))
y=int(input("enter second number"))
print('x > y is‘ , x>y)
print('x < y is', x<y)
print('x == y is', x==y)
enter first number78
enter second number90
print('x != y is', x!=y)
x > y is False
print('x >= y is', x>=y)
print('x <= y is', x<=y)
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True
>>>
Write a Python program to calculate surface volume and area of a cylinder .
pi=22/7
height = float(input('Height of cylinder: '))
radius= float(input('Radius of cylinder: '))
volume = pi * radius * radius * height
sur_area = ((2*pi*radius) * height) + ((pi*radius**2)*2)
print("Volume is: ", volume)
print("Surface Area is: ", sur_area)
Write a Python program to calculate and display
the percentage of a student. Take user input for 5
subjects.
• #to calculate percentage of 5 subjects taken from the user assuming out of 50
• List is a sequence of values of any type. It can have any number of items and
they may be of different types (integer, float, string etc.).
• Values in the list are called elements / items.
• List is enclosed in square brackets.
• Example: a = [1,2.3,"Hello"]
HOW TO CREATE A LIST?
#empty list
empty_list = []
#list of integers
age = [15,12,18]
#list with mixed data types
student_height_weight = ["Ansh", 5.7, 60]
HOW TO ACCESS ELEMENTS OF A LIST?
The pop() method removes the specified index, (or the last item if index is not specified):
thislist = ["apple", "banana", "cherry", "mango"]
thislist.pop(2)
thislist.pop()
print(thislist)
SLICING A LIST
Python List Methods
append() - Add an element to the end of the list
extend() - Add all elements of a list to the another list
insert(i,x) - Insert an item at the defined index Insert an item at a given
position. The first argument is the index of the element before which to
insert, so a.insert(0, x) inserts at the front of the list,
and a.insert(len(a), x) is equivalent to a.append(x)
remove() - Removes an item from the list
pop() - Removes and returns an element at the given index
Python List Methods
.
>>> fruits count('apple')
2
.
>>> fruits count('tangerine')
0
.
>>> fruits index('banana')
3
.
>>> fruits index('banana', 4) # Find next banana starting a position 4
6
.
>>> fruits reverse()
>>> fruits ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
.
>>> fruits append ('grape')
>>> fruits ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
.
>>> fruits sort()
>>> fruits ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
.
>>> fruits pop () 'pear' # removes the specified item in the given index or removes the last item
The index() method searches an element in the list and returns its index.
TUPLES IN PYTHON
Tuple is a collection of Python objects which is ordered and unchangeable.
The sequence of values stored in a tuple can be of any type, and they are
indexed by integers.
Example :
thistuple = ("apple", "banana", "cherry")
print(thistuple)
ACCESSING OF TUPLES
❑ We can use the index operator [] to access an item in a tuple where the index starts
from 0.
❑ Trying to access an element outside of tuple (for example, 6, 7,...) will raise an
IndexError.
❑ The index must be an integer; so, we cannot use float or other types. This will result in
TypeError.
❑ Example fruits = ("apple", "banana", "cherry") print(fruits[1]) The above code will give the
output as "banana"
DELETING A TUPLE
Tuples are immutable and hence they do not allow deletion of a part of it. Entire tuple
gets deleted by the use of del() method.
Example :
num = (0, 1, 2, 3, 4)
del num
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the
range.
When specifying a range, the return value will be a new tuple with the specified items.
DECISION MAKING STATEMENTS
The if..else statement evaluates test expression and will execute body of if only when test
condition is True. If the condition is False, body of else is executed. Indentation is used to
separate the blocks.
➢ The while statement allows you to repeatedly execute a block of statements as long as a condition is true.
➢ A while statement can have an optional else clause.
➢ In Python, the body of the while loop is determined through indentation. Body starts with indentation and
the first unindented line marks the end.
➢ Python interprets any non-zero value as True. None and 0 are interpreted as False.
# Program to add natural numbers upto sum = 1+2+3+...+n
# To take input from the user,
n = int(input("Enter n: "))
#n = 10
# initialize sum and counter
sum = 0
i=1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is", sum)