1.1 Variables and Data Types

Chapter 1: Python Essentials 1.2 Operators and Expressions

Loading content...

1.1 Variables and Data Types

Module 1.1 — Variables & Data Types | Python for ML 🐍 Module 1.1 • Python for Machine Learning Your first step in Python — learn how to store, label, and work with data. Every ML model starts with data, and data starts here! 🚀 📦 Daily-life analogy first Imagine you have labeled boxes at home. One box says "shoes", another says "books". You put the right items inside each box. Later, you just read the label to know what's inside. 📦 A variable in Python is exactly that — a labeled box that stores a piece of data. You choose the label (name), and Python remembers the value inside. 🏷️ A variable is a name that refers to a value stored in your computer's memory. You create it with the = sign (called the assignment operator). name = "Ahmed" # label = "name", value = "Ahmed" age = 22 # label = "age", value = 22 print(name) # reads the label, shows the value name = "Ahmed" means: "Create a box labeled name and put Ahmed inside." The label is name, the value inside is "Ahmed" ✅ True or False — Activity 1 of 15 A variable is a named location that stores data in memory. Python has simple rules for naming variables. Break a rule and Python will throw an error! ❌ ✏️ Must start with letter or underscore 🔤 Only letters, digits, underscores Hyphens and symbols not allowed Python treats uppercase & lowercase as different 🛑 Can't use Python keywords class, if, for are reserved 📝 Convention (not a rule, but everyone does it) Use snake_case for variables: student_name, total_price. This is the Python style. 🐍 🧠 Quick Check — Activity 2 of 15 Which of these is a valid Python variable name? ✅ True or False — Activity 3 of 15 In Python, Score and score are the same variable. 📊 The Four Basic Data Types Every value in Python has a type. Think of it as the category of the box's contents. Is it a number? Text? A yes/no answer? 🤔 Whole numbers — no decimal point. 📌 Examples: 5, -3, 1000, 0 🌍 Real life: 🎂 age = 22, 📦 quantity = 10 Numbers with a decimal point. 📌 Examples: 3.14, -0.5, 99.99 🌍 Real life: 💰 price = 49.99, 🌡️ temp = 36.6 Text — always wrapped in quotes. 📌 Examples: "hello", 'Python', "42" 🌍 Real life: 🧑 name = "Sara", 📧 email = "a@b.com" Only two values: True or False. 📌 Like a light switch — ON or OFF. 💡 🌍 Real life: ✅ is_student = True, 🔒 is_locked = False "42" is a string (text), not a number! Because it has quotes around it. Without quotes, 42 is an integer. Quotes make everything text. 📝 🧠 Quick Check — Activity 4 of 15 ✅ True or False — Activity 5 of 15 "123" is an integer in Python. False — it's a string because of the quotes 🎬 Animation — Variables Come Alive! Watch how Python creates variables one by one. Each box gets a name tag and a colored value! 🎨 📦 Variable Creation Animation See labeled boxes appear with their values and types 👆 Press Play to see variables being created step by step! 🔍 The type() Function — Your Data Detective 🕵️ Analogy: The magnifying glass Imagine you find a closed box. You don't know if it holds a number, text, or something else. You use a magnifying glass 🔍 to inspect it. That magnifying glass is type()! type() tells you what kind of data a variable holds. Super useful for debugging! 🐛 print(type(name)) # 🔍 What type is name? print(type(age)) # 🔍 What type is age? print(type(gpa)) # 🔍 What type is gpa? print(type(is_student)) # 🔍 What type is is_student? ✍️ Fill in the Blank — Activity 6 of 15 What does type(42) return? Type the answer: Hint: write exactly what Python shows, e.g. <class 'int'> 🧠 Quick Check — Activity 7 of 15 What does type("3.14") return? 💬 <class 'str'> — it has quotes! 🔄 Type Conversion — Changing Data Types 🍳 Analogy: Cooking ingredients Imagine a recipe says "add 2 cups of flour." But you have flour in a bag (raw), not measured in cups. You need to convert it to the right form. That's what type conversion does — it changes data from one type to another! 🥣 print(age_int + 5) # ✅ Now we can do math! 30 print(x) # 10.0 print("Price: $" + str(price)) # "Price: $99" print(bool(0)) # False (zero = False) print(bool(42)) # True (any non-zero = True) print(bool("")) # False (empty string = False) print(bool("hello")) # True (non-empty = True) ⚠️ Watch out! Not everything can be converted int("hello") will crash! 💥 Python can't turn the word "hello" into a number. Only numeric strings like "42" can be converted to int. ✍️ Fill in the Blank — Activity 8 of 15 📊 Calculate — Activity 9 of 15

Subject: Python for Machine Learning | Chapter: Chapter 1: Python Essentials

Chapter 1: Python Essentials 1.2 Operators and Expressions
Topics
Chapter 1: Python Essentials
Chapter 2: Python for Data Science
© Copyright © 2020 ALGOPK . All Rights Reserved.