SomeQuiz

Course

Start coding with Python

Co-Founder

Top Contributor

Nuwan Perera

@nuwanperera.me

Python is a versatile and beginner-friendly programming language that has gained immense popularity in recent years. Whether you're a coding novice or have some experience in other languages, Python is an excellent choice to start your coding journey. In this blog post, we'll cover the basics of Python programming, focusing on print statements and logical arithmetic. By the end of this lesson, you'll be equipped with fundamental skills to write your first Python program.

Note: Before starting this Lesson, make sure you have installed Python properly on your computer.

Print statement

Print statements are a fundamental tool in any programming language, allowing you to display output on the console. In Python, you can use the print() function to achieve this. Let's start with a simple example:

print("Hello, Welcome to SomeQuiz!")

In this example, the print() function outputs the string "Hello, Welcome to SomeQuiz"to the console. You can replace the text within the quotation marks with any message you want to display.

You can also print multiple items by separating them with commas:

name = "Sithija"
age = 20
 
print("My name is", name, "and I am", age, "years old.")

This will print: My name is Sithija and I am 20 years old.

Input statement

User interaction is a key aspect of many Python programs, allowing users to provide input and interact with the program's functionalities. The input() statement in Python is a powerful tool that enables you to receive input from users during program execution. In this article, we will explore the input() statement and its various applications, empowering you to create interactive programs that engage users and provide dynamic experiences.

1. Basic Usage

The input() statement allows you to prompt the user with a message or question and wait for their input. It then returns the user's input as a string. Here's an example:

name = input("Enter your name: ")
print("Hello, " + name + "!")

2. Handling User Input

The input provided by the user through the input() statement is treated as a string by default. If you need to work with numerical values, you can convert the input to the desired data type using functions like int() or float():

age = int(input("Enter your age: "))
next_year = age + 1
print("Next year, you will be", next_year, "years old.")

3. Prompting for Multiple Inputs

You can prompt the user for multiple inputs by using multiple input() statements or by separating the inputs with commas:

name = input("Enter your name: ")
age = int(input("Enter your age: "))
 
print("Hello,", name + "!")
print("You are", age, "years old.")

he input() statement in Python empowers you to create interactive programs by receiving user input during runtime. In this article, we explored the various aspects of the input() statement, from basic usage to handling user input, input validation, and enhancing the user experience. By leveraging the input() statement effectively, you can build dynamic and engaging programs that respond to user input, opening up endless possibilities for interactive applications. So go ahead, embrace user interaction, and create Python programs that truly connect with your users.

Logical Arithmetics

Python provides various operators performing arithmetic operations and logical comparisons. Here are some commonly used operators:

Python Operators

Arithmetic operators play a crucial role in Python programming, enabling you to perform mathematical calculations and manipulate numerical data. Understanding these operators is fundamental to writing efficient and accurate code. In this article, we will explore the various arithmetic operators in Python and delve into their functionalities and use cases. By the end, you'll have a solid grasp of how to leverage arithmetic operators to perform complex calculations and solve real-world problems.

OperatorSymbol
Addition+
Substraction-
Multiplication*
Division/
modulus (Remainder)%
Exponentiation**
Floor Divition//

arithmetic operators follow the concept of operator precedence and associativity. The order of operations is as follows:

  1. Parentheses () - Operations within parentheses are evaluated first.
  2. Exponentiation - Exponentiation is evaluated next.
  3. Multiplication, Division, and Floor Division - These operations are evaluated from left to right.
  4. Addition and Subtraction - These operations are evaluated from left to right.

When necessary, you can use parentheses to specify the order of evaluation explicitly.

Addition Operator (+)

The addition operator + is used to add two numbers together. It can also be used for string concatenation, merging two strings into one.

# Numeric addition
result = 5 + 3
print(result)  # Output: 8
 
# String concatenation
name = "John"
greeting = "Hello, " + name
print(greeting)  # Output: Hello, John

Subtraction Operator (-)

The subtraction operator - is used to subtract one number from another.

result = 10 - 5
print(result)  # Output: 5

Multiplication Operator (*)

The multiplication operator * is used to multiply two numbers together. It can also be used to repeat a string multiple times.

# Numeric multiplication
result = 4 * 3
print(result)  # Output: 12
 
# String repetition
text = "Python"
repeated_text = text * 3
print(repeated_text)  # Output: PythonPythonPython

Division Operator (/)

The division operator / is used to divide one number by another. In Python 3.x, it always returns a floating-point result, even if the division is between two integers.

result = 15 / 4
print(result)  # Output: 3.75

Modulo Operator (%)

The modulo operator % returns the remainder of a division operation. It is often used to check if a number is divisible by another number or to cycle through a range of values.

result = 15 % 4
print(result)  # Output: 3
 
# Checking divisibility
number = 24
if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Exponentiation Operator (**)

The exponentiation operator ** raises a number to the power of another number.

result = 2 ** 3
print(result)  # Output: 8

Floor Division Operator (//)

The floor division operator // performs division and rounds the result down to the nearest whole number. It returns the quotient without the remainder.

result = 15 // 4
print(result)  # Output: 3

Arithmetic operators are essential tools for performing mathematical calculations and manipulating numerical data in Python. In this article, we explored the various arithmetic operators available in Python, including addition, subtraction, multiplication, division, modulo, exponentiation, and floor division. By mastering these operators, you now have the foundational knowledge to build sophisticated algorithms and solve a wide range of mathematical problems.

Assignment Operators

Python offers a variety of assignment operators that go beyond the simple assignment (=) operator. These assignment operators provide concise and efficient ways to manipulate variables by combining assignment with arithmetic or bitwise operations. In this article, we will explore the world of Python assignment operators and delve into their functionalities and practical applications. By the end, you'll have a solid understanding of how to leverage assignment operators to streamline your code and enhance variable manipulation in Python.

OperatorexampleSame As
=x = 3x = 3
+=x += 4x = x + 4
-=x -= 4x = x - 4
*=x *= 2x = x * 2
/=x /= 2x = x / 2
%=x %= 2x = x % 2
**=x **= 2x = x ** 2
//=x %= 2x = x % 2

1. Basic assignment

The basic assignment operator = is used to assign a value to a variable.

x = 10

2. Addition Assignment

The addition assignment operator (+=) adds a value to a variable and assigns the result back to the variable. It is equivalent to x = x + y.

x = 5
x += 3  # equivalent to x = x + 3
print(x)  # Output: 8

Subtraction Assignment

The subtraction assignment operator (-=) subtracts a value from a variable and assigns the result back to the variable. It is equivalent to x = x - y.

x = 10
x -= 4  # equivalent to x = x - 4
print(x)  # Output: 6

Multiplication Assignment

The multiplication assignment operator (*=) multiplies a variable by a value and assigns the result back to the variable. It is equivalent to x = x * y.

x = 2
x *= 5  # equivalent to x = x * 5
print(x)  # Output: 10

Division Assignment

The division assignment operator (/=) divides a variable by a value and assigns the result back to the variable. It is equivalent to x = x / y.

x = 20
x /= 4  # equivalent to x = x / 4
print(x)  # Output: 5.0

Modulo Assignment

The modulo assignment operator (%=) calculates the remainder of dividing a variable by a value and assigns the result back to the variable. It is equivalent to x = x % y.

x = 17
x %= 5  # equivalent to x = x % 5
print(x)  # Output: 2

Exponentiation Assignment

The exponentiation assignment operator (**=) raises a variable to the power of a value and assigns the result back to the variable. It is equivalent to x = x ** y.

x = 3
x **= 4  # equivalent to x = x ** 4
print(x)  # Output: 81

Floor Division Assignment

The floor division assignment operator (//=) performs integer division between a variable and a value, rounding the result down to the nearest whole number, and assigns it back to the variable. It is equivalent to x = x // y.

x = 23
x //= 5  # equivalent to x = x // 5
print(x)  # Output: 4

Python's assignment operators provide powerful and concise ways to manipulate variables by combining assignment with arithmetic or bitwise operations. In this article, we explored various assignment operators, including addition, subtraction, multiplication, division, modulo, exponentiation, and floor division. By utilizing these operators, you can streamline your code, improve readability, and perform complex variable manipulations in a more efficient manner. Understanding and utilizing assignment operators effectively will empower you to write clean and expressive Python code while optimizing your programming workflow.

Logical and comparison operators

Logical operators and comparison operators are indispensable tools in Python programming, enabling you to make decisions, compare values, and control the flow of your code based on conditions. Understanding these operators is essential for writing robust and efficient programs. In this article, we will explore both logical and comparison operators in Python, delving into their functionalities and practical use cases. By the end, you'll have a solid grasp of how to leverage these operators to create logical conditions, perform comparisons, and implement sophisticated decision-making in your Python code.

  • Comparison Operators

Comparison operators compare values and return a Boolean result (True or False). They are used to check conditions, compare values, and make decisions.

NameOperator
Equals==
Not Equals!=
Greater than>
Less than<
Greater than or Equal to>=
Less than or Equal to<=
  • Logical operators

Logical operators are used to combine or manipulate boolean values (True or False) and evaluate conditions.

OperatorDescription
andReturns True if both statements are true
orReturns True if one of the statements is true
notReverse the result, returns False if the result is true

Equality Operator

The equality operator == compares two values and returns True if they are equal, and False otherwise.

x = 5
y = 8
 
print(x == y)  # Output: False
 
name1 = "John"
name2 = "John"
 
print(name1 == name2)  # Output: True

Inequality Operator

The inequality operator != compares two values and returns True if they are not equal, and False otherwise.

x = 5
y = 8
 
print(x != y)  # Output: True

Greater Than Operator

The greater than operator > compares two values and returns True if the left operand is greater than the right operand, and False otherwise.

x = 5
y = 8
 
print(x > y)  # Output: False

Less Than Operator

The less than operator < compares two values and returns True if the left operand is less than the right operand, and False otherwise.

x = 5
y = 8
 
print(x < y)  # Output: True

Greater Than or Equal To Operator

The greater than or equal to operator >= compares two values and returns True if the left operand is greater than or equal to the right operand, and False otherwise.

x = 5
y = 8
 
print(x >= y)  # Output: False

Less Than or Equal To Operator

The less than or equal to operator <= compares two values and returns True if the left operand is less than or equal to the right operand, and False otherwise.

x = 5
y = 8
 
print(x <= y)  # Output: True

Logical AND Operator

The logical AND operator and returns True if both the left and right operands are True, and False otherwise.

x = 5
y = 8
 
print(x > 0 and y < 10)  # Output: True

Logical OR Operator

The logical OR operator or returns True if either the left or right operands (or both) are True, and False if both operands are False.

x = 5
y = 8
 
print(x > 10 or y < 10)  # Output: True

Logical NOT Operator

The logical NOT operator not returns the inverse of the operand's Boolean value. It converts True to False and vice versa.

x = 5s
 
print(not x > 10)  # Output: True

By mastering both logical and comparison operators, you gain the ability to create complex conditions, implement decision-making logic, and control the behavior of your Python programs with precision. These operators serve as the building blocks for constructing robust algorithms, enabling you to handle various scenarios and conditions efficiently.