Earn 20 XP


Comments in Python

  • When writing code in Python, it's essential to ensure that your code can be easily understood by others, say by your friend who wants to see your code.
  • Python ignores everything after the hash mark and up to the end of the line. You can insert them anywhere in your code!
  • A shortcut for adding comments is by using CTRL + /

image.png

Operators

  • Operators perform simple additions, comparisons, etc., on variables and values.
  • Python supports the following types of operators. We will be looking at some of the commonly used operators.

image.png

Arithmetic Operators

Using Python, we can perform basic arithmetic operations such as addition, subtraction, multiplication, etc.

Example:

x = 2
y = 3
x + y # addition
x * y # multiplication

Snapshot of basic arithmetic operations on notebook

Aritmetic Operators: Meanings & Examples

image.png

Comparison Operator

  • These are used to compare two values
  • Gives a Boolean result (True/False)

image.png

Comparison Operators: Meanings & Examples

image.png

Logical Operator

  • Logical operators are used to combine conditional statements
  • Gives a Boolean result (True/False)

image.png

image.png

Variables

  • A variable can be considered as a storage container for data.
  • Every variable will have a name.
  • It is a good way to store information while making it easy to refer to that information in our code later.

image.png

  • For instance, instead of working with the number 3.14, we can assign it to a variable pi and use it as many times as we want in our code (wherever required).

  • The equal sign (=) is used to assign values to variables.

  • The syntax for assigning values to a variable is as follows: Variable name = value or information.

  • Example:

    • x = 5
    • y = "John"

Input and Output in Python

image.png

Input Function

image.png

image.png