❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Operators, Conditionals and Inputs

26 July 2024 at 13:57

Operators

Operators are symbols that tell the computer to perform specific mathematical or logical operations.

1.Arithmetic Operators

These operators perform basic mathematical operations like addition, subtraction, multiplication, and division.

*Addition (+): Add two numbers.
eg:

>>>print(1+3)

*Subtraction (-): Subtracts one number from another.
eg:

>>>print(1-3)

Multiplication (): Multiplies two numbers.
eg:

>>>print(1*3)

*Division (/): Divides one number by another.
eg:

>>>print(1/3)

*Floor Division (//): Divides one number by another and rounds down to the nearest whole number.
eg:

>>>print(1//3)

*Modulus (%): Returns the remainder when one number is divided by another.
eg:

>>>print(1%3)

Exponentiation (*): Raises one number to the power of another.
eg:

>>>print(1**3)

2.Comparison Operators

These operators compare two values and return either True or False.

*Equal to (==): Checks if two values are equal.

>>>a = 5
>>>b = 3
>>>result = (a == b)  

>>>result is False

*Not equal to (!=): Checks if two values are not equal.

>>>a = 5
>>>b = 3
>>>result = (a != b)  

>>>result is True

*Greater than (>): Checks if one value is greater than another.

>>>a = 5
>>>b = 3
>>>result = (a > b)  

>>>result is True

*Less than (<): Checks if one value is less than another.

>>>a = 5
>>>b = 3
>>>result = (a < b)  

>>>result is False

*Greater than or equal to (>=): Checks if one value is greater than or equal to another.

>>>a = 5
>>>b = 3
>>>result = (a >= b)  

>>>result is True

*Less than or equal to (<=): Checks if one value is less than or equal to another

>>>a = 5
>>>b = 3
>>>result = (a <= b)  
>>>result is False

3.Logical Operators

These operators are used to combine conditional statements.

*and: Returns True if both statements are true.

>>>a = 5
>>>b = 3
>>>result = (a > b and a > 0)  

>>>result is True

*or: Returns True if one of the statements is true.

>>>a = 5
>>>b = 3
>>>result = (a > b or a < 0)  
>>>result is True

*not: Reverses the result, returns False if the result is true.

>>>a = 5
>>>result = not (a > 0)  

>>>result is False

Conditionals

Conditionals are like traffic signals for your code. They help your program decide which path to take based on certain conditions.

1. The if Statement

The if statement checks a condition and executes the code block if the condition is True.
eg:

>>>a = 5
>>>b = 3
>>>if a > b:
    print("a is greater than b")

2. The elif Statement

The elif statement is short for β€œelse if”. It checks another condition if the previous if condition was False.
eg:

>>>a = 5
>>>b = 5
>>>if a > b:
    print("a is greater than b")
>>>elif a == b:
    print("a is equal to b")

3. The else Statement

The else statement catches anything that isn’t caught by the preceding conditions.
eg:

>>>a = 3
>>>b = 5
>>>if a > b:
    print("a is greater than b")
>>>elif a == b:
    print("a is equal to b")
>>>else:
    print("a is less than b")

TASK 3 – Operators, Conditionals, Input()

15 July 2024 at 03:10
  1. Create a program that takes two numbers and an operator (+, -, *, /) as input and performs the corresponding arithmetic operation.
  2. Write a program that takes an integer as input and checks if it is even or odd.
  3. Create a program that asks the user to enter a number and then prints whether the number is positive, negative, or zero.
  4. Write a program that takes a numerical grade as input and prints the corresponding letter grade (A, B, C, D, or F). Total Marks is 100. You can set your own range.
  5. Create a program that takes three numbers as input and prints the largest of the three.
  6. Write a program that takes a year as input and checks if it is a leap year.
  7. Create a program that takes a person’s age as input and checks if they are eligible to vote (age 18 or older).
  8. Write a program that takes a number as input and checks if it is divisible by 5 and 11.
  9. Create a program that calculates simple interest. Take the principal amount, rate of interest, and time period as input.
  10. Write a program that asks the user to enter a password. If the password is correct, print β€œAccess granted”; otherwise, print β€œAccess denied”.

Python – Operators CondItionals and Input()

15 July 2024 at 03:03

In this blog, we’ll cover three fundamental concepts: operators, conditionals, and getting user input with the input() function. By the end, you’ll have a good grasp of how to use them in your Python programs.

What Are Operators?

Operators are symbols that tell the computer to perform specific mathematical or logical operations.

Think of them as the verbs in a sentence that tell you what action to take. There are several types of operators in Python:

Arithmetic Operators

These operators perform basic mathematical operations like addition, subtraction, multiplication, and division.

  • Addition (+): Add two numbers.
  • Subtraction (-): Subtracts one number from another.
  • Multiplication (*): Multiplies two numbers.
  • Division (/): Divides one number by another.
  • Floor Division (//): Divides one number by another and rounds down to the nearest whole number.
  • Modulus (%): Returns the remainder when one number is divided by another.
  • Exponentiation ():** Raises one number to the power of another.

Comparison Operators

These operators compare two values and return either True or False.

  • Equal to (==): Checks if two values are equal.
a = 5
b = 3
result = (a == b)  # result is False
  • Not equal to (!=): Checks if two values are not equal.
a = 5
b = 3
result = (a != b)  # result is True
  • Greater than (>): Checks if one value is greater than another.
a = 5
b = 3
result = (a > b)  # result is True
  • Less than (<): Checks if one value is less than another.
a = 5
b = 3
result = (a < b)  # result is False

  • Greater than or equal to (>=): Checks if one value is greater than or equal to another.
a = 5
b = 3
result = (a >= b)  # result is True

  • Less than or equal to (<=): Checks if one value is less than or equal to another.
a = 5
b = 3
result = (a <= b)  # result is False

Logical Operators

These operators are used to combine conditional statements.

  • and: Returns True if both statements are true.
a = 5
b = 3
result = (a > b and a > 0)  # result is True

  • or: Returns True if one of the statements is true.
a = 5
b = 3
result = (a > b or a < 0)  # result is True

  • not: Reverses the result, returns False if the result is true.
a = 5
result = not (a > 0)  # result is False

What Are Conditionals?

Conditionals are like traffic signals for your code. They help your program decide which path to take based on certain conditions. The most common conditional statements in Python are if, elif, and else.

1. The if Statement

The if statement checks a condition and executes the code block if the condition is True.

a = 5
b = 3
if a > b:
    print("a is greater than b")

2. The elif Statement

The elif statement is short for β€œelse if”. It checks another condition if the previous if condition was False.

a = 5
b = 5
if a > b:
    print("a is greater than b")
elif a == b:
    print("a is equal to b")

3. The else Statement

The else statement catches anything that isn’t caught by the preceding conditions.

a = 3
b = 5
if a > b:
    print("a is greater than b")
elif a == b:
    print("a is equal to b")
else:
    print("a is less than b")

Using input() to Get User Input

This function allows you to get input from the user. It’s like asking a question and waiting for the user to answer.

The input you get is always a string (text), so if you need a number, you have to convert it.

Basic Usage of input()

Here’s a simple example:

name = input("What is your name? ")
print("Hello, " + name + "!")

In this example, the program asks the user for their name and then prints the same.

Converting Input to Numbers

If you want to work with numbers, you’ll need to convert the input from a string to an integer or a float.

age = input("How old are you? ")
age = int(age)
print("You are " + str(age) + " years old.")

Putting It All Together

Let’s combine operators, conditionals, and input() in a simple example. Suppose you want to create a program that checks if a number entered by the user is positive, negative, or zero.

number = input("Enter a number: ")
number = float(number)  # Convert the input to a float

if number > 0:
    print("The number is positive.")
elif number < 0:
    print("The number is negative.")
else:
    print("The number is zero.")

In this example, the program:

  1. Asks the user to enter a number.
  2. Converts the input to a float (to handle decimal numbers).
  3. Uses conditionals to check if the number is positive, negative, or zero, and prints the result.

Quiz

https://docs.google.com/forms/d/e/1FAIpQLSdwRG3dEZBsjsFWy6nLzQqu5ksjjdtvy7OUXQg8Z0u3fvMXfw/viewform?usp=sf_link

Tasks:

Notebook:

https://colab.research.google.com/drive/1pt0t51u7EM5uAHf_3uNUnbIqogd5Cv47?usp=sharing

Infographics:

❌
❌