Concepts
Data Type |
the type of the value |
Variable |
stores a value |
Identifier |
name of a variable |
Data Types
String |
Sequence of characters |
“Hello World” |
Integer |
Whole numbers |
101 |
Float |
Decimal numbers |
71.24 |
Boolean |
True or False |
4 > 3 |
String Concatenation
A method for combining strings
print("Hello" + " World!")
|
Type Casting
A method for changing the data type of a value
price = 2.00
print("The apple is " + str(price) + "dollars")
|
Type Cast Functions
str() |
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 Operations
num1 = 5
num2 = 3
product = num1 * num2
print("Product:" + str(product))
|
|
|
Condition
an 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 statement
used to run instructions when the condition is True
if grade >= 90: print("Letter grade: A")
|
If-else statement
used 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-elif
used 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 Loop
used 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