Module 1 Cheatsheet - Python Basics
Module 1 Cheatsheet - Python Basics
lines of text
● # This is a comment
that are
ignored by the
Copied!
Python
interpreter
when
executing the
code
Variable Assigns a Syntax:
Assignment value to a
variable 1
● variable_name = value
Copied!
Example:
1
2
name
● x = 5 # assigning 5 to variable x
Copied!
Integer
● Float 1
● Boolean 2
● String
3
4
5
Copied!
message or
variable inside 1
`()` 2
● print("Hello, world")
● print(a+b)
Copied!
Concatenation Combines Syntax:
(concatenates)
strings 1
Copied!
Example:
1
Copied!
length of a
string 1
● len(string_name)
Copied!
Example:
1
2
● my_string="Hello"
● length = len(my_string)
Copied!
to uppercase
1
2
● my_string="Hello"
● uppercase_text = my_string.upper()
Copied!
to lowercase
1
2
● my_string="Hello"
● uppercase_text = my_string.lower()
Copied!
portion of the
string 1
● substring = string_name[start:end]
Copied!
Example:
1
2
● my_string="Hello"
● substring = my_string[0:5]
Copied!
character at a
specific index 1
2
● my_string="Hello"
● char = my_string[0]
Copied!
leading/trailing
whitespace 1
2
● my_string="Hello"
● trimmed = my_string.strip()
Copied!
substrings
1
2
● my_string="Hello"
Copied!
split() Splits string Example:
into a list
based on a 1
delimiter 2
● my_string="Hello"
● split_text = my_string.split(",")
Copied!
Python ● Example:
Operators Additio
n (+): 1
Adds 2
two
3
values
4
together
. 5
● Subtrac 6
tion (-):
7
Subtrac
8
ts one
value ● x=9
from ● y=4
another.
● result_add= x + y # Addition
● Multipli
● result_sub= x - y # Subtraction
cation
● result_mul= x * y # Multiplication
(*):
es two
● result_fdiv= x // y # Floor Division
values.
● result_mod= x % y # Modulo
● Division
(/):
Copied!
Divides
one
value
by
another,
returns
a float.
● Floor
Division
(//):
Divides
one
value
by
another,
returns
the
quotient
as an
integer.
● Modulo
(%):
Returns
the
remaind
er after
division
Author(s)
Pooja Patel
Contributor(s)
Malika Singla
Changelog