ConceptsData Type | the type of the value | Variable | stores a value | Identifier | name of a variable |
Data TypesString | Sequence of characters | “Hello World” | Integer | Whole numbers | 101 | Float | Decimal numbers | 71.24 | Boolean | True or False | 4 > 3 |
String ConcatenationA method for combining strings
print("Hello" + " World!") |
Type CastingA method for changing the data type of a value
price = 2.00
print("The apple is " + str(price) + "dollars") |
Type Cast Functionsstr() | converts a value to a string | int() | converts a value to an integer | float() | converts a value to a float | bool() | converts a value to a boolean |
Basic Mathematical Operators+ | Addition | - | Subtraction | * | Multiplication | / | Division |
Example of Mathematical Operationsnum1 = 5
num2 = 3
product = num1 * num2
print("Product:" + str(product))
|
| | Conditionan expression that uses relation operators and is either True or False
also known as a boolean expression |
Relational Operators== | Checks if the values are equal | != | Checks if the values are not equal | > | Checks if the left value is greater than the right value | < | Checks if the left value is less than the right value | >= | Checks if the left value is greater than or equal to the right value | <= | Checks if the left value is less than or equal to the right value |
If statementused to run instructions when the condition is True
if grade >= 90: print("Letter grade: A") |
If-else statementused to run instructions when the condition is True and when the condition is False
if grade >= 90: print("Letter grade: A")
else: print("You did not get an A") |
If-elifused to run instructions when multiple conditions are met
if grade >= 90: print("Letter grade: A")
elif grade >= 80: print("Letter grade: B")
else: print("Letter grade: unknown") |
| | For Loopused to repeat a set of instructions for a sequence of values
grade_list = [88,90,68,78,89,96,100,40]
for grade in grade_list:
print(grade) '
prints each grade in grade_list |
range(stop)is a function that returns a sequence of numbers starting from 0 to stop-1.
for x in range(6):
print(x)
prints x when x is 0, 1, 2, 3, 4 and 5 |
range(start, stop)is a function that returns a sequence of numbers starting from start to stop-1.
for x in range(1, 6):
print(x)
prints x when x is 1, 2, 3, 4 and 5 |
range(start, stop, step)is a function that returns a sequence of numbers starting from start to stop-1 while increasing by step.
for x in range(1, 6, 2):
print(x)
prints x when x is 1, 3 and 5 |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets