🐍

Beginner's Python Crash Course Overview

Sep 16, 2024

Python Crash Course for Beginners

Introduction

  • Previous course used Eclipse IDE; now using VS Code.
  • Lecture uses a Python sandbox from a Django course on Udemy.
  • Sandbox contains starter files on strings, functions, classes, etc.

Setup

  • Download Python 3 from the official site.
  • Verify installation using python3 --version.
  • Use VS Code’s integrated terminal for running Python scripts.

Variables and Data Types

  • Variables are containers for values.
  • No semicolons or var keyword needed in Python.
  • Common data types: int, float, str, bool.
  • Boolean values must capitalize True/False.
  • Multiple assignment is possible in Python.
  • Example: X, Y, name, is_cool = (1, 2.5, 'John', True).

Comments

  • Single-line comment: # Comment
  • Multi-line comment: ''' Comment ''' or """ Comment """

Strings

  • Use single or double quotes.
  • Concatenation with + operator.
  • String formatting with f-strings and .format().

Lists

  • Created using brackets [].
  • Allows duplicate members and is ordered.
  • Methods: append, remove, insert, pop, reverse, sort.
  • Example: fruits = ['apple', 'orange']

Tuples

  • Created using parentheses ().
  • Ordered but unchangeable, duplicates allowed.
  • Single value tuples need trailing comma.

Sets

  • Created using braces {}.
  • Unordered, unindexed, no duplicates allowed.
  • Example methods: add, remove, clear.

Dictionaries

  • Created using curly braces {} with key-value pairs.
  • Changeable, unordered, indexed.
  • Methods: keys, items, copy, clear.
  • Access values using keys or .get() method.

Functions

  • Defined with def keyword.
  • Can have default parameters.
  • Return values using return keyword.

Lambda Functions

  • Anonymous functions using lambda keyword.
  • Single-expression, returns value.

Conditionals

  • Use if, elif, else without parentheses.
  • Logical operators: and, or, not.
  • Membership operators: in, not in.

Loops

  • for loops iterate over sequences (lists, strings, etc.).
  • while loops run while a condition is true.
  • break to exit loop, continue to skip iteration.

Modules

  • Import core modules or install external ones using pip.
  • Example: import datetime, from datetime import date.
  • Custom modules can be created and imported.

Classes

  • Template for creating objects.
  • Use class keyword.
  • Constructors defined with __init__().
  • Methods within class need self parameter.

Files

  • Open, read, write, and delete files.
  • Use open() with modes ('w', 'a', 'r+').
  • Example: file = open('file.txt', 'w')

JSON

  • Parse JSON to Python dictionary using json.loads().
  • Convert dictionary to JSON using json.dumps().

Conclusion

  • Basic concepts necessary for programming in Python.
  • Recommended next steps: tutorials, books, or projects.
  • Promo for a Django course associated with this lecture.