Python Tutorial — Learn Python Step by Step
Key takeaway: Master Python programming from scratch — variables, loops, functions, OOP, file handling, and libraries — with clear human-readable explanations and runnable code examples.
Python Type Conversion
Type conversion (casting) changes a value from one data type to another. Python provides built-in functions for explicit conversion and also performs some implicit conversions automatically.
Explicit Type Conversion (Casting)
# int() — convert to integer
a = int("42") # str to int: 42
b = int(3.99) # float to int: 3 (truncates, does NOT round)
c = int(True) # bool to int: 1
d = int("0xFF", 16) # hex string to int: 255
# float() — convert to float
e = float("3.14") # str to float: 3.14
f = float(42) # int to float: 42.0
g = float("inf") # infinity
# str() — convert to string
h = str(100) # "100"
i = str(3.14) # "3.14"
j = str(True) # "True"
k = str([1, 2, 3]) # "[1, 2, 3]"
# bool() — convert to boolean
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False (empty string)
print(bool("hello")) # True (non-empty string)
print(bool([])) # False (empty list)
print(bool([1])) # True (non-empty list)Implicit Type Conversion
# Python automatically converts int to float in mixed arithmetic
result = 5 + 3.0 # int + float = float (8.0)
print(type(result)) # <class 'float'>
# bool to int in arithmetic
total = True + True + False # 1 + 1 + 0 = 2
# String concatenation requires explicit conversion
age = 25
# print("Age: " + age) # TypeError!
print("Age: " + str(age)) # "Age: 25" (explicit)
print(f"Age: {age}") # "Age: 25" (f-string, preferred)Common Conversion Errors
# ValueError — invalid string for the target type
# int("hello") # ValueError: invalid literal
# int("3.14") # ValueError: must be whole number string
# Use: int(float("3.14")) for "3.14" → 3
# Safe conversion with error handling
user_input = "abc"
try:
number = int(user_input)
except ValueError:
print(f"Cannot convert '{user_input}' to int")
# Practical: safely read numeric input
age_str = input("Enter age: ")
try:
age = int(age_str)
print(f"You are {age} years old")
except ValueError:
print("Please enter a valid number")Conversion Reference Table
| Function | Converts To | Example | Result |
|---|---|---|---|
int(x) | Integer | int("42") | 42 |
float(x) | Float | float("3.14") | 3.14 |
str(x) | String | str(100) | "100" |
bool(x) | Boolean | bool(0) | False |
list(x) | List | list("abc") | [\"a\",\"b\",\"c\"] |
tuple(x) | Tuple | tuple([1,2]) | (1, 2) |
set(x) | Set | set([1,1,2]) | {1, 2} |
Key Takeaways
int(),float(),str(),bool()are the core conversion functions.int()truncates floats (3.99 → 3); useround()for rounding.- Invalid conversions raise
ValueError— always wrap user input conversion in try/except. - Python auto-converts int→float in mixed arithmetic but NOT str+int (requires explicit cast).
- "Falsy" values:
0,0.0,"",[],{},None,False. Everything else is truthy.
Python Programming Tutorial — Learn Python in a practical, step-by-step way. According to the official Python documentation, Python emphasizes code readability and simplicity, making it ideal for beginners.
Related tools: Python Online Compiler | Python Examples | JavaScript Tutorial | Java Tutorial
Python Career & Learning Guide
Python is the world's most popular programming language for beginners, data science, AI/ML, web development, and automation. This tutorial teaches Python step-by-step with clear explanations and runnable code examples in our free Python Compiler.
Why Learn Python in 2026?
- #1 Most Popular Language: Ranked first on TIOBE, Stack Overflow, and GitHub.
- AI and Data Science: The primary language for machine learning (TensorFlow, PyTorch, scikit-learn) and data analysis (Pandas, NumPy).
- Web Development: Django and Flask power scalable backends at companies like Instagram and Spotify.
- Automation & Scripting: Automate files, emails, web scraping, and data processing in minutes.
Python vs Other Languages
| Feature | Python | Java | JavaScript | C++ |
|---|---|---|---|---|
| Syntax | Very clean, readable | Verbose | Moderate | Complex |
| Typing | Dynamic, strong | Static, strong | Dynamic, weak | Static, strong |
| Best For | AI/ML, Data, Web | Enterprise, Android | Web Frontend/Backend | Systems, Games |
| Learning Time | 2–4 weeks basics | 4–6 weeks basics | 3–4 weeks basics | 8–12 weeks basics |
Frequently Asked Questions
Do I need prior programming experience to learn Python?
No! Python is specifically designed to be beginner-friendly. This tutorial starts from zero and builds up gradually.
Which Python version should I use?
Python 3.10+ is recommended. All examples in this tutorial use Python 3 syntax.
Is this Python tutorial completely free?
Yes, 100% free with no registration or sign-up required. Access all lessons and code examples anytime.