Unit-1 Notes
Unit-1 Notes
Unit-1 Notes
Unit -1
Introduction
• Portability.
• Object-oriented
– Everything in Python is an object.
– https://www.spyder-ide.org/
• IDLE
– https://docs.python.org/3/library/idle.html
• Sublime Text 3
– https://www.sublimetext.com/3
• Jupyter
– https://jupyter.org/install.html
Interacting with Python Programs (CO1)
6. Click on the code cell write down the code 1+1 and then press
shift + Enter from keyboard/run command from interface. Then
check the output.
Elements of Python (CO1)
The students will study the elements of Python like Keywords and
Identifiers, Variables, Data types and Operators.
Prerequisite and Recap
• Expression
• Operators
• Constant and variable
Elements of Python (CO1)
• They store numeric values. Number objects are created when a value is
assigned to them. For ex:
>>> var1 = 1
>>> var2 = 10
• They can be deleted the reference to a number object by using the del
statement. The syntax of the del statement is
del var1[,var2[,var3[....,varN]]]]
• A single object or multiple objects can be deleted by using the del statement.
For example
>>> del var
>>> del var_a, var_b
Types of Number Types (CO1)
• For example
>>> str = ‘Hello World!’
>>> print(str) # Prints complete string
>>> print(str[0]) # Prints first character of the string
>>> print(str[2:5]) # Prints characters starting from 3rd to 5th
>>> print(str[2:]) # Prints string starting from 3rd character
>>> print(str * 2) # Prints string two times
>>> print(str + ‘TEST’) # Prints concatenated string
Slicing Strings (CO1)
Slicing
• A range of characters can be returned by using the slice syntax.
• Specify the start index and the end index, separated by a colon, to
return a part of the string.
Example
Get the characters from position 2 to position 5 (not included):
Program
b = "Hello, World!"
print(b[2:5])
Program Output
llo
Program to demonstrate slicing of Strings (CO1)
# String slicing
String ='ASTRING'
# Using slice constructor
s1 = slice(3)
s2 = slice(1, 5, 2)
s3 = slice(-1, -12, -2)
print("String slicing")
print(String[s1])
print(String[s2])
print(String[s3])
Output of slicing of Strings (CO1)
Output:
String slicing
AST
SR
GITA
Lists (CO1)
• The plus (+) sign is the list concatenation operator, and the asterisk
(*) is the repetition operator.
Lists (CO1)
Operator Description
= a=b
Assigns values from right side operands(b) to left side operand (a)
+= a+=b is same as a = a + b
It adds right operand to the left operand and assign the result to left operand
-= a-=b is same as a = a - b
It subtracts right operand from the left operand and assign the result to left operand
*= a*=b is same as a = a * b
It multiplies right operand with the left operand and assign the result to left operand
/= a/=b is same as a = a / b
It divides left operand with the right operand and assign the result to left operand
Assignment Operators (CO1)
Operator Description
%= a%=b is same as a = a % b
It takes modulus using two operands and assign the result to left operand
a**=b is same as a = a ** b
**= Performs exponential (power) calculation on operators and assign value to the
left operand
//= a//=b is same as a = a //b
It performs floor division on operators and assign value to the left operand
Logical Operators (CO1)
Operator Description
a b a and b a or b not a
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
Bitwise Operators (CO1)
is not True if the operands are not identical (do not a is not b False
refer to the same object)
Operator Precedence and associativity(CO1)
Operator Description
<=,>=,<,> Relational inequality operators
==, != Equal and not equal operators
=,*=,/=,//=,%=,+=,-=,**=,&= Assignment operators
is, is not Identity operators
in, not in Membership operators
not Logical not operator
and Logical and operator
or Logical or operator
Expressions in Python (CO1)
class car:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)
c1 = car("Toyota", 2016)
c1.display()
Output of example (CO1)
Toyota 2016
Python Constructor(CO1)
ID: 101
Name: John
ID: 102
Name: David
Python Non-Parameterized Constructor (CO1)
The non-parameterized constructor uses when we do not want to
manipulate the value or the constructor that has only self as an
argument.
Example:
class Student:
# Constructor - non parameterized
def __init__(self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("John")
Output of Non-Parameterized Constructor (CO1)
2. _________Operator
* is used to multiply numbers.
MCQs