Unit 1 Basics of Python
Unit 1 Basics of Python
Basics of
Python Programming
X 2 10 2
X
label
10
Memory Memory
Python versions
❖ Python 0.9 was published in 1991
❖ Python 1.0 was released on January, 1994
❖ Python 2.0 was released on October, 2000
❖ Python version 2.x means 2.0 to 2.7
❖ Python 3.0 was released on December, 2008
❖ Python3 version 3.x means 3.0 to 3.12.1
❖ We are going to use python 3.12.1
Python Data types
Data types
Scalar Non-scalar
List
Bool Numeric None
Tuple
String
int float Complex Octal Hex Binary
Dictionary
Set
Frozen set
Data types
A) Numeric types
Integers
•Examples: 0, 1, 1234, -56
Complex numbers
•Examples: 3+4j, 3.0+4.0j, 2J
•Must end in j or J
Contd…
• Binary constants
• Examples: 0b1011, -0b101
• Must start with a leading ‘0b’ or ‘0B’
• Octal constants
• Examples: 0o177, -0o1234
• Must start with a leading ‘0o’ or ‘0O’
• Hex constants
• Examples: 0x9ff, -0X7AE
• Must start with a leading ‘0x’ or ‘0X’
Contd…
B) Bool:
•It is used to represent Boolean values: True OR False
•Word ‘TRUE’ or ‘true’ is not valid. Use- True
•Python is case sensitive language
C) None:
•Its value is ‘None’. It shows ‘empty’ or ‘no value’
•It has some specific use like when a python function returns
• Membership operators:
• It includes operators: in, not in
• Ex: x in y
• It in results in True if x is a member of sequence y
Contd...
Identity operators:
•It includes operators: is, is not
•Ex: x is y
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisions, Identity, Membership operators
not Logical NOT
and Logical AND
or Logical OR
Cntd…
• Operators of same group have same precedence. When
operators of same group exist in an expression, then we need to
find out order of their evaluation means which operation will
be performed first.
• Associativity is the order in which an expression is evaluated
that has multiple operator of the same precedence. Almost all
the operators have left-to-right associativity.
• For example, multiplication and floor division have the same
precedence. Hence, if both of them are present in an
expression, left one is evaluated first.
• Exponent operator ** has right-to-left associativity in Python.
Non associative operators
• Some operators like assignment operators and
comparison operators do not have associativity in
Python. There are separate rules for sequences of this
kind of operator and cannot be expressed as
associativity.
• For example, x < y < z neither means (x < y) < z nor x
< (y < z). x < y < z is equivalent to x < y and y < z, and
is evaluates from left-to-right.
• Furthermore, while chaining of assignments like
x=y=z is perfectly valid, but x = y += z will result into
error.
• Solve this: 1) a= 4+++--2-8--+++7
2) a= ++9--+-6---3-+++5
Python syntax & Execution of program
• Ways to execute python statements/commands:
1) By writing directly in “python command prompt” or in
python IDLE:
– B) Run that ‘.py’ file using “Run” menu option (or F5 key) in IDLE.
• Note:- Before you run ‘.py’ file in command line, add ‘path’
of ‘python.exe’ using ‘Environment Variables’.
Indentation
• Indentation refers to the spaces at the beginning of a code
line.
• In other programming languages the indentation is for
better readability only, but in Python it is mandatory.
• Python uses indentation to indicate a block of code.
• A block means group of statements like if-else, for loop,
while loop, class.
• Example:
if 5 > 2:
print ("You are inside if statement.")
print("Five is greater than two!")
Comments
• Single line comment- use ‘#’
• Multiline comment- Python doesn’t have syntax for
multiline comment option, but one can use ‘multiline
string’ concept to write multiline comments.
• Python ignores string literals that are not assigned to a
variable, you can add a multiline string (triple quotes)
in your code, and place your comment inside it.
• Example:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Variables
• In python, variables are just label given to any data
value.
• A variable is created the moment you first assign a
value to it.
1) Simple if…else
Syntax:
if expression:
statement(s)
else:
statement(s)
Cntd…
2) if..elif…else ladder:
Syntax:
if expression1:
statement(s)
elif expression2:
statement(s)
else:
statement(s)
3) Nested if..else:
if...elif...else statement inside another if...elif...else
statement is called nesting.
Loops
• Loop consists of three important parts:
the initialisation, the condition, and the update.
1) while loop:
Syntax:
while expression:
statement(s)
2) for loop:
Syntax:
for iterator in sequence:
statements(s)
Cntd…
Examples:
1) count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
2) for i in range(5):
print (i)
3) for i in range(1,6,2):
print (i)
4) for i in ‘computer’:
print (i)
Cntd…
• break, continue, pass statements & else clause:
• break: Terminates the loop statement and transfers
execution to the statement immediately following the
loop.
• continue: Causes the loop to skip the remainder of its
body and continue with next iteration.
• pass: The pass statement in Python is used when a
statement is required syntactically but you do not want
any command or code to execute.
Cntd…
Examples:
1) for i in range(6):
if i==3:
break
print(i)
2) for i in range(6):
if i==3:
continue
print(i)
3) while True:
pass
4) class MyEmptyClass:
pass
Cntd…
• Else: else clause runs when no break occurs.
Example:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n//x)
break
else:
print(n, 'is a prime number')