Unit 2 - Python Programming
Unit 2 - Python Programming
1. Decision Making
➔ What is Decision Making in Python?
Decision making in Python refers to the ability of a program to make choices and execute
different blocks of code based on certain conditions.
➡️
In simple terms:
It allows your program to "decide what to do" when a condition is True or False.
1.1 if Statements
● An if statement in Python is used to execute a block of code only when a specific
condition is True.
● If the condition is False, the block is skipped and nothing happens.
1.2 if-else Statement
● Allows you to execute one block of code if the condition is True, and another block else
part if the condition is False.
4.1.1. math.sqrt(x)
● Returns the square root of x.
4.1.2. math.pow(x, y)
● Returns x raised to the power y (i.e., x^y).
4.1.3. math.sqrt(x)
● Rounds a decimal number down to the nearest whole number.
4.1.4. math.ceil(x)
● Rounds a decimal number up to the nearest whole number.
4.2.1. random.randint(a, b)
● Returns a random integer between a and b (inclusive).
4.2.2. random.choice(sequence)
● Returns a random element from a list or sequence.
4.2.3. random.random()
● Returns a random float between 0.0 and 1.0.
4.1.4. random.shuffle(list)
● Shuffles the items in a list randomly (in-place).
5. Python Functions
➔ What is a Function?
◆ A function in Python is a block of code that does a specific job.
◆ Instead of writing the same code again and again, we write it once inside a
function and reuse it whenever needed by calling the function.
◆ Benefits:
● Increases code readability
● Promotes code reusability
5.1 Types of Functions
Below are the different types of functions in Python:
● Built-in library(Predefined) function: These are Standard functions in Python that are
available to use. Example:- Predefined (e.g., len(), print())
● User-defined function: We can create our own functions based on our requirements.
Functions defined by the user using def
` ` OR operator