Python Programming Law Students-S1&S2
Python Programming Law Students-S1&S2
Programming
Dr. A. Ajina
Associate Professor
Department of AI &ML
RIT
DEVOTION TO ENLIGHTENMENT
Components of Program
• input Get data from the keyboard, a file, or some other
device.
• output Display data on the screen or send data to a file
or other device.
• math Perform basic mathematical operations like
addition and multiplication.
• conditional execution Check for certain conditions and
execute the appropriate sequence of statements.
• repetition Perform some action repeatedly, usually
with some variation.
4
DEVOTION TO ENLIGHTENMENT
What is Programming
Features of Python
DEVOTION TO ENLIGHTENMENT
https://www.menti.com/alr3yicfgx8p
DEVOTION TO ENLIGHTENMENT
Programming Editors
DEVOTION TO ENLIGHTENMENT
Installation demo
DEVOTION TO ENLIGHTENMENT
1
1
DEVOTION TO ENLIGHTENMENT
What is debugging?
Programming errors are called bugs and the process of tracking them down and
correcting them is called debugging.
• Runtime errors
• Syntax errors • Error does not appear
• Syntax refers to the structure until you run the
of a program and the rules program. These errors
about that structure. are also called
• Python will display an error exceptions because
message and quit, and you will they usually indicate
not be able to run your that something
program. exceptional (and bad)
has happened.
• Semantic errors
• If there is a semantic error in your
program, it will run successfully, in the
sense that the computer will not
generate any error messages, but it will
not do the right thing.
DEVOTION TO ENLIGHTENMENT
print("Hello, World!")
DEVOTION TO ENLIGHTENMENT
1
5
DEVOTION TO ENLIGHTENMENT
Variables
Variables
https://www.menti.com/alrk6zi4t2b5
DEVOTION TO ENLIGHTENMENT
https://www.menti.com/alv91wc7mxik
DEVOTION TO ENLIGHTENMENT
1. Numbers: Number data types store numeric values. Number objects are created
when you assign a value to them.
2. Strings: Strings in Python are identified as a contiguous set of characters
represented in the quotation marks. Python allows either pair of single or double
quotes.
3. Lists: Lists are the most versatile of Python's compound data types. A list
contains items separated by commas and enclosed within square brackets ([ ]).
4. Tuples: A tuple is another sequence data type that is similar to the list. A tuple
consists of a number of values separated by commas. Unlike lists, however,
tuples are enclosed within parenthesis.
5. Dictionary: Python's dictionaries are kind of hash-table type. They work like
associative arrays or hashes found in Perl and consist of key- value pairs. A
dictionary key can be almost any Python type, but are usually numbers or
strings. Values, on the other hand, can be any arbitrary Python object.
Dictionaries are enclosed within curly braces.
DEVOTION TO ENLIGHTENMENT
Data types
DEVOTION TO ENLIGHTENMENT
String
A string represents a sequence of characters or
alphabets. It can be used to store text-based
information. The string is like a collection of letters,
words, or sentences.
# create a string using double quotes
string1 = "Python programming"
# create a string using single quotes
string2 = 'Python programming'
name = "Rahul"
print(name)
DEVOTION TO ENLIGHTENMENT
Example
Example
Integer
Float
Program
pie = 3.14
Radius = 5 .3
area_of_the_circle = pie * Radius * Radius
print (" The area of the given circle is: ", area_of_
the_circle)
DEVOTION TO ENLIGHTENMENT
Boolean
List
Example
list1=["hello",1,4,8,"good"]
print(list1)
# assigning values ("hello" is replaced with "morning")
list1[0]="morning"
print(list1)
print(list1[4])
print(list1[-1])
# list also allow negative indexing
print(list1[1:4]) # slicing
list2=["apple","mango","banana"]
print(list1+list2) # list concatenation
DEVOTION TO ENLIGHTENMENT
Program
Program
Tuple
Program
Program
Program
Program
Program
Write a Python program to get the 4th element from the last element of a
tuple.
DEVOTION TO ENLIGHTENMENT
Program
Write a Python program to get the 4th element from the last element of a
tuple.
#Get an item of the tuple
tuplex = ("w", 3, "r", "e", "s","c", "e")
print(tuplex)
#Get item (4th element)of the tuple by index
item = tuplex[3]
print(item)
DEVOTION TO ENLIGHTENMENT
Dictionary
Set
A set is an unordered collection of unique elements. It is a
data type that allows storing multiple values but
automatically eliminates duplicates.
# create a set named student_id
student_id = {112, 114, 116, 118, 115}
# display student_id elements
print(student_id)
# display type of student_id
print(type(student_id))
DEVOTION TO ENLIGHTENMENT
• >>> int(3.14)
•3
• >>> float(17)
• 17.0
• >>>
float("123.45")
• 123.45
• >>> str(17)
• ’17’
• >>> str(123.45) 16
• ’123.45’
DEVOTION TO ENLIGHTENMENT
Quiz
Input
Example
Example
# using + operator
result = greet + name
print(result)
Self -test
Evaluating expressions
>>> 1 + 1
2
>>>
len("hello")
5
DEVOTION TO ENLIGHTENMENT
Operators
DEVOTION TO ENLIGHTENMENT
Operations on strings
Order of operations
When more than one operator appears in an expression, the
order of evaluation depends on the rules of precedence.
PEMDAS order of operation is followed in Python :
Parentheses have the highest precedence and can be used to
force an expression to evaluate in the order you want.
Exponentiation has the next highest precedence,
Multiplication and Division have the same precedence,
which is higher than
Addition and Subtraction, which also have the same
precedence.
Operators with the same precedence are evaluated from left
to right.
DEVOTION TO ENLIGHTENMENT
Quiz
Quiz
• 33
• 108
• 36
DEVOTION TO ENLIGHTENMENT
Arithmetic operators
x = 15
y=3
a=2
b=3
print(x + y) #addition
print(x - y) #substraction
print(x * y)#Multiplication
print(x /y)#division
print(x %y)#modulus
print(a ** b)#Exponentiation
print(x //y)#Floor
DEVOTION TO ENLIGHTENMENT
Quiz
4
4.0
DEVOTION TO ENLIGHTENMENT
Assignment operators
x %= 3
print(x)
x //= 3
print(x)
x **= 3
print(x)
x &= 3
print(x)
x>>= 3
print(x)
x <<= 3
print(x)
DEVOTION TO ENLIGHTENMENT
Comparison operators
Logical operators
Identity operators
Identity operators are used to compare the objects, not if they
are equal, but if they are actually the same object, with the
same memory location
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is z)
# returns True because z is the same object as x
print(x is y)
# returns False because x is not the same object as y, even if
they have the same content
print(x == y)
# to demonstrate the difference betweeen "is" and "==": this
comparison returns True because x is equal to y
DEVOTION TO ENLIGHTENMENT
print(x is not z)
# returns False because z is the same object as x
print(x is not y)
# returns True because x is not the same object as y, even if they have the
same content
print(x != y)
# to demonstrate the difference betweeen "is not" and "!=": this comparison
returns False because x is equal to
DEVOTION TO ENLIGHTENMENT
Membership operators
Membership operators are used to test if a sequence is
presented in an object
x = ["apple", "banana"]
print("banana" in x)
# returns True because a sequence with the value
"banana" is in the list
print("pineapple" not in x)
Bitwise operators
OR
print(6 | 3)
7
The | operator compares each bit and set it to 1 if one or both is 1,
otherwise it is set to 0:
6 = 0000000000000110
3 = 0000000000000011
--------------------
7 = 0000000000000111
====================
DEVOTION TO ENLIGHTENMENT
XOR
print(6 ^ 3)
5
The ^ operator compares each bit and set it to 1 if only one is 1, otherwise
(if both are 1 or both are 0) it is set to 0:
6 = 0000000000000110
3 = 0000000000000011
--------------------
5 = 0000000000000101
====================
DEVOTION TO ENLIGHTENMENT
NOT
print(~3)
The ~ operator inverts each bit (0 becomes 1 and 1 becomes 0).
Inverted 3 becomes -4:
3 = 0000000000000011
-4 = 1111111111111100
DEVOTION TO ENLIGHTENMENT
SELECTION ITERATION
DEVOTION TO ENLIGHTENMENT
: Colon Must
if first condition:
first body
elif second condition:
second body
elif third condition:
third body
else:
fourth body
DEVOTION TO ENLIGHTENMENT
DEVOTION TO ENLIGHTENMENT
EXAMPLES – if STATEMENT
else is missing,
it is an optional
statement
OUT PUT
DEVOTION TO ENLIGHTENMENT
: Colon Must
else is
used
OUT PUT
DEVOTION TO ENLIGHTENMENT
EXAMPLE
EXAMPLE
Program
3. ITERATION OR LOOPING
while loop
for loop
DEVOTION TO ENLIGHTENMENT
while loop
while loop
FLOW CHART
while loop
DEVOTION TO ENLIGHTENMENT
OUTPUT
DEVOTION TO ENLIGHTENMENT
OUTPUT
DEVOTION TO ENLIGHTENMENT
Program
Program
for LOOP
for LOOP
OUTPUT
DEVOTION TO ENLIGHTENMENT
x = range(3, 6)
for n in range(3,6):
OR for n in x:
print(n)
print(n)
DEVOTION TO ENLIGHTENMENT
OUTPUT
DEVOTION TO ENLIGHTENMENT
OUTPUT
DEVOTION TO ENLIGHTENMENT
https://www.menti.com/almkaea6w7og
DEVOTION TO ENLIGHTENMENT
Thank You
Happy Learning