0% found this document useful (0 votes)
5 views6 pages

Day-3 Notes On Data Operations

The document provides an overview of basic Python concepts including the `input()` function, arithmetic and comparison operators, and Python keywords. It explains how to use these elements through examples and practice scenarios such as calculating a shopping bill, gardening area, temperature conversion, message repetition, and loan interest calculation. Each section is designed to illustrate practical applications of Python programming for beginners.

Uploaded by

dipak61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Day-3 Notes On Data Operations

The document provides an overview of basic Python concepts including the `input()` function, arithmetic and comparison operators, and Python keywords. It explains how to use these elements through examples and practice scenarios such as calculating a shopping bill, gardening area, temperature conversion, message repetition, and loan interest calculation. Each section is designed to illustrate practical applications of Python programming for beginners.

Uploaded by

dipak61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DAY-3 NOTES

Prof. Sagar Chouksey


Python Trainer, CODING WISE {Awarded as BEST IT EDUCATION INSTITUTE}

➔Input function-
The `input()` function in Python is like having a conversation where you ask someone a
question and wait for their response. Let's use a real-life example to illustrate why we need
this function:

Imagine you're a teacher creating a quiz program for your students. You need a way to ask
each student their name and their answers to the questions. In a real classroom, you would
simply ask them, and they would respond. In a computer program, you need a similar
mechanism to ask a question and receive an answer. This is where the `input()` function
comes in.

The `input()` function in Python allows your program to pause and wait for the user to type
something. Once the user types their response and presses Enter, the function captures that
response as a string (text), which you can then use in your program.

Here's a simple example of how it works in Python:

Ask for the user's name


name = input("What is your name? ")

Greet the user with their name


print("Hello, " + name + "!")

Ask a simple math question


answer = input("What is 2 + 2? ")

In this code:

1. `input("What is your name? ")`: The program displays the question "What is your name?"
and waits for the user to type something. Whatever they type is stored in the variable
`name`.

2. `print("Hello, " + name + "!")`: The program greets the user with the name they entered.

3. `input("What is 2 + 2? ")`: The program then asks a math question and waits for the user's
response, storing it in the variable `answer`.

This interaction is similar to how you would conduct a quiz in a classroom, but it's done
through a computer program using the `input()` function.
Operators in python

Operators in programming are like tools that perform specific actions on data. They are
symbols that tell the computer to perform certain mathematical, relational, or logical
operations and produce a final result. Let's break them down into two main types you
mentioned: arithmetic and comparison operators.

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations. In Python, the
basic arithmetic operators are:

1. Addition (`+`): Adds two values. For example, `5 + 3` results in `8`.


2. Subtraction (`-`): Subtracts the right-hand operand from the left-hand operand. For
instance, `5 - 3` results in `2`.
3. Multiplication (`*`): Multiplies two values. So, `5 * 3` gives `15`.
4. Division (`/`): Divides the left-hand operand by the right-hand operand. `10 / 2` results in
`5`. This division always returns a floating-point number.

5. Modulus (`%`): Divides and returns the remainder. For example, `7 % 2` is `1` because
`7` divided by `2` leaves a remainder of `1`.
6. Exponentiation (``): Raises the first number to the power of the second. `2 3` equals `8`,
as it's `2` raised to the power of `3`.
7. Floor Division (`//`): Divides and returns the largest whole number that is not greater than
the result. For example, `7 // 2` is `3`.

Comparison Operators

Comparison operators are used to compare values. They evaluate to Boolean values (`True`
or `False`). Here are the common comparison operators in Python:

1. Equal to (`==`): Checks if the values of two operands are equal. If yes, the condition
becomes true. For example, `5 == 5` is `True`.
2. Not equal to (`!=`): Checks if the values of two operands are not equal. If yes, the
condition becomes true. For example, `5 != 3` is `True`.
3. Greater than (`>`): Checks if the value of the left operand is greater than the value of the
right operand. If yes, the condition becomes true. So, `5 > 3` is `True`.
4. Less than (`<`): Checks if the value of the left operand is less than the value of the right
operand. If yes, the condition becomes true. Thus, `3 < 5` is `True`.
5. Greater than or equal to (`>=`): Checks if the value of the left operand is greater than or
equal to the value of the right operand. If yes, the condition becomes true. For instance, `5
>= 5` is `True`.
6. Less than or equal to (`<=`): Checks if the value of the left operand is less than or equal
to the value of the right operand. If yes, the condition becomes true. For example, `3 <= 5` is
`True`.

In programming, these operators are fundamental in controlling the flow of the program,
performing calculations, and making decisions. For example, you might use arithmetic
operators to calculate the total price of items in a shopping cart, and comparison operators
to check if the total price is within a budget.

Keywords in python
Keywords in Python are reserved words that have a special meaning and purpose in the
language. Each keyword is a predefined word by Python, and they cannot be used as
identifiers, like variable names, function names, or any other identifiers. Think of them as the
core vocabulary of the Python language, each carrying out a specific function.

As of Python 3.9, there are 35 keywords:

1. `False`: Represents the boolean value False.


2. `None`: Represents the absence of a value or a null value.
3. `True`: Represents the boolean value True.
4. `and`: A logical operator used to combine conditional statements.
5. `as`: Used to create an alias while importing a module.
6. `assert`: Used for debugging purposes.
7. `async`: Used to define asynchronous functions.
8. `await`: Used to wait for the completion of an async function.
9. `break`: Used to break out of a loop.
10. `class`: Used to define a new class.
11. `continue`: Skips the rest of the code inside a loop for the current iteration.
12. `def`: Used to define a function.
13. `del`: Used to delete a reference to an object.
14. `elif`: Used in conditional statements, same as else if.
15. `else`: Used in conditional statements.
16. `except`: Used with exceptions, what to do when an exception occurs.
17. `finally`: Used with exceptions, a block of code that will be executed no matter if there is
an exception or not.
18. `for`: Used to create a for loop.
19. `from`: Used to import specific parts of a module.
20. `global`: Used to declare a global variable.
21. `if`: Used to make a conditional statement.
22. `import`: Used to import a module.
23. `in`: Used to check if a value is present in a sequence.
24. `is`: Used to test if two variables are the same object.
25. `lambda`: Used to create an anonymous function.
26. `nonlocal`: Used to declare a non-local variable.
27. `not`: A logical operator used to invert the truth value.
28. `or`: A logical operator used to combine conditional statements.
29. `pass`: A null statement, a statement that will do nothing.
30. `raise`: Used to raise an exception.
31. `return`: Used to exit a function and return a value.
32. `try`: Used to make a try-except block for exception handling.
33. `while`: Used to create a while loop.
34. `with`: Used to simplify exception handling.
35. `yield`: Used to end a function and returns a generator.

These keywords are the building blocks of Python code. They help define the structure and
control the flow of the Python program. Each keyword has rules about how it can be used,
which are defined by the syntax of the language. Remember, since they have special
meanings, you cannot use these keywords as names for variables or other identifiers in your
code.

Practice Questions
Scenario 1: Shopping Bill Total
Imagine you are at a grocery store, and you've picked up two items. Write a Python program
to calculate the total bill amount. The program should ask you for the price of each item and
then display the total cost.

# Prices of two items from user input


item1_price = float(input("Enter the price of the first item: "))
item2_price = float(input("Enter the price of the second item: "))

# Calculate total
total_price = item1_price + item2_price

# Display the total price


print("Total bill amount: $", total_price)

Scenario 2: Gardening Circle


You're planning a circular garden and need to calculate how much area it will cover. Create a
Python program that asks for the radius of the circle (the distance from the center to the
edge of the circle) and calculates the garden's area. Remember, the formula for the area of a
circle is `3.14 * radius * radius`.

# Radius of the circle from user input


radius = float(input("Enter the radius of the garden circle: "))

# Calculate the area


area = 3.14 * radius * radius
# Display the area
print("The area of the garden is:", area, "square units")

Scenario 3: Vacation Temperature


You're planning a vacation to Europe and want to convert the weather forecast from
Fahrenheit to Celsius. Write a Python program that asks for the temperature in Fahrenheit
and converts it to Celsius using the formula `(Fahrenheit - 32) * 5/9`. Display the temperature
in Celsius so you can pack accordingly.

# Temperature in Fahrenheit from user input


fahrenheit = float(input("Enter the temperature in Fahrenheit: "))

# Convert to Celsius
celsius = (fahrenheit - 32) * 5 / 9

# Display the temperature in Celsius


print("Temperature in Celsius is:", celsius)

Scenario 4: Text Messaging


You want to send a repeated message to a friend as part of a game. Write a program that
asks for a short message and a number `n`. The program should then print that message `n`
times in a row. For example, if you input "LOL" and the number 4, the program should output
"LOLLOLLOLLOL".

# Message and number of repetitions from user input


message = input("Enter your message: ")
n = int(input("How many times do you want to repeat the message? "))

# Print the message n times


print(message * n)

Scenario 5: Loan Interest


You're considering taking out a loan and want to calculate how much interest you'll have to
pay. Create a program that asks for the principal amount (the initial amount of the loan), the
interest rate per year (as a percentage), and the duration of the loan in years. The program
should calculate the total interest you'll pay using the formula `Interest = (Principal * Rate *
Time) / 100`. Display the total interest to help you make your decision.

# Principal, interest rate, and time from user input


principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the interest rate (as a percentage): "))
time = float(input("Enter the time in years: "))

# Calculate the interest


interest = (principal * rate * time) / 100

# Display the total interest


print("Total interest to be paid: $", interest)

You might also like