Sunday, December 22, 2024

Python Introduction | Learn Python Programming Language | Getting Started with Python Programming

  1. What is Python?
  • Python is a popular programming language.
  • Created by Guido van Rossum and released in 1991.
  1. Uses of Python:
  • Web development (server-side).
  • Software development.
  • Mathematics.
  • System scripting.

What can Python do?

  1. Python can be used on a server to create web applications.
  2. Python can be used alongside software to create workflows.
  3. Python can connect to database systems and also read and modify files.
  4. Python can handle big data and perform complex mathematics.
  5. Python is suitable for rapid prototyping or production-ready software development.

Why Python?

  1. Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.).
  2. Python has a simple syntax similar to the English language.
  3. Python’s syntax allows developers to write programs with fewer lines than many other programming languages.
  4. Python runs on an interpreter system, enabling immediate code execution (ideal for quick prototyping).
  5. Python supports different programming approaches:
  • Procedural.
  • Object-Oriented.
  • Functional.

Good to Know:

  1. The most recent major version of Python is Python 3 (used in this tutorial).
  2. Python 2 is no longer updated except for security fixes but remains popular.
  3. Python code can be written in:
  • Text editors.
  • Integrated Development Environments (IDEs) such as Thonny, Pycharm, NetBeans, or Eclipse (useful for managing larger projects).

Python Syntax Compared to Other Programming Languages:

  1. Python was designed for readability with similarities to the English language and influences from mathematics.
  2. Python uses new lines to complete a command instead of semicolons or parentheses.
  3. Python relies on indentation (whitespace) to define scope (e.g., loops, functions, and classes), unlike other languages that use curly brackets.

Example Code

print("Hello, World!")
💥 YouTube https://www.youtube.com/channel/UCJojbxGV0sfU1QPWhRxx4-A
💥 Blog https://localedxcelcambridgeictcomputerclass.blogspot.com/

Here is a detailed list of all the points from the content:

Python Getting Started

Python Installation

  1. Many PCs and Macs come with Python pre-installed.
  2. To check if Python is installed:
  • On Windows:
  • Search in the Start menu for “Python.”
  • Or run the command:
  • python --version
  • On Linux/Mac:
  • Open the command line or Terminal and type:
  • python --version
  1. If Python is not installed, download it for free from https://www.python.org/.

Python Quickstart

  1. Python is an interpreted programming language, meaning you write Python files (.py) in a text editor and execute them with the Python interpreter.
  2. To run a Python file from the command line:
  • python helloworld.py
  • Here, helloworld.py is the name of the Python file.
  1. Example of a simple Python program:
  • Create a file named helloworld.py:
  • print("Hello, World!")
  • Save the file, open the command line, navigate to the directory where the file is saved, and run:
  • python helloworld.py
  • Output:
  • Hello, World!

Python Version

  1. To check the Python version in the editor or your system:
  • import sys print(sys.version)
  1. You will learn more about importing modules in the Python Modules chapter.

The Python Command Line

  1. Python can be run directly from the command line, which is useful for testing small snippets of code.
  2. To start Python in the command line:
  • For Windows, Mac, or Linux:
  • python
  • If the python command does not work, try:
  • py
  1. Example of running Python code directly in the command line:
  • Start Python:
  • python
  • Then enter:
  • print("Hello, World!")
  • Output:
  • Hello, World!
  1. To exit the Python command line interface:
  • exit()


===================================================================

Execute Python Syntax

  1. Executing Python Syntax in the Command Line

    • Python code can be executed directly in the Command Line interface.
    • Example:
      >>> print("Hello, World!")
      Hello, World!
      
  2. Executing Python Syntax via a File

    • Create a .py file (e.g., myfile.py) and run it in the Command Line:
      C:\Users\Your Name>python myfile.py
      

Python Indentation

  1. Definition

    • Indentation refers to the spaces at the beginning of a code line.
    • While in other programming languages indentation is for readability only, in Python, it is mandatory and used to indicate a block of code.
  2. Example of Proper Indentation

    if 5 > 2:
        print("Five is greater than two!")
    
  3. What Happens Without Indentation?

    • Python will throw a Syntax Error if indentation is missing.
    • Example:
      if 5 > 2:
      print("Five is greater than two!")  # Syntax Error
      
  4. Number of Spaces

    • The number of spaces for indentation is flexible but must be consistent within the same block of code.
    • Common practice: Use 4 spaces.
    • Examples of proper indentation:
      if 5 > 2:
          print("Five is greater than two!")  # 4 spaces
      
      if 5 > 2:
              print("Five is greater than two!")  # More spaces (valid)
      
  5. Error for Inconsistent Indentation

    • Mixing different numbers of spaces in the same block leads to a Syntax Error.
    • Example:
      if 5 > 2:
          print("Five is greater than two!")  # 4 spaces
              print("This will cause an error!")  # 8 spaces (Syntax Error)
      

Python Variables

  1. Creating Variables

    • Variables are created when you assign a value to them.
    • Example:
      x = 5
      y = "Hello, World!"
      
  2. No Declaration Needed

    • Python does not require a specific command to declare variables.
    • You will learn more in the "Python Variables" chapter.

Python Comments

  1. Purpose of Comments

    • Comments are used for in-code documentation and to make code more readable.
  2. How to Write Comments

    • Start a comment with the # symbol.
    • Python ignores the rest of the line after the #.
  3. Example of Comments

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

Purpose of Comments in Python

  1. Explain Python Code

    • Comments help clarify what the code does.
  2. Improve Code Readability

    • They make the code easier to understand for others (or your future self).
  3. Prevent Code Execution

    • Comments can be used to temporarily disable code for testing purposes.

Creating a Comment

  1. Single-Line Comments

    • Start with #, and Python will ignore the rest of the line.
    • Example:
      # This is a comment
      print("Hello, World!")
      
  2. Comments at the End of a Line

    • Add a comment after the code on the same line.
    • Example:
      print("Hello, World!")  # This is a comment
      
  3. Prevent Execution with Comments

    • Use # to comment out lines of code.
    • Example:
      # print("Hello, World!")
      print("Cheers, Mate!")
      

Multiline Comments

  1. Using # for Each Line

    • Write a # symbol before each line of the comment.
    • Example:
      # This is a comment
      # written in
      # more than just one line
      print("Hello, World!")
      
  2. Using Multiline Strings as Comments

    • Use triple quotes (""" or ''') to create a multiline string.
    • As long as the string is not assigned to a variable, Python will treat it as a comment.
    • Example:
      """
      This is a comment
      written in
      more than just one line
      """
      print("Hello, World!")
      

Exercise

  • Question: Which character is used to define a Python comment?
    • Correct Answer: #









No comments:

Post a Comment