Quick Start Guide to Python Basics

Sep 23, 2024

Getting Started with Python in Under 10 Minutes

Overview

  • Version: Python 3.8
  • Code Editor: PyCharm
  • Tools are free and open source.

Assigning Values to Variables

  • To create a variable, choose a name (e.g., item).
  • Assign a value (string, number).
    • Example: item = "orange".
  • Python is case-sensitive (e.g., item vs. Item).
  • Use underscores for multi-word variable names (e.g., item_name).

Printing Variables

  • Use print() to display values.
  • Example: print(item) will show orange.

Data Types

  1. Integer: Any whole number (e.g., 10).
  2. String: Text enclosed in quotes (e.g., "hello").
  3. Boolean: True or false values (e.g., True, False).
  4. List: Collection of values in brackets (e.g., my_list = ["apple", 2, False]).

Combining Different Data Types

  • Convert data types as needed to combine them.
    • Example: print(name + str(integer)) to combine string and integer.

Basic Math Operations

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: **

Logic Statements

  • Use if, elif, and else for conditional logic.
    • Example:
      if age > 21:
          print("You are old")  
      elif age == 18:
          print("You are getting old")  
      else:
          print("You are still young")  
      

Loops

For Loop

  • Syntax: for i in range(n):
  • Example:
    for i in range(3):
        print("hello" + str(i))  
    

While Loop

  • Syntax: while condition:
  • Example:
    i = 0
    while i < 5:
        print(i)
        i += 1
    

Functions

  • Define a function using def keyword.
  • Example:
    def say_hello(name):
        print(f"Hey there, {name}!")  
    
  • Reuse code by calling functions multiple times.

Placeholder Functions

  • Use pass for functions without logic to avoid errors.

Exception Handling

  • Use try and except blocks to handle errors.
    • Example:
    try:
        result = 10 + int(user_input)
    except ValueError:
        print("That is not a valid number")
    

Conclusion

  • This guide covers the basics of Python programming.
  • Many more topics to explore beyond this introductory guide.