Unit 2 Basics of Python
Unit 2 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.11
We are going to use python 3.11
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
nothing, return statement should be: return None
•
‘None’ is equal to ‘False’, when written inside ‘if’ condition.
•
Note: Datatype of a variable can be checked by type()
function. For example, c=10
type (c)
Type casting (Type conversion)
• int() - constructs an integer number from an integer literal, a float
literal (by rounding down to the previous whole number), or a string
literal (providing the string represents a whole number)
• float() - constructs a float number from an integer literal, a float literal
or a string literal (providing the string represents a float or an integer)
• str() - constructs a string from a wide variety of data types, including
strings, integer literals and float literals
• dec(), bin(), oct(), hex() functions are also used for type conversion.
• Example: x= int(2.8)
• y=float(“4”)
• z= str(3.1)
• m= int(“7”)
• n=float(1)
• p= int(“6.5”) # Value Error
Python operators
Basic arithmetic operators
Four arithmetic operations: a+b, a-b, a*b, a/b, a%b
Exponentiation: a**b and Floor division: a//b
Comparison (Relational) operators
Greater than, less than, etc.: a < b, a > b, a <= b, a >= b
Identity tests: a == b, a != b
Logical operators:
logical operators are: and, or , not
Assignment operators:
It includes operators like: =, +=, -=, *=, /=, %=, //=, **=,
&=, |=, ^=, >>=, <<=
Contd…
• Bitwise operators
Bitwise or: a | b, Bitwise and: a & b
Bitwise not: ~x
Bitwise exclusive or: a ^ b # Don’t get confuse this
with exponentiation
Shift a left or right by b bits: a << b, a >> b
• 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
•
It is results in True if id(x) equals id(y).
– 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')