Python Basics

Basic syntax

Comment

Comments are completely ignored by the interpreter. They are meant for fellow programmers. In Python, there are two types of comments: - Single-line comment - Multi-line comment

We use the # symbol to write a single-line comment and triple quotes, either ''' or """ to write multi-line comment.

# This is a single-line comment in Python

"""
This is a
multi-line
comment in Python
"""

Indentation

Python uses indentation to define blocks of code, instead of curly braces. Both tabs and spaces are supported, but the standard indentation requires standard Python code to use four spaces

if 5 > 2:
    # Indented four spaces
    # This block has one level of indentation
    print("5 is greater than 2")

    if 10 > 5:
        # This block has two levels of indentation
        print("10 is greater than 5")

Lines

You can use a backslash if you need to place a long statement on multiple lines or put multiple short statements on the same line using a semicolon.

# This is a long statement broken into two lines by using a backslash
result = 1 + 2 + 3 + 4 + \
         5 + 6 + 7 + 8

# These are two short statements placed on the same line using a semicolon
x = 5; y = 10

Data types

Numbers

Python supports two types of numbers - integers(whole numbers) and floating point numbers(decimals).

Integers are whole numbers, both positive and negative, without decimals. Floats represent real numbers, both positive and negative, with decimal points.

myint1 = 5  # An integer
myint2 = -42 # Another integer

myfloat1 = 3.14   # A float
myfloat2 = -0.007 # Another float

String

Strings are sequences of characters, enclosed in single or double quotes.

mystring1 = 'Hello, World!' # A string using single quotes
mystring2 = "Hello, World!" # A string using double quotes

Boolean

A binary variable, having two possible values called True and False.

x = True  # A boolean with the value True
y = False # A boolean with the value False

Variables and value assignment

Variable names in Python should start with a letter or an underscore (_) and can be followed by any combination of letters, numbers, or underscores. They are case-sensitive, so var1 and Var1 are different variables. Assigning a value to a variable is done using the assignment operator (=).

# Assigning an integer value to a variable
x = 10

# Assigning a float value to a variable
y = 5.5

# Assigning a string value to a variable
name = "John Doe"

# Assigning a boolean value to a variable
is_active = True

Assignments can be done on more than one variable “simultaneously” on the same line, and you can assign the same value to multiple variables

# assign values to multiple variables
a, b, c = 1, 2, 3

# assign the same value to multiple variables
x = y = z = 8

Mathematical and comparison operations

Mathematical Operations

# Addition (+)
a = 5 + 3 # a will be 8

# Subtraction (-)
b = 5 - 3 # b will be 2

# Multiplication (*)
c = 5 * 3 # c will be 15

# Division (/)
d = 15 / 3 # d will be 5.0

# Modulus (%)
e = 7 % 3 # e will be 1

# Exponentiation (**)
f = 2 ** 3 # f will be 8

# Floor Division (//)
g = 7 // 3 # g will be 2

Comparison Operations

# Equal to (==)
result = (5 == 3) # result will be False

# Not equal to (!=)
result = (5 != 3) # result will be True

# Greater than (>)
result = (5 > 3) # result will be True

# Less than (<)
result = (5 < 3) # result will be False

# Greater than or equal to (>=)
result = (5 >= 3) # result will be True

# Less than or equal to (<=)
result = (5 <= 3) # result will be False

Logical operations

In Python, you can perform logical operations using the following logical operators: and, or, and not. They are used to combine or modify boolean values (True and False).

and

Returns True if both operands are True, otherwise returns False.

x = True and False # x will be False
y = True and True  # y will be True

or

Returns True if at least one operand is True, otherwise returns False.

x = True or False # x will be True
y = False or False # y will be False

not

Returns True if the operand is False, and False if the operand is True.

x = not True  # x will be False
y = not False # y will be True

You can combine multiple logical operators and use parentheses to group conditions and control the order of evaluation.

a = 5
b = 3
c = 2

result = (a > b) and (c < b) # result will be True

In this example, (a > b) is True, (c < b) is True, and as both conditions are True, the logical and operation returns True.

Exercise Python Basics

!pip install rggrader
# @title #### Student Identity
student_id = "your student id" # @param {type:"string"}
name = "your name" # @param {type:"string"}
# @title #### 00. Area of a circle
from rggrader import submit

r = 5 

# TODO: 
# Compute the area of a circle with radius r.
# Store the result in the 'area' variable.

# Put your code here:
area = 0


# ---- End of your code ----

# Submit Method
assignment_id = "00_python-basics"
question_id = "00_area-of-circle"
submit(student_id, name, assignment_id, str(area), question_id)

# Expected Output: The area of a circle with radius 5
Back to top