CMPSC 100 Computational Expression
Introduction to Python programming fundamentals
def
Examples:
You can name variables almost anything, but:
❌ Cannot start with numbers:
✅ Can contain letters, numbers, underscores:
❌ Cannot use Python keywords:
Balance descriptive and easy to type:
Python variables can hold different types of data:
int
: Integer numbersfloat
: Decimal numbersstr
: Text (string)bool
: True or Falselist
: Collection of itemsExamples:
Use #
to create single-line comments:
Use triple quotes for multi-line comments:
Think of variables as containers or labeled boxes that hold values:
print()
to display outputExample:
How Python reads your code
Python executes code line by line, from top to bottom:
n = 5 # Step 1: assigns variable n the value 5
print(n) # Step 2: output: 5
n = 7 # Step 3: reassigns n a new value
print(n) # Step 4: output: 7
n = n + 2 # Step 5: assigns n a value based on prior value
print(n) # Step 6: output: 9
Complete output:
5
7
9
main()
function for your program’s main logicExample:
Comments
#
""" ... """
Examples: