Lesson 1.4: Data Types and Operators in Python

Quick Warm-Up

Everyone type this and see what happens:

print(5 + 3)
print("5" + "3")

What's different about the results?

Discussion: Why did Python treat these differently?

From Shell to Module in IDLE

  • Interactive Shell
    • Quick testing
    • Runs one line at a time
    • Code disappears when you close IDLE

Module (Script) Mode in IDLE

  • Write full programs
    • Saved as .py files
    • Can be re-run anytime
  • Run with F5
    • Executes entire script at once
  • Benefits
    • Permanent record of code
    • Easier debugging
    • Shareable and reusable

Python's Data Types

Python treats different kinds of data differently:

  • Numbers for math
  • Text for words and messages
  • True/False for yes/no decisions

Let's explore each type with real code!

Integers: Whole Numbers

Integers are whole numbers (no decimal point):

age = 16
temperature = -5
score = 100
big_number = 1000000

Try it: Create variables with your age, favorite number, and a negative number

Floating Point Numbers: Decimals

Floats have decimal points:

price = 19.99
height = 5.75
pi = 3.14159
temperature = 98.6

Code-along: Make variables for your height and the price of something you want to buy

Strings: Text Data

Strings are text enclosed in quotes:

name = "Sarah"
message = "Hello, world!"
address = "123 Main Street"
empty_string = ""

Your turn: Create string variables for your name, favorite movie, and favorite food

Comments: Notes in Your Code

Comments help you document your code - Python ignores them completely:

# This is a comment
age = 16  # This is also a comment
print(age)

Try it: Add comments to explain what your variables represent

Checking Data Types

Use type() to see what kind of data you have:

print(type(42))
print(type(3.14))
print(type("hello"))

Code-along: Run this and see what Python tells you about each type

Mixing Different Data Types

What happens when you combine different types?

number = 5
text = "dogs"
print(number, text)

This works! But this doesn't:

number = 5
text = "dogs"
print(number + text)

Try both: See the difference between using a comma vs. plus sign

Converting Between Types

Type casting changes one type to another:

age_text = "16"
age_number = int(age_text)
print(age_number + 1)
price = 19.99
price_text = str(price)
print("The price is " + price_text)

Practice: Convert a number to text, then back to a number

Practice with Type Conversion

# Get user input and convert
age_input = input("How old are you? ")
age = int(age_input)
next_year_age = age + 1
print("Next year you'll be", next_year_age)

# Working with prices
price_input = input("Enter a price: ")
price = float(price_input)
with_tax = price * 1.08
print("With tax:", with_tax)

Your turn: Try this program with different inputs

Input is Always a String

Remember: input() always gives you a string, even if the user types a number!

age = input("How old are you? ")
print(type(age))  # This will be <class 'str'>

age_number = int(age)
print(type(age_number))  # This will be <class 'int'>

Code-along: Test this to see the types change

Arithmetic Operators

Python can do lots of math operations:

print(10 + 3)   # Addition
print(10 - 3)   # Subtraction
print(10 * 3)   # Multiplication
print(10 / 3)   # Division

Try each one: What results do you get?

More Math Operators

print(10 ** 3)  # Exponentiation (10 to the power of 3)
print(10 // 3)  # Floor division (integer division)
print(10 % 3)   # Modulus (remainder)

Experiment: Try these with different numbers

Question: What's the difference between / and //?

Operator Precedence

Python follows math order of operations:

print(2 + 3 * 4)        # What do you think this equals?
print((2 + 3) * 4)      # How about this?

Test your prediction: Run both and see if you were right

Remember: PEMDAS still applies in programming!

String Operators

You can do "math" with strings too:

first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name)
laugh = "ha"
big_laugh = laugh * 5
print(big_laugh)

Your turn: Create your full name and make a word repeat several times

Assignment Operators

Shortcut operators make code shorter:

score = 100
score = score + 10  # The long way
print(score)

score = 100
score += 10  # The shortcut way
print(score)

Try both methods: They do the same thing!

More Assignment Shortcuts

points = 50
points += 10   # Same as: points = points + 10
points -= 5    # Same as: points = points - 5
points *= 2    # Same as: points = points * 2
points /= 4    # Same as: points = points / 4

Code-along: Start with points = 50 and apply each operation

Different Bases

Python can work with numbers in different bases:

# Different ways to write the number 10
decimal = 10
binary = 0b1010     # Base 2
octal = 0o12        # Base 8  
hexadecimal = 0xA   # Base 16

print(decimal, binary, octal, hexadecimal)

Try it: All four should print as 10!

Converting Number Systems

number = 42
print("Decimal:", number)
print("Binary:", bin(number))
print("Octal:", oct(number))
print("Hex:", hex(number))

Experiment: Try this with your favorite number

Scientific Notation

For very large or very small numbers:

big_number = 1.5e6      # 1.5 * 10^6 = 1,500,000
small_number = 3.2e-4   # 3.2 * 10^-4 = 0.00032

print(big_number)
print(small_number)

Test it: Python automatically converts to regular notation

Floating Point Precision

Computers can't always store decimals perfectly:

print(0.1 + 0.2)        # You might be surprised!
print(0.1 + 0.2 == 0.3) # Is this True or False?

Run this: See why floating point math can be tricky

Putting It All Together: Enhanced Calculator

Let's build a calculator using what we learned:

print("Enhanced Calculator")
print("This calculator works with decimals!")

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)
print("Exponentiation:", num1 ** num2)
print("Floor division:", num1 // num2)
print("Remainder:", num1 % num2)

Code-along: Build this step by step together

Data Type Challenge

Write a program that:

  1. Asks for someone's name (string)
  2. Asks for their age (convert to int)
  3. Asks for their height in feet (convert to float)
  4. Calculates their age in 10 years
  5. Prints everything with appropriate messages

15 minutes: Work on this challenge

Show Your Solutions

Volunteers: Share your data type challenge programs

As we watch: Notice how different data types are used for different purposes

Assignment 1.4

Your assignment will practice:

  • Working with different data types (int, float, string)
  • Using various arithmetic operators
  • Converting between types
  • Building programs that combine multiple concepts

Key skill: Understanding when to use which data type