Course Topics
Python Basics Introduction and Setup Syntax and Indentation Comments and Documentation Running Python Programs Exercise Variables and Data Types Variables and Assignment Numbers (int, float, complex) Strings and Operations Booleans and None Type Conversion Exercise Operators Arithmetic Operators Comparison Operators Logical Operators Assignment Operators Bitwise Operators Exercise Input and Output Getting User Input Formatting Output Print Function Features Exercise Control Flow - Conditionals If Statements If-Else Statements Elif Statements Nested Conditionals Exercise Control Flow - Loops For Loops While Loops Loop Control (break, continue) Nested Loops Exercise Data Structures - Lists Creating and Accessing Lists List Methods and Operations List Slicing List Comprehensions Exercise Data Structures - Tuples Creating and Accessing Tuples Tuple Methods and Operations Tuple Packing and Unpacking Exercise Data Structures - Dictionaries Creating and Accessing Dictionaries Dictionary Methods and Operations Dictionary Comprehensions Exercise Data Structures - Sets Creating and Accessing Sets Set Methods and Operations Set Comprehensions Exercise Functions Defining Functions Function Parameters and Arguments Return Statements Scope and Variables Lambda Functions Exercise String Manipulation String Indexing and Slicing String Methods String Formatting Regular Expressions Basics Exercise File Handling Opening and Closing Files Reading from Files Writing to Files File Modes and Context Managers Exercise Error Handling Understanding Exceptions Try-Except Blocks Finally and Else Clauses Raising Custom Exceptions Exercise Object-Oriented Programming - Classes Introduction to OOP Creating Classes and Objects Instance Variables and Methods Constructor Method Exercise Object-Oriented Programming - Advanced Inheritance Method Overriding Class Variables and Methods Static Methods Exercise Modules and Packages Importing Modules Creating Custom Modules Python Standard Library Installing External Packages Exercise Working with APIs and JSON Making HTTP Requests JSON Data Handling Working with REST APIs Exercise Database Basics Introduction to Databases SQLite with Python CRUD Operations Exercise Final Project Project Planning Building Complete Application Code Organization Testing and Debugging Exercise

Numbers (int, float, complex)

Introduction

You'll begin your Python journey with a fundamental understanding of numbers. Numbers are the building blocks of any programming language, allowing you to perform calculations and store values. In this lesson, we will cover three types of numbers: integers (int), floating-point numbers (float), and complex numbers (complex). By mastering these concepts, you'll be well on your way to creating powerful Python programs!

Core Concepts

Integers (int)

An integer is a whole number without decimal points. Examples include 0, 7, -5. You can use integers for counting items, such as the number of apples in a basket or the number of times a loop executes.

Floating-Point Numbers (float)

Floating-point numbers are real numbers that can have decimal points. Examples include 3.14, 0.0001, and -2.5. They are used to represent measurements with decimals, such as weight, temperature, or distance.

Complex Numbers (complex)

Complex numbers consist of a real part and an imaginary part. The general form is a + bi, where a is the real part, b is the imaginary part, and i is the square root of -1. Complex numbers are used in various mathematical and scientific applications, like solving equations with imaginary roots or calculating Fourier transforms.

Practical Examples

Let's dive into some examples:

# Integers
integer_value = 42
print(integer_value)  # Output: 42

# Floating-point numbers
float_value = 3.14159
print(float_value)    # Output: 3.14159

# Complex numbers
complex_value = 1 + 2j
print(complex_value)  # Output: (1+2j)

Common Issues and Solutions

TypeError

What causes it: Attempting to perform an operation with incompatible types, such as adding an integer and a float.

# Bad code example that triggers the error
integer_value = 42
float_value = 3.14
print(integer_value + float_value)

Error message:

TypeError: unsupported operand type(s) for +: 'int' and 'float'

Solution: Ensure that both operands are of the same type before performing an operation.

# Corrected code
integer_value = 42
float_value = 3.14
print(float(integer_value) + float_value)

Why it happens: Incompatible types cannot be directly combined in Python without proper conversion.
How to prevent it: Always ensure that both operands are of the same type before performing an operation or use a function like float() or int() to convert one value to the desired type.

Best Practices

  • Use integers when working with whole numbers, such as counting items or iterations.
  • Use floating-point numbers for measurements with decimals or real values.
  • Be aware of potential TypeError issues and convert data types accordingly.
  • Keep your code clean and easy to read by using appropriate variable names.

Key Takeaways

  • Understand the differences between integers, floating-point numbers, and complex numbers in Python.
  • Know how to perform basic operations with each type of number.
  • Learn to recognize and resolve common issues, such as TypeErrors, when working with numbers.
  • Follow best practices for writing clean, efficient, and maintainable code.

Next steps: Build upon your knowledge of numbers by learning about variables, data types, and operators in Python! Keep practicing and exploring the Python documentation for more insights into this powerful programming language. Happy coding!