Fundamentals of Programming with Python

Jul 8, 2024

Fundamentals of Programming with Python

Introduction

  • Presenter: Kevin
  • Audience: Beginners with no programming background
  • Goals: Learn basics of Python step-by-step until you can write and run your own source code.

Why Learn Programming?

  • Ubiquity of Programming: Apps on phone, software on PC – all are products of programming.
  • Benefits: Realize ideas through a series of steps that a computer can execute.
  • Example Applications: Games, workplace applications, scientific analyses.

Why Python?

  • Popularity: High demand; voted as the most desired language by Kevin's audience.
  • Usability: Easy to use and understand with concise syntax.
  • Versatility: Suitable for web apps, internal tools, games, scientific analysis.
  • Transferable Skills: Concepts learned in Python apply to other languages.

Setting Up Python

  1. Check Installation: python --version in Command Prompt.
  2. Install Python: If not found, download from python.org
  3. Installation Tips: Check 'add Python to PATH' during setup.
  4. Verify Installation: python --version should now show the version.

Writing and Running Python Code

  1. Python Terminal: Use Python terminal to execute code (python in Command Prompt).
  2. Basic Syntax: Correct syntax needed, e.g., 1 + 2, print("Hello World").
  3. Exit Python Terminal: Type exit().
  4. Text Editor: Use Notepad or more efficient IDEs (Integrated Development Environments).

IDE Installation

  • Visual Studio Code: Recommended, free, supports all major OS.
  • Install Extensions: Python extension.
  • Set Interpreter: Ctrl+Shift+PPython: Select Interpreter → Choose installed version.

Variables and Data Types

  1. Creating Variables:
    • Syntax: variable_name = value
    • Must follow rules (no spaces, starts with a letter, etc.)
  2. Example:
    red_bucket = "Kevin"
    red_bucket = 10  # Overwrites 'Kevin'
    
  3. Type Checking: type(variable) returns the type (e.g., str, int).
  4. Deleting Variables: del variable_name
  5. User Input:
    var = input("Message")
    

Operators

  1. Arithmetic Operators: +, -, *, /.
  2. Order of Operations: PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).
  3. Special Operators:
    • Exponentiation: **
    • Floor Division: //
    • Modulus: %

Strings

  1. Creating Strings: Enclosed in double or single quotes ("Hello" or 'Hello').
  2. Handling Quotes: Use alternate quote styles to handle quotes within strings.
  3. Whitespace: Space and newline characters do not affect code execution.

Comments

  1. Single-Line Comment: Precede with #.
  2. Multi-Line Comment: Use ''' or """.
  3. Usage: Explain code or temporarily disable code.

Conditional Statements (if, elif, else)

  1. Syntax:
    if condition:
        # Code block
    elif another_condition:
        # Code block
    else:
        # Code block
    
  2. Example: Check age for school level.
    if age < 5:
        print("Preschool")
    elif age == 5:
        print("Kindergarten")
    else:
        print("Other class")
    

Functions

  1. Defining a Function:
    def function_name(parameters):
        # Code block
    
  2. Example:
    def print_hello():
        print("Hello")
    
  3. Calling a Function:
    print_hello()
    
  4. Parameters and Return Values:
    def add(a, b):
        return a + b
    result = add(3, 4)  # Returns 7
    
  5. Using Functions with Conditional Logic: Functions can include if-statements.

Loops

  1. While Loop:
    while condition:
        # Code block
    
    Example:
    x = 0
    while x < 5:
        print(x)
        x += 1
    
  2. For Loop:
    for variable in iterable:
        # Code block
    
    Example:
    for x in range(5, 10):
        print(x)
    

Importing Libraries

  1. Syntax: import library_name
  2. Example:
    import math
    print(math.pi)  # Prints value of Pi
    

Error Handling

  1. Syntax Errors: Usually due to incorrect syntax. Fix by reviewing code.
  2. Runtime Errors: Occur during code execution (e.g., division by zero).
  3. Semantic Errors: Code runs but not as expected due to logical errors.

Conclusion

  • Covered basics of Python: Variables, operators, strings, comments, conditionals, loops, functions, importing libraries, and basic error handling.
  • Practice by exploring further and trying out more advanced projects.