AI Python Notes
AI Python Notes
Program
Set of Commands/Instructions.
Programming Language
A way to write set of Instructions.
Ex- Python, C, C++, Java, C#.........
Computer Department
Python is a very popular
programming language among
beginners as well as developers.
Python is an Multi-paradigm
language.
PYTHON LANGUAGE
Python was
designed for
Socket
Python was developed by Programming
Guido Van Rossum in 1990s.
Python was named after the BBC
comedy series ‘Flying Circus’ of
Monty Python. Computer Department
Tokens/Lexical unit
The smallest individual unit in a program is
known as a token.
Python has following tokens:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators
Computer Department
Operators
Arithmetic operators
+(Addition), -(Subtraction),
*(Multiplication), //(Floor Division),
/(divide), %(Modulus), **(Power)
Computer Department
Membership operators
In, not In
Ex-
10 in [10,20,30,40,50]
True
10 not in [10,20,30,40,50]
False
Computer Department
DATA TYPES
Numbers
String
List
Complex
Tuples
Integer Dictionary
Floating point
Computer Department
Immutable Mutable
▪ Tuple ▪ List
▪ String ▪ Dictionary
▪ Number
Python Screen
Computer Department
Print() Statement
Use to give output or show message on screen.
Ex-
Print(“Welcome to Python”)
Input() Statement
Use to get data/information/string from user.
Ex-
A=input(“Enter your name”)
Or
A=int(input (“Enter a number”))
Computer Department
Variable
Labeled storage location, whose value can be
manipulated during program run.
But python variables are not storage containers,
rather python variables are lie memory references.
Ex- A=10 # Integer value
Name=“Rohan” # String value
A=B=C=10 # Multiple Assignment
A,B,C=10,20,30 #Multiple value to multiple
variable.
Comments in Python
Single line = # Single line comment
Multiple line = ””” Multi line comment”””
Computer Department
Rules to declare Identifier/Variable
•It Cannot start with a digit
•It can contain letters , digits or
underscore
•Punctuation and blanks are not allowed
•Python is case sensitive. The identifier
total is different form Total.
•Keywords cannot be used as a variable.
Computer Department
KeyWords
These are reserved words that have a pre-
defined meaning to the interpreter. They are
Easy to recognize in IDLE because they are
automatically colored orange.
Keywords in Python:
and def for is return
as del from lambda try
assert elif global not while
break else if or with
class except import pass yield
continue finally IN raise print
Computer Department
#Write a program to enter two numbers
from user and find sum of them.
Computer Department
Programs to practice
•Write a program to enter two numbers from user and
find sum of them.
•Write a program to enter two numbers from user and
find difference of them.
•Write a program to enter a number from user and find
area of square.
•Write a program to enter two numbers from user and
find area of rectangle.
•Write a program to enter Height and Radius from user
and find area of triangle.
•Write a program to enter two numbers from user and
swap values.
Computer Department
#Write a program to enter a number
two numbers from user and swap
values.
Computer Department
Type Conversion
Conversion of a type of value into another.
It is of two types.
•Implicit conversion
Conversion done by compiler itself. Also called
Automatic conversion. No data lose.
Ex- a=4.5+5
print(a) # 9.5
•Explicit conversion
Conversion done by user intervention. Also called
type casting. Data lose can occur.
Ex- a=int(4.5+5)
print(a) #9
Computer Department