Variables, Data Types, and Input Handling
# CHAPTER 6
Variables, Data Types, and Input Handling
1. Chapter Introduction
Now that you are comfortable navigating the Jupyter interface, it's time to write real Python code. In data science, everything starts with storing information. This chapter covers the fundamental building blocks of Python: how to create variables, understand the different types of data, convert between them, and use Jupyter's unique interactive input features.2. Variables in Python
A variable is like a labeled box where you store data. In Python, you don't need to declare the type of data beforehand (unlike Java or C++). You just assign a value using the equals sign =.
Cell 1:
*Output: Alice*
Naming Rules:
-
Must start with a letter or underscore (
).
- Cannot start with a number.
-
Can only contain alphanumeric characters and underscores (
A-z, 0-9, and).
-
Case-sensitive (
age,Age, andAGEare three different variables).
-
*Best Practice:* Use
snake_case(all lowercase, separated by underscores) for Python variables.
3. Core Data Types
Python has four primary primitive data types. You can always check a variable's type using the built-in type() function.
Cell 2:
4. Type Conversion (Casting)
Often, data comes in the wrong format (e.g., a number is stored as text). You must convert it before doing math.
Cell 3:
5. String Formatting (f-strings)
When building reports in Jupyter, you frequently need to combine variables with text. The modern and most readable way to do this in Python is using f-strings (formatted string literals). Place an f before the quotes and use curly braces {} for variables.
Cell 4:
*Output: 'User David has logged in 12 times this month.'*
6. Interactive Input Handling
Because Jupyter is interactive, you can actually pause the Kernel and ask the user to type something into a text box right inside the notebook!
Cell 5:
*Note: The input() function ALWAYS returns a String, even if the user types a number. You must cast it to an int if you want to do math.*
Cell 6:
7. Common Mistakes
-
The Kernel Hangs on Input: If you run an
input()cell, Jupyter stops and waits. If you don't realize this and try to run the next cell, it will just queue up (showingIn [*]). If your notebook is stuck, check if it is waiting for an input box to be filled!
-
Using reserved keywords: Don't name your variables
print,list,str, orint. This overwrites the built-in Python functions for that specific Kernel session.
8. MCQs
How do you assign the value 10 to a variable named score in Python?
Which of the following is an invalid variable name in Python?
What function tells you the data type of a variable?
The value 42.0 is what data type?
How do you convert the string "55" into an integer?
What does the f do in the statement: f"Hello {name}"?
What data type does the input() function always return?
If your Jupyter notebook shows In [*] and refuses to run any new cells, what is a likely cause related to this chapter?
Is Age the same variable as age in Python?
Why should you avoid naming a variable print = 5?
9. Interview Questions
- Q: Explain Python's dynamic typing. Do you need to declare a variable's type before using it?
-
Q: A user enters "10" into an
input()prompt. You try to runresult = userinput * 2and the output is"1010". What happened and how do you fix it?
10. Summary
Variables in Python are created dynamically via assignment (=). The four core types are integers, floats, strings, and booleans. You can convert between them using casting functions like int() and str(). For dynamic text, f-strings provide a clean syntax for injecting variables. Jupyter supports interactive data entry via the input() function, but remember that it pauses the Kernel until the user responds.