Unit 4 AI - Python
Unit 4 AI - Python
Unit 4 AI - Python
Introduction to Python
Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he
decided to call the language Python. When he began implementing Python, Guido van Rossum was
also reading the published scripts from “Monty Python's Flying Circus”, a BBC comedy series from
the 1970s.so he thought that naming his Language as Python would grab the attention of the all.
What is Python?
• Python is an object-oriented, high-level programming and interpreted language. First launched in 1992.
Why Python?
• Python works on different platforms like Windows, Mac, Linux etc
• Python has a simple syntax similar to the English language. It allows developers to write programs with
fewer lines than some other programming languages.
• Python runs on an interpreter system, meaning that code can be executed as soon as it is written.
• Can easily interact with the database systems and handle big data and perform complex mathematics
Features of Python programming Applications of Python:
▪ Python is a case-sensitive language.
▪ Web and Internet Development
▪ Easy To Learn & Use While Coding.
▪Desktop GUI Application
▪ Clear syntax and a simple keyword structure.
▪ Extensible Feature. ▪Software Development
▪ Interpreted Language.
▪Database Access
▪ Expressive Language.
▪Business Application
▪ Cross-Platform Portable Language.
▪ Dynamic Memory Allocation. ▪Games and 3D Graphics
▪ High-Level Interpreted Language.
▪ Graphical User Interface (GUI) Support:
Python Code Editors
• IDLE
• Replit
• PyCharm
• Visual Studio Code
Number Datatype
Numerical Values are stored in the Number data type. There are four categories of number datatype –
1.Int – Int datatype is used to store the whole number values. Example : x=500
2.Float – Float datatype is used to store decimal number values. Example : x=50.5
Python Output Using print() function
Example Code Sample Output
a = 200
b = 100 500
print(a + b)
print(45 + 35) 80
print("My name is Kajal") My name is Kajal
a = “Nisha"
My name is : Nisha
print("My name is :",a)
x = 56.5 x=
print("x = \n", x) 56.5
m=6
I have 6 apples
print(" I have",m,"apples")
a="My"
b="India" MyIndia (Concatenation of string)
print(a+b)
1. The end parameter
The end parameter is used to append any string at the end of the output of the print statement in python.
By default, the print the method ends with a new line. This means there is no need to explicitly specify the parameter
end as '\n'. Programmers with a background in C/C++ may find this interesting.
Example with out end parameter
print(“Your Performance") # By default, the print the method ends with a new line.
print("is awesome")
Output:
Your Performance
is awesome
Example with end parameter
print(“Your Performance“, end=“ “ ) # end parameter indicates that the end character has to be identified by
whitespace and not a new line
print("is awesome")
Output:
Your Performance is awesome
2. The sep parameter
The sep parameter is primarily used to format the strings that need to be printed on the console and add a
separator between strings to be printed. This feature was newly introduced in Python 3 version.
Example with out sep parameter
print("Excellent","performance",sep="#")
print("Study","Score",sep="&")
Output:
Excellent#performance
Study & Score
The end parameter is used to specify a string that will be printed after the output. However, the
sep parameter is used as a separator between the items that you want to print.
Flow of control and conditions
Conditional Statement in Python perform different computations
or actions depending different situations. There are 3 control
flow statements in Python - if, for and while
Decision making statements in programming languages decide the direction of flow of
program execution. Decision making statements available in Python are:
● if statement ● if .. else statements ● if-elif ladder
If Statement
The if statement is used to check a condition: if the condition is
true, we run a block of statements (called the if-block).
Syntax Sample Output
Enter a Number -9
# To check if the entered Number is Positive try again
n = int(input("Enter a Number"))
if n>0: Enter a Number 5
print(n," is positive number") 5 is positive number
print("try again") try again
if...else Statement
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.
Syntax Sample Output
#check if the student is pass or fail Enter the mark 89
n = int(input("Enter the mark")) Result :PASS
if n>=40:
print("Result :PASS") Enter the mark 35
else: Result :FAIL
print("Result :FAIL")
if..elif.. else Statement
The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is
False, it checks the condition of the next elif block and so on. If all the conditions are False, body of
else is executed. The if block can have only one else block. But it can have multiple elif blocks.. But
only block among several if...elif...else blocks is executed based on the condition
#To check the discount for Entry for Global Village Enter the age 3
Syntax age=int(input("Enter the age")) Junior Discount
if age >= 60:
print('Senior Discount') Enter the age 65
elif 4 <= age < 60: Senior Discount
print('No Discount')
else: Enter the age 4
print('Junior Discount') No Discount
Find Errors in the following Python program
Find error(s) in the following code rewrite the code and output (answers are given in pink)
1. Print("Anuj") #print("Anuj")
2. age =Int(input(“Enter the age of the person”) # int(input(“Enter the age of the person”))
3. If i =< 5 # if i <= 5:
4. print(5,6,2030,Sep=‘/’) # print(5,6,2030,sep='/’)
5. print(“ Welcome to ”, End=“ “) # print(“ Welcome to ”, end=“ “)
6. x= int(“Enter value of x:”)
if x=y
print("They are equal")
else:
Print( "They are unequal")
7.
a = input("Enter any number")
if a%2=0: # if a%2==0:
print("Even number)
Else # else:
print("Odd number")
Find Errors in the following Python program
Find error(s) in the following code rewrite the code and output
8.
x = int(Input("Enter any number")) # x = int(input("Enter any number"))
y = int(input("Enter any number"))
If x>y # if x>y:
print("First number is greater"))
else:
Print("Second number is greater") # print("Second number is greater")
2.
x,y=25,50
Output:
print("Before Swapping x=",x,"y=",y)
Before Swapping x= 25 y= 50
x,y=y,x After Swapping x= 50 y= 25
print("After Swapping x=",x,"y=",y)
3.
print("Welcome to", end=" ") Output:
print("explore the world of Python") Welcome to explore the world of Python
4. Output:
print("*"*25)
print("I like Python programming") *************************
I like Python programming
print("*"*25)
*************************
Different types of Loop statements are :
Parameter :
•start: [ optional ] start value of the sequence
Default the start value starts from 0
•stop: next value after the end value of the sequence. Should be integer only
• step: [ optional ] integer value, denoting the difference between any two numbers
in the sequence
Default the step count is 1
List Index
A list index is the position at which any element is present in the list. Index in the list
starts from 0, so if a list has 5 elements the index will start from 0 and go on till 4. In
order to access an element in a list we need to use index operator [].
Negative Indexing
Python allows negative indexing for its sequences. The index of -1 refers to the last item,
-2 to the second last item and so on.
Trying to access indexes other than the given range will raise an IndexError.
Some of the Methods which can be used with lists are mentioned below:
Method Description
append() Adds an element at the end of the list. Syntax : Listname.append(value)
clear() Removes all the elements from the list. Syntax : Listname.clear()
len() Returns the number of items in the list. Syntax : len(listname)
count() Returns the number of elements with the specified value. listname.count(value)
extend() Add more than 1 element to the end of the list. lname.extend([list of values])
index() Returns the index of the first element with the specified value. ln.index(value)
insert() Adds an element at the specified position. Syntax : Listname.insert(position,value)
pop() Removes the element from the specified position. Syntax : Listname.pop(position)
remove() Removes the first item with the specified value. Syntax : Listname.remove(value)
reverse() Reverses the order of the list. Syntax : Listname.reverse()
sort() Sorts the list. Syntax : Listname.sort()
del() Deletes the Items from the list Syntax : del.listname or del.listname[start val:end val+1]
Find output of the following Python program
Program
color =["Violet","Indigo","Blue","Green","Yellow"]
print("***** List items are *****")
print(*color, sep="\n")
Output
***** List items are *****
Violet
Indigo
Blue
Green
Yellow
Program
numlist=[2,6,4,5,3,8,7,5]
print(numlist[-7:-2])
Output
[6, 4, 5, 3, 8]