#LifeKoKaroLift
Navigating Python
Interviews with
Confidence
1
Module Name:
Python Fundamentals
Edit Master text styles
Day : 01
Instructor : Er. Waseem
2
Today’s Agenda
● Data Types
● Variables
● Operators
● Conditionals
Navigating Python Interviews with Confidence 2
Poll 1
Edit Master text styles
Can you guess the output of the following?
string1 = "Listen"
string2 = "Silent"
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()
print(sorted(str1) == sorted(str2))
• Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 Footer 3 4
Introduction: What is Python?
● Python is a general-purpose, high-level,
interpreted, dynamically typed, and case-
sensitive programming language.
● Python was designed and developed by
Guido Van Rossum and released in the year
of 1991.
● Python distinguishes itself from other
programming languages in its easy to write
and understand syntax.
Python Installtion
● Download the latest version of Python from
https://www.python.org/downloads/
Variables, Values ,Data Types &
Comment
● A value is one of the basic things a program
works with, like a letter or a number. E.g. 1,
12.3, “Hello”
● These values belong to different types: 1 is
an integer, and “Hello” is a string, so called
because it contains a “string” of letters.
● Types or Data Types refers to the different
kinds of value we can store in a variable
Variables, Values & Data Types
(contd.)
● We have 6 basic Data Types in Python:
● Integer – all integer type values
● Float – all decimal type values
● String – any no. of characters within quotes
● Boolean – True or False
● None – special type; represents absence of
value
● Complex - type used to represent complex
numbers in Python
Variables, Values & Data Types
(contd.)
● A variable is like a container used to store
values. It allows us to label data with
descriptive names, so our programs can
understand and manipulate it.
● For example, we can create a variable named
age to store your age or a variable named pi
to store the value of pi (3.14159) .
● In Python, we can create variables by
assigning them a value using the equals ( = )
sign.
Variables, Values & Data Types
(contd.)
● E.g. age = 20
● name = “ Vicky”
● weight = 50.22
● If we want to check the type of the value or a
variable, we can use the built-in type()
function.
E.g. type( age) will give output as type <str>
which means it’s a string.
Variables, Values & Data Types
(contd.)
● Comments are lines of text/code in the
program, which are ignored by the compiler
during program execution.
● # begins the mark for a comment till the end
of line
Variable Naming Rules
● Variable names can include alphabets (a-z, A-
Z), digits (0-9), and underscores ( _ ).
● Variable names can’t begin with a digit !
● Python is case sensitive, so myVariable,
myvariable and MYVARIABLE are 3 different
variable names.
● The convention is to use small case letters
for variable names
Variable Naming Rules
● Python has some reserved words called
keywords which can’t be used as variable
names because they have special meanings
in python.
● You can get all keywords in python by the
following piece of code:
import keyword
print (keyword.kwlist)
print() function in Python
● The print() function prints some specified
message to the screen or some standard
output device. We can print strings, numbers,
or any other object using this function.
● print ( ‘Hello UPGRAD’)
print (233 + 6)
print (‘Sachin ‘ + ‘Tendulkar’ )
● Check the complete function signature of the
print( ) function in the next slide
print() function signature
● print(*objects, sep=' ', end='\n', file=sys.stdout,
flush=False)
● objects: The values to be printed. You can pass multiple values separated by
commas. These values will be converted to strings and printed.
● sep: The separator between the objects. It is a space (' ') by default.
● end: The string that is printed at the end. It is a newline character ('\n') by default.
● file: The output stream where the values are printed. It is set to sys.stdout by
default, which corresponds to the console.
● flush: A boolean indicating whether to forcibly flush the stream. It is set to False
by default, meaning the stream is not forcibly flushed.
print() function in Python (contd..)
● Example Usage:
print("Hello", "World", sep='-', end='!',
file=sys.stdout,
flush=True)
● Output:
Hello-World!
input() function in Python
● The input() function in Python is used to take
any form of inputs from the user/standard
input device, which can later be processed
accordingly in the program.
● It is complementary to the print() function.
>>> print('Enter your name.’)
>>> myName = input()
>>> print('Hello, ', myName)
input() function signature
● input([prompt])
● prompt (optional): A string that is printed to the standard output (usually the
console) before waiting for the user input. If the prompt is not provided, the
function simply waits for the user to enter input.
● Example:
user_input = input("Enter something: ")
print("You entered:", user_input)
Operators in Python
● Arithmetic Operators
● Relational/Comparison Operators
● Logical Operators
● Assignment Operators
● Membership Operators
● Bitwise Operators
● Identity Operators
Arithmetic Operators
● + (Addition)
● - (Subtraction)
● * (Multiplication)
● / (Division)
● % (Modulus - returns the remainder)
● ** (Exponentiation)
● // (Floor Division - returns the quotient
rounded down to the nearest integer)
● Example:
a = 10
b=3
addition = a + b # 13
division = a / b # 3.333...
modulus = a % b # 1
Relational Operators
● == (Equal to)
● != (Not equal to)
● < (Less than)
● > (Greater than)
● <= (Less than or equal to)
● >= (Greater than or equal to)
● Example:
x=5
y = 10
is_equal = x == y # False
is_greater = x > y # False
Logical Operators
● and (Logical AND)
● or (Logical OR)
● not (Logical NOT)
● Example:
a = True
b = False
logical_and = a and b # False
logical_or = a or b # True
logical_not = not a # False
Assignment Operators
● = (Assignment)
● += (Add and assign)
● -= (Subtract and assign)
● *= (Multiply and assign)
● /= (Divide and assign)
● %= (Modulus and assign)
● **= (Exponentiate and assign)
● //= (Floor divide and assign)
● Example:
x=5
x += 3 # x is now 8
Membership Operators
● in (True if a value is found in the sequence)
● not in (True if a value is not found in the
sequence)
● Example:
myList = [1, 2, 3]
is_present = 2 in myList # True
Bitwise Operators
● & (Bitwise AND)
● | (Bitwise OR)
● ^ (Bitwise XOR)
● ~ (Bitwise NOT)
● << (Left shift)
● >> (Right shift)
● Example
x = 5 # Binary: 0b101
y = 3 # Binary: 0b011
bitwise_and = x & y # 1
Binary: 0b001
Some Tricks ✨✨
● Multiple Assignment:
Python allows multiple assignments in a single line, which can be a concise way
to assign values to multiple variables.
a, b, c = 1, 2, 3
● Swapping Values:
We can swap the values of two variables without using a temporary variable.
a, b = b, a
● Underscore in Numeric Literals:
We can use underscores in numeric literals for better readability.
million = 1_000_000
Some Tricks ✨✨
● Chaining comparison operators:
Chaining comparison operators for multiple comparisons in a single line.
x = 5
print(1 < x < 10) # True
● Use the divmod() function to get both the quotient and remainder of a division
in a single operation.
quotient, remainder = divmod(10, 3) # quotient = 3,
remainder = 1
Conditionals/Conditional Statements
● A boolean expression is an expression
that evaluates to either True or False.
● Relational expressions or logical
expressions produce Boolean values.
● Conditional statements give us the
ability to check conditions and change the
behavior of the program accordingly.
Conditionals/Conditional Statements
Alternative Execution
● A second form of the if statement is alternative execution, in which there are two
possibilities, and the condition determines which one gets executed. The syntax
looks like this:
if(expression):
statements
. . . . .
else:
statements
. . . . .
Sequence Types
● List - An ordered and mutable collection of
items.
● Tuple - An ordered and immutable
collection of items.
● String - A sequence of characters.
● Range - Represents an immutable
sequence of numbers.
Key Takeaways
● Data Types
● Variables
● Operators
● Conditionals
Navigating Python Interviews with Confidence 2
#LifeKoKaroLift
Thank You!
33