Class 02
Class 02
Programming is one of the most fundamental skills you will need as a data
scientist/analyst. Among the numerous tools available for analyzing data, we choose
Python as it is the most robust, has an intuitive syntax, and is a modern and widely
used language.
Here you will learn how to think and do coding for real life scenarios. This portion
will be mostly based on building a strong sense of logic using only the basics.
Below are the fundamental data types in Python. You can create variables of these
types and perform operations accordingly.
1. Integer (int): Represents whole numbers without any decimal points.
x=5
y = -10
2. Float (float): Represents numbers with decimal points.
pi = 3.14
gravity = 9.81
3. String (str): Represents a sequence of characters enclosed within single or
double quotes.
name = "John Doe"
message = 'Hello, World!'
4. Boolean (bool): Represents the truth values True or False.
is_sunny = True
is_raining = False
5. Sequence Types:
1. list: Ordered collection of items, mutable.
Tuple: Tuples are ordered collections of items, which can be of different data types.
Tuples are immutable, meaning their elements cannot be changed once defined.
Syntax: They are defined using parentheses ().
# Defining a tuple
point = (10, 20)
dimensions = (100, 200, 300)
# Set operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2)) # Output: {1, 2, 3, 4, 5}
print(set1.intersection(set2)) # Output: {3}
# Dictionary operations
print('a' in my_dict) # Output: True
print('z' in my_dict) # Output: False
print(my_dict.keys()) # Output: dict_keys(['a', 'b', 'd'])
print(my_dict.values()) # Output: dict_values([1, 5, 4])
Comparison Operators :
Comparison operators in Python are used to compare values. They return either
True or False depending on whether the comparison is true or false. Here are the
comparison operators in Python along with examples:
x = 10
y=5
x=5
y = 10
x = 10
y = 10
x=5
y = 10
print(x <= y) # Output: True
These comparison operators can be used to compare numbers, strings, or any other
comparable objects in Python, and they return a boolean value (True or False) based
on the comparison result.
Conditional Operators:
Python, logical operators are used to combine conditional statements. Here are the
three logical operators in Python along with examples:
1. AND (and): Returns True if both conditions on its left and right are True.
x=5
y = 10
print(message)
In this example:
name and age are variables containing string and integer values respectively.
The f-string f"My name is {name} and I am {age} years old." allows embedding of
variables directly within the string using curly braces {}.
When this f-string is evaluated, the expressions within curly braces are replaced
with the values of the corresponding variables.
The resulting string is then stored in the variable message.
Finally, the message variable is printed, resulting in the formatted string being
displayed.
Swapping
Description
You are given two integer variables, x and y. You have to swap the values stored in
x and y.
----------------------------------------------------------------------------------------------------------------
Code:
x=5
y = 10
print("Before swapping:")
print("x =", x)
print("y =", y)
# Swapping values
temp = x
x=y
y = temp
print("\nAfter swapping:")
print("x =", x)
print("y =", y)
Can you swap two variables in python without using a third variable?
#x,y=y,x
a=5
b = 10
print("Before swap: a =", a, ", b =", b)
# Swapping
a, b = b, a
# After swap
print("After swap: a =", a, ", b =", b)
Even or Odd
Description
Given an integer, print whether it is Even or Odd.
-----------------------------------------------------------------------------------------------------------------
Code
#Take input on your own
num=int(input())
Description
You're trying to automate your alarm clock by writing a function for it. You're given a
day of the week encoded as 1=Mon, 2=Tue, ... 6=Sat, 7=Sun, and whether you are on
vacation as a boolean value Based on the day and whether you're on vacation, write a
function that returns a time in form of a string indicating when the alarm clock should
ring.
When not on a vacation, on weekdays, the alarm should ring at "7:00" and on the
weekends (Saturday and Sunday) it should ring at "10:00".
------------------------------------------------------------------------------------------------------------------
Factorial
Factorial is a mathematical function denoted by '!'. It is defined as
In this question, you have to make a function that will take an integer as input, and
return the factorial of that integer if that integer is greater than or equal to zero and
return -1 if the number is less than zero or negative.
Note: the function doesn't return print the factorial but returns it.
def factorial(n):
if n < 0:
return -1 # Return -1 for negative numbers
elif n == 0:
return 1 # Factorial of 0 is 1
else:
result = 1
for i in range(1, n + 1):
result *= i (this is nothing but augmented function and can also be written
as result = result*i)
return result
# Example usage:
print(factorial(5)) # Output: 120 (5 factorial)
print(factorial(0)) # Output: 1 (Factorial of 0)
print(factorial(-3)) # Output: -1 (Negative input)
print(r)
while w//3!=0:
choc = choc +w//3
w = w//3 + w%3
#dont forget to print the number of chocolates Sanjay can eat
print(choc)
While surfing on Facebook, whenever you visit a profile, you must have
noticed that it shows a small tab called mutual friends. This shows the
number of mutual friends and also the names of all the mutual friends.
How would you implement this functionality?
You have to implement the ATM machine. The basic function of an ATM
machine is, you insert your card and pin, if the pin matches, it lets you
withdraw money from your account and deducts the same from your
account. Don't forget to handle cases like insufficient balance and no cash
available. The more realistic ATM model you build the better.
Start -->Insert ATM card --> Select language of user choice -->Enter 4 digit ATM
PIN and press enter -->If PIN does not match for the given card , show “Error
Message” and asks to re-enter correct PIN else move to next step --> Select the
transaction type to withdraw money --> If there is no money in the ATM machine
show error as “Unable to withdraw money” else move to next step -->Enter the
amount manually in the given box or choose the amount as per given options on
the screen --> For the amount entered if user does not have enough balance in
savings , show error as “Insufficient Balance” else move to the next step--> Select
options “Yes” or “No” for the transaction receipt --> Take the cash and the receipt
when the machine gives it -->When the machine beeps take back your ATM card--
> End
Start --> Define a function called “Calculator” that takes two arguments:
num1 and num2 --> Enter first number (num1) -->Enter the operation that
user wants to perform (+,-,*,/,%,//) --> Enter the second number (num2) --
>Perform the calculation. --> Print the result --> End