# This is a single-line comment in Python
"""
This is a
multi-line
comment in Python
"""
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.
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
= 1 + 2 + 3 + 4 + \
result 5 + 6 + 7 + 8
# These are two short statements placed on the same line using a semicolon
= 5; y = 10 x
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.
= 5 # An integer
myint1 = -42 # Another integer
myint2
= 3.14 # A float
myfloat1 = -0.007 # Another float myfloat2
String
Strings are sequences of characters, enclosed in single or double quotes.
= 'Hello, World!' # A string using single quotes
mystring1 = "Hello, World!" # A string using double quotes mystring2
Boolean
A binary variable, having two possible values called True
and False
.
= True # A boolean with the value True
x = False # A boolean with the value False y
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
= 10
x
# Assigning a float value to a variable
= 5.5
y
# Assigning a string value to a variable
= "John Doe"
name
# Assigning a boolean value to a variable
= True is_active
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
= 1, 2, 3
a, b, c
# assign the same value to multiple variables
= y = z = 8 x
Mathematical and comparison operations
Mathematical Operations
# Addition (+)
= 5 + 3 # a will be 8
a
# Subtraction (-)
= 5 - 3 # b will be 2
b
# Multiplication (*)
= 5 * 3 # c will be 15
c
# Division (/)
= 15 / 3 # d will be 5.0
d
# Modulus (%)
= 7 % 3 # e will be 1
e
# Exponentiation (**)
= 2 ** 3 # f will be 8
f
# Floor Division (//)
= 7 // 3 # g will be 2 g
Comparison Operations
# Equal to (==)
= (5 == 3) # result will be False
result
# Not equal to (!=)
= (5 != 3) # result will be True
result
# Greater than (>)
= (5 > 3) # result will be True
result
# Less than (<)
= (5 < 3) # result will be False
result
# Greater than or equal to (>=)
= (5 >= 3) # result will be True
result
# Less than or equal to (<=)
= (5 <= 3) # result will be False result
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
.
= True and False # x will be False
x = True and True # y will be True y
or
Returns True
if at least one operand is True
, otherwise returns False
.
= True or False # x will be True
x = False or False # y will be False y
not
Returns True
if the operand is False
, and False
if the operand is True
.
= not True # x will be False
x = not False # y will be True y
You can combine multiple logical operators and use parentheses to group conditions and control the order of evaluation.
= 5
a = 3
b = 2
c
= (a > b) and (c < b) # result will be True result
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
= "your student id" # @param {type:"string"}
student_id = "your name" # @param {type:"string"} name
# @title #### 00. Area of a circle
from rggrader import submit
= 5
r
# TODO:
# Compute the area of a circle with radius r.
# Store the result in the 'area' variable.
# Put your code here:
= 0
area
# ---- End of your code ----
# Submit Method
= "00_python-basics"
assignment_id = "00_area-of-circle"
question_id str(area), question_id)
submit(student_id, name, assignment_id,
# Expected Output: The area of a circle with radius 5