Skip to main content
Jupyter Notebooks
CHAPTER 06 Beginner

Variables, Data Types, and Input Handling

Updated: May 18, 2026
5 min read

# 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:

python
1234567
# Creating variables
customer_name = "Alice"
age = 28
is_premium_member = True

# Displaying variables
print(customer_name)

*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, and AGE are 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:

python
123456789101112131415
# 1. Integer (Whole numbers)
quantity = 50
print(type(quantity))

# 2. Float (Decimals)
price = 19.99
print(type(price))

# 3. String (Text, wrapped in single or double quotes)
product_id = "ITEM-4421"
print(type(product_id))

# 4. Boolean (True or False)
in_stock = False
print(type(in_stock))

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:

python
12345678910111213141516
# A number stored as a string
string_num = "100"

# This will cause an error: string_num + 50 (Cannot add text and numbers)

# Convert string to integer
real_num = int(string_num)
print(real_num + 50)  # Output: 150

# Convert integer to string
new_string = str(real_num) + " dollars"
print(new_string)     # Output: 100 dollars

# Convert integer to float
decimal_num = float(real_num)
print(decimal_num)    # Output: 100.0

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:

python
123456
user = "David"
logins = 12

# Using an f-string
message = f"User {user} has logged in {logins} times this month."
message

*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:

python
12345
# The input() function pauses execution and waits for the user
user_age = input("Please enter your age: ")

print(f"You entered: {user_age}")
print(type(user_age)) 

*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:

python
1234
# Proper way to handle numeric input
birth_year = input("Enter your birth year: ")
age_in_2030 = 2030 - int(birth_year)
print(f"In 2030, you will be {age_in_2030} years old.")

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 (showing In [*]). 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, or int. This overwrites the built-in Python functions for that specific Kernel session.

8. MCQs

Question 1

How do you assign the value 10 to a variable named score in Python?

Question 2

Which of the following is an invalid variable name in Python?

Question 3

What function tells you the data type of a variable?

Question 4

The value 42.0 is what data type?

Question 5

How do you convert the string "55" into an integer?

Question 6

What does the f do in the statement: f"Hello {name}"?

Question 7

What data type does the input() function always return?

Question 8

If your Jupyter notebook shows In [*] and refuses to run any new cells, what is a likely cause related to this chapter?

Question 9

Is Age the same variable as age in Python?

Question 10

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 run result = userinput * 2 and 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.

11. Next Chapter Recommendation

In Chapter 7: Working with Functions and Modules, we will learn how to write reusable blocks of code and how to import powerful third-party libraries (like math and random) into our Jupyter Notebook.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·