Basic Datatypes, Operators, and Structures - Examples
Basic Datatypes
str
exampleVar = "Hello World"
int
exampleVar = 1
float
exampleVar = 1.1
list
exampleVar = ["Hello", "World"]
tuple
exampleVar = ("Hello", "World")
dict
exampleVar = {"labelStr": "Hello World", "labelFlt":1.6}
bool
exampleVar = True
Basic Math Operators
+
exampleVar = 9 + 2
exampleVar = "Hello" + "World"
-
exampleVar = 9 - 2
*
exampleVar = 9 * 2
/
exampleVar = 9 / 2
//
exampleVar = 9 // 2
%
exampleVar = 9 % 2
Basic Structures
Functions
def exampleFunc(name):
message = f"Hello {name}, how are you today?"
return message
Loops
repeatNumber = 10
for name in range(repeatNumber):
print("Hello World")
Conditionals
number = 10
if number == 10:
print("This will print only if the variable 'number' is given the value: 10")
elif number == 20:
print("This will print only if the variable 'number' is given the value: 20")
else:
print("This will print only if the variable 'number' has neither values 10 nor 20")
Try Statements
try:
print("This will print if the code within the try statement runs successfully")
except
print("This will print if the code within the try statement excounters an error")
Last modified: 25 May 2024