PYTHON PROGRAMMING
ARTIFICIAL INTELLIGENCE – 417
CBSE GRADE 10
WORKING WITH JUPYTER NOTEBOOK
• Launch Anaconda Navigator
• Click on LAUNCH button below Jupyter Notebook
• Jupyter notebook is web-based, open in browser
• Click on NEW and select Python 3 to create a notebook for
executing programs
Token and Keyword
• The smallest individual unit in a program is known as token.
• Keywords
• Identifiers (Names)
• Literals
• Operators
• Punctuators
• A keyword is a word having special meaning reserved by the
programming language.
LITERALS
• String Literals
• Numeric Literals
• Boolean Literals
• Special Literal
Operators
Arithmetic Relational Logical
• Unary Operators: • < Less than
• AND
• +, -, Not, ~ • > Greater than
• OR
• Binary Operator: • <= Less than equal to
• + Addition 6+5 • >= Greater than equal to
• * Multiplication 6*5 • == Equal to
• - Subtraction 6-5 • != Not Equal to
• / Division 6/5 • <>Not Equal to
• % Remainer/Modulus 6%5
• // Floor Division 6.5//2
• ** Exponent 6**2
Working of Logical Operators
• (4==4) or (5==8) gives True
• 5>8 or 5<2 gives false
• (4==4) and (5==8) gives false
• 8>5 and 2<5 gives true
• Not(5>2) gives false
• Not(3==5) gives true
Operator Precedence
Python Indentation
• Indentation refers to the spaces at the beginning of a code line.
• Python uses indentation to indicate a block of code.
•
Python Comments
• Comments can be used to explain Python code.
• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.
•
Python Variables
• Variables are containers for storing data values.
•
A variable is created the moment you first assign a value to it.
•
Casting
• If you want to specify the data type of a variable, this can be
done with casting.
•
VARIABLE AND ASSIGNMENTS
• Dynamic Typing
• X=10
• Print(X)
• X= “Hello World”
• Print(X)
• Static Typing
• Datatype is attached with a variable
Multiple Assignments
• Assigning same value to multiple variables
• A=b=c=10
• Assigning multiple values to multiple
variables
• x,y,z=10,20,30
• x,y=y,x (swaps values)
DATA TYPES
• NUMBER – Integers, Long integers, Boolean, Float
• STRING – ‘boy’ “girl” “1234G”
• LIST: [1,2,3,4,5] or a=[‘a’, ‘e’, ‘I’ , ‘o’, ‘u’] o4r z= [‘Neha’,
102, 79.5]
GETTING USER INPUT
• Str=input(“Enter your name:”) Enter your name: Jiva
Hello Jiva
• Print(“Hello”, str)
• Value=float(input(“Enter Packet’s Weight:”))
• Print(value,”kg”) Enter Packet’s Weight: 15.7
15.7 Kg
• age=int(input(“Enter your age:”))
• Print(“you are”, age) Enter your age: 20
you are 20
PYTHON CONDITIONALS AND
LOOPS
ARTIFICIAL INTELLIGENCE – 417
CBSE GRADE 10
The IF statement
• An "if statement" is written by using the if keyword.
The IF statement
Indentation
Python relies on indentation (whitespace at the beginning of a line) to
define scope in the code. Other programming languages often use curly-
brackets for this purpose.
The if-else statement
The else keyword catches anything which isn't caught by the preceding conditions.
The range() Function and Operators
in, not in
Range (0,5)=[0,1,2,3,4]
Range(12,18) = [12,13,14,15,16,17]
Range(0,10,2) = [0,2,4,6,8]
Range(5,0,-1)=[5,4,3,2,1]
Operators in and not in
• 3 in [ 1,2,3,4] will return True
• 5 in [1,2,3,4] will return False
THE FOR LOOP
• The FOR loop of Python is designed to process the items of any sequence
such as a list or a string one by one.
• For<variable> in <sequence>:
statements_to_repeat
For a in [1,4,7]:
Print(a)
For val in range(3,18)
print(val)
The while loop
A while loop is a conditional loop that will repeat the instructions
within itself as long as a conditional remains true.
While <logicalexpression>
loop-body
A=5
While a>0:
print(“hello”, a)
a=a-3
Print(“loop over!!”)