Show Menu
Cheatography

Cheat sheet for the cereal data python lesson

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 Concat­enation

A method for combining strings
print(­"­Hel­lo" + " World!­")

Type Casting

A method for changing the data type of a value
price = 2.00

print(­"The apple is " + str(price) + "­dol­lar­s")

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 Mathem­atical Operators

+
Addition
-
Subtra­ction
*
Multip­lic­ation
/
Division

Example of Mathem­atical 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 instru­ctions when the condition is True
 if grade >= 90:
  print("Letter grade: A")

If-else statement

used to run instru­ctions 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 instru­ctions when multiple conditions are met
 if grade >= 90:
  print("Letter grade: A")

 elif grade >= 80:
  print("Letter grade: B")

 else:
  print("Letter grade: unknow­n")
 

For Loop

used to repeat a set of instru­ctions for a sequence of values
grade_list = [88,90­,68­,78­,89­,96­,10­0,40]

for grade in grade_­list:

 ­ ­pri­nt(­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):

 ­ ­pri­nt(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):

 ­ ­pri­nt(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):

 ­ ­pri­nt(x)

prints x when x is 1, 3 and 5
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter