Show Menu
Cheatography

Computer Language - Unit 4 - Selections Cheat Sheet (DRAFT) by

Python - Computer Language

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Comparison Operators

radius = 4
Python Operator
Name
Example
Result
<
less than
radius < 0
FALSE
<=
less than or equal to
radius <= 0
FALSE
>
greater than
radius > 0
TRUE
>=
greater than or equal to
radius >= 0
TRUE
==
equal to
radius == 0
FALSE
!=
not equal to
radius != 0
TRUE

Generating Random Numbers

randint(a, b) # random int between a and b

1 import random
2
3 # Generate random numbers
4
5 number2 = random.randint(0, 9)
6
7 # Prompt the user to enter an answer
8 answer = eval(input("What is "+ + " + "
9 + str(number2) + "? "))
10
11 # Display result
12 print(number1, "+", number2, "=", answer,
13 "is", number1 + number2 == answer)

If statements

One-way If statements
A one-way if statement executes the statements if the condition is true.
Two-Way if-else Statements
A two-way if-else statem­ent.A two-way if-else statement decides which statements to execute based on whether the condition is true or false.
Nested if and Multi-Way if-eli­f-else Statements
One if statement can be placed inside another if statement to form a nested if statement.

Logical Operators

Operator
Descri­ption
not
logical negation
and
logical conjun­ction
or
logical disjun­ction

Operator Precedence Chart

+, - (Unary plus and minus)
** (Expon­ent­iation)
not
*, /, //, % (Multi­pli­cation, division, integer division, and remainder)
+, - (Binary addition and subtra­ction)
<, <=, >, >= (Compa­rison)
==, != (Equality)
and
or
=, +=, -=, *=, /=, //=, %= (Assig­nment operators)

one-way if

1 number = eval(input("Enter an integer: "))
2
3 if :
4 print("HiFive")
5
6 if :
7 print("HiEven")

two-way if-else

1 import random
2
3 # 1. Generate two random single-digit integers
4 number1 = random.randint(0, 9)
5 number2 = random.randint(0, 9)
6
7 # 2. If number1 < number2, swap number1 with number2
8 if number1 < number2:
9 number1, number2 = number2, number1 # Simultaneous assignment
10
11 # 3. Prompt the student to answer "What is number1 - number2?"
12 answer = eval(input("What is "+ str(number1) + " - " +
13 str(number2) + "? "))
14
15 # 4. Check the answer and display the result
16 if number1 - number2 == answer:
17 print("You are correct!")
18 else:
19 print("Your answer is wrong.\n", number1, '-',
20 number2, "is", number1 - number2, '.')

Nested if and Multi-Way if-eli­f-else

1 year = eval(input("Enter a year: "))
2 zodiacYear = year % 12
3 if zodiacYear == 0:
4 print("monkey")
5 elif zodiacYear == 1:
6 print("rooster")
7 elif zodiacYear == 2:
8 print("lion")
9 elif zodiacYear == 3:
10 print("goat")
11 elif zodiacYear == 4:
12 print("rat")
13 elif zodiacYear == 5:
14 print("ox")
15 elif zodiacYear == 6:
16 print("tiger")
17 elif zodiacYear == 7:
18 print("rabbit")
19 elif zodiacYear == 8:
20 print("dragon")
21 elif zodiacYear == 9:
22 print("snake")
23 elif zodiacYear == 10:
24 print("horse")
25 else:
26 print("sheep")

Comput­eBMI.py

1 # Prompt the user to enter weight in pounds
2 weight = eval(input("Enter weight in pounds: "))
3
4 # Prompt the user to enter height in inches
5 height = eval(input("Enter height in inches: "))
6
7 KILOGRAMS_PER_POUND = 0.45359237 # Constant
8 METERS_PER_INCH = 0.0254 # Constant
9
10 # Compute BMI
11 weightInKilograms = weight * KILOGRAMS_PER_POUND
12 heightInMeters = height * METERS_PER_INCH
13 bmi = weightInKilograms / (heightInMeters * heightInMeters)
14
15 # Display result
16 print("BMI is", format(bmi, ".2f"))
17 if bmi < 18.5:
18 print("Underweight")
19 elif bmi < 25:
20 print("Normal")
21 elif bmi < 30:
22 print("Overweight")
23 else:
24 print("Obese")

LeapYe­ar.py

1 year = eval(input("Enter a year: "))
2
3 # Check if the year is a leap year
4 isLeapYear =
5
6
7 # Display the result
8 print(year, "is a leap year?", isLeapYear)