Introduction to Python

Welcome to your Python learning journey! Python is a high-level, interpreted programming language known for its readability and simplicity. It's widely used in data science, web development, artificial intelligence, and many other fields.

Why Python?

Python has become one of the most popular programming languages in the world for several reasons:

  • Readability: Python's syntax is clear and resembles English, making it easier to learn and understand.
  • Versatility: Python can be used for web development, data analysis, artificial intelligence, scientific computing, automation, and more.
  • Large Community: Python has a vast community of developers who contribute to its libraries and frameworks.
  • Rich Ecosystem: Python has thousands of libraries and frameworks that extend its functionality.
  • Cross-platform: Python runs on Windows, macOS, Linux, and many other platforms.

Python Versions

There are two major Python versions: Python 2 and Python 3. Python 2 reached end-of-life in 2020, so all new projects should use Python 3. In this course, we'll be using Python 3.

Python Interpreter

Python is an interpreted language, which means that Python code is executed line by line by the Python interpreter. This is different from compiled languages like C++ or Java, where the code is first compiled into machine code before execution.

Your First Python Program

Let's start with the classic "Hello, World!" program. In Python, it's just one line of code:

print("Hello, World!")

The print() function displays the specified message on the screen. In this case, it displays "Hello, World!".

Python Syntax

Python uses indentation to define code blocks. This is different from other languages that use braces {} or keywords like begin and end. For example:

if x > 0:
    print("x is positive")
    print("This is still part of the if block")
print("This is outside the if block")

In this example, the first two print statements are part of the if block because they are indented. The third print statement is outside the if block because it's not indented.

Comments in Python

Comments are lines of text that are not executed by the interpreter. They are used to explain the code and make it more readable. In Python, comments start with the # symbol:

# This is a comment
print("Hello, World!")  # This is also a comment

Multi-line Statements

In Python, statements usually end with a newline. However, you can use the backslash \ character to continue a statement on the next line:

total = 1 + 2 + 3 + \
       4 + 5 + 6

You can also use parentheses (), square brackets [], or curly braces {} to create multi-line statements without using the backslash:

total = (1 + 2 + 3 +
       4 + 5 + 6)

Python Identifiers

Identifiers are names given to variables, functions, classes, etc. In Python, identifiers can contain letters, digits, and underscores, but they cannot start with a digit. Python is case-sensitive, so myVar and myvar are different identifiers.

Python Keywords

Keywords are reserved words that have special meanings in Python. You cannot use keywords as identifiers. Some examples of Python keywords are if, else, for, while, def, class, etc.

Try running the "Hello, World!" program in the code playground below!

Quick Quiz

What function is used to display output in Python?

Code Playground

Code output will appear here...