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

Running Python Programs

Introduction

  • Why this topic matters: Understanding how to run Python programs is essential for applying your knowledge and solving real-world problems.
  • What you'll learn: In this tutorial, we will cover the basics of running Python programs, exploring key concepts, practical examples, common issues, solutions, best practices, and key takeaways.

Core Concepts

To run a Python program, you need to have Python installed on your computer. You can write your code in a text editor or use an Integrated Development Environment (IDE) like PyCharm, Jupyter Notebook, or Visual Studio Code.

Once you've written your code, save it with a .py extension, such as my_program.py. To run the program, open a terminal or command prompt, navigate to the directory containing your Python file, and use the following command:

python my_program.py

Interactive Mode

You can also run Python in interactive mode by simply typing python in your terminal or command prompt, which allows you to write and execute code line-by-line without saving it in a file first.

Practical Examples

Let's create a simple Python program that prints "Hello, World!" to the console:

# my_program.py
print("Hello, World!")

To run this program, save it as my_program.py, navigate to its directory in your terminal or command prompt, and execute the following command:

python my_program.py

You should see "Hello, World!" printed on the screen.

Common Issues and Solutions

NameError

What causes it: This error occurs when you try to use a variable that hasn't been defined yet in your code.

# bad_code.py
print(my_variable)

Error message:

Traceback (most recent call last):
  File "bad_code.py", line 1, in <module>
    print(my_variable)
NameError: name 'my_variable' is not defined

Solution: Define the variable before using it in your code.

# corrected_code.py
my_variable = "Hello"
print(my_variable)

Why it happens: Python doesn't automatically declare variables, so you must define them before using them.

How to prevent it: Always make sure to declare your variables before using them in your code.

TypeError

What causes it: This error occurs when you try to perform an operation on objects of incompatible types.

# bad_code.py
print(1 + "2")

Error message:

Traceback (most recent call last):
  File "bad_code.py", line 1, in <module>
    print(1 + "2")
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Solution: Make sure to use the appropriate operators for your data types.

# corrected_code.py
print(str(1) + "2")

Why it happens: Python doesn't support adding an integer and a string directly. You need to convert one of them into a string before performing the addition operation.

How to prevent it: Always check the data types of your variables before performing operations on them, and use appropriate conversion functions if necessary.

Best Practices

  • Use meaningful variable names for easy understanding and debugging.
  • Keep your code organized by using functions, modules, and classes as needed.
  • Comment your code to explain what it does and why you've made certain decisions.
  • Test your code with different input values to ensure it works correctly in various scenarios.
  • Use version control systems like Git to manage your code and collaborate with others.

Key Takeaways

  • To run a Python program, save it as a .py file and use the command python filename.py.
  • You can also run Python in interactive mode for quick testing and experimentation.
  • Be aware of common errors like NameError and TypeError and learn how to prevent them.
  • Follow best practices such as using meaningful variable names, commenting your code, and testing it thoroughly.
  • Keep learning and exploring Python's features to solve more complex problems effectively.