Module 1
Python Foundations in JupyterLab
What is JupyterLab?
JupyterLab is like a digital notebook where you can write and run Python code in small chunks called cells. Think of it as a word processor for programmers - you can write code, see results immediately, and add notes to explain what you're doing.
Two Types of Cells
Code Cells: Where you write Python commands
Markdown Cells: Where you write notes and explanations (like this text)
💡 Running Cells
Press Shift + Enter to run a cell and move to the next one, or Ctrl + Enter to run without moving.
Built-in Functions
Python comes with ready-made functions that perform common tasks. Think of them as tools in a toolbox.
print() - Display Information
print("Hello, World!")
print("My name is Sarah")
print(42)
Output:
Hello, World!
My name is Sarah
42
type() - Check What Kind of Data You Have
print(type("Hello"))
print(type(25))
print(type(3.14))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
len() - Count Characters or Items
print(len("Python"))
print(len("Hello World"))
round() - Round Decimal Numbers
print(round(3.14159))
print(round(3.14159, 2))
help() - Get Information About Functions
help(print)
This will show you detailed information about how the print() function works.
input() - Get Information from User
name = input("What's your name? ")
print("Nice to meet you, " + name)
Output:
What's your name? John
Nice to meet you, John
Python Data Types
Data types are like categories that tell Python what kind of information you're working with.
Integers (int) - Whole Numbers
age = 25
score = -10
population = 1000000
print(type(age))
Floats (float) - Decimal Numbers
temperature = 98.6
price = 19.99
pi = 3.14159
print(type(temperature))
Booleans (bool) - True or False
is_sunny = True
is_raining = False
has_license = True
is_weekend = False
print(type(is_sunny))
print(is_sunny)
Output:
<class 'bool'>
True
Strings (str) - Text
name = "Alice"
message = 'Hello there!'
address = "123 Main Street"
print(type(name))
String Indexing and Slicing
Strings are like a row of mailboxes - each character has a position number (index).
String Indexing
word = "Python"
print(word[0]) # First character
print(word[1]) # Second character
print(word[-1]) # Last character
String Slicing
sentence = "Hello World"
print(sentence[0:5]) # Characters 0 to 4
print(sentence[6:]) # From position 6 to end
print(sentence[:5]) # From start to position 4
String Formatting
name = "Sarah"
age = 28
print(f"My name is {name} and I am {age} years old")
Output:
My name is Sarah and I am 28 years old
Type Casting
Sometimes you need to convert one data type to another. It's like translating between languages.
Converting to Integer
text_number = "25"
real_number = int(text_number)
print(type(text_number))
print(type(real_number))
print(real_number + 5)
Output:
<class 'str'>
<class 'int'>
30
Converting to String
number = 42
text = str(number)
print(type(number))
print(type(text))
print("The answer is " + text)
Output:
<class 'int'>
<class 'str'>
The answer is 42
Converting to Float
whole_number = 10
decimal_number = float(whole_number)
print(type(whole_number))
print(type(decimal_number))
print(decimal_number)
Output:
<class 'int'>
<class 'float'>
10.0
Data Types Comparison Table
| Data Type |
Description |
Example |
Use Case |
Common Operations |
| int |
Whole numbers |
42, -17, 0 |
Counting, ages, quantities |
+, -, *, /, % |
| float |
Decimal numbers |
3.14, -2.5, 0.0 |
Measurements, prices, temperatures |
+, -, *, /, round() |
| bool |
True or False values |
True, False |
Conditions, flags, yes/no questions |
and, or, not |
| str |
Text and characters |
"Hello", 'Python' |
Names, messages, addresses |
+, len(), upper(), lower() |
🎯 Practice Exercises
1. Create variables for your name, age, and favorite color. Print them using f-string formatting.
2. Take a string and practice slicing it to get the first 3 characters, last 2 characters, and middle portion.
3. Convert a string number to an integer, perform a calculation, then convert back to string for display.