Day-3 Notes On Data Operations
Day-3 Notes On Data Operations
➔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.
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:
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.
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.
# Calculate total
total_price = item1_price + item2_price
# Convert to Celsius
celsius = (fahrenheit - 32) * 5 / 9