Data Structures in Python

HaleInfinity6862 avatar
HaleInfinity6862
·
·
Download

Start Quiz

Study Flashcards

11 Questions

What is the primary difference between a list and a tuple in Python?

A tuple is immutable, while a list is mutable.

What is the purpose of the try-except block in Python?

To catch and handle exceptions

What is the result of the code my_dict = {'name': 'John', 'age': 30}; print(my_dict['name'])?

It prints 'John'

What is the purpose of inheritance in object-oriented programming?

To create a new class from an existing class

What is the result of the code my_set = {1, 2, 3}; print(my_set[0])?

It raises a TypeError

What is the purpose of raising a custom exception in Python?

To provide a more informative error message

What is the purpose of the open() function in file input/output?

To specify the file mode and return a file object

What is the main difference between Flask and Django?

Flask is a micro framework, while Django is a high-level framework

What is the purpose of the csv module in Python?

To work with CSV files

What is the purpose of the extends keyword in template inheritance?

To inherit from a parent template

What is the purpose of the class MyException(Exception): pass code?

To define a new exception type

Study Notes

Data Structures

  • Lists: Ordered collection of items, can be modified, and can contain duplicate values.
    • Create: my_list = [1, 2, 3]
    • Indexing: my_list[0] returns the first element
    • Slicing: my_list[1:3] returns a subset of the list
  • Tuples: Ordered, immutable collection of items.
    • Create: my_tuple = (1, 2, 3)
    • Indexing: my_tuple[0] returns the first element
  • Dictionaries: Unordered collection of key-value pairs.
    • Create: my_dict = {'name': 'John', 'age': 30}
    • Access: my_dict['name'] returns the value associated with the key
  • Sets: Unordered collection of unique items.
    • Create: my_set = {1, 2, 3}
    • Operations: union, intersection, difference

Object-Oriented Programming

  • Classes: Blueprints for creating objects.
    • Define: class MyClass: pass
    • Instantiate: my_obj = MyClass()
  • Objects: Instances of classes.
    • Attributes: my_obj.my_attribute
    • Methods: my_obj.my_method()
  • Inheritance: Mechanism for creating a new class from an existing class.
    • Single inheritance: class Child(Parent): pass
    • Multiple inheritance: class Child(Parent1, Parent2): pass

Exception Handling

  • Try-Except Blocks: Catch and handle exceptions.
    • try: ... except ExceptionType: ...
    • try: ... except ExceptionType as e: ...
  • Raising Exceptions: Manually raise an exception.
    • raise ExceptionType("Error message")
  • Custom Exceptions: Define and raise custom exceptions.
    • class MyException(Exception): pass

File Input/Output

  • Reading Files:
    • open() function: file = open('file.txt', 'r')
    • read() method: file.read()
  • Writing Files:
    • open() function: file = open('file.txt', 'w')
    • write() method: file.write('Hello, World!')
  • CSV and JSON Files:
    • csv module: import csv
    • json module: import json

Web Development

  • Flask: Micro web framework.
    • Create app: from flask import Flask; app = Flask(__name__)
    • Define routes: @app.route('/')
  • Django: High-level web framework.
    • Create project: django-admin startproject myproject
    • Define models: from django.db import models
  • Templates: Separating presentation logic from application logic.
    • Template engines: Jinja2, Mustache
    • Template inheritance: extends keyword

Data Structures

  • Lists are ordered collections of items that can be modified and contain duplicate values.
  • Lists can be created using square brackets [] and elements can be accessed using indexing.
  • Slicing allows retrieving a subset of a list using list[start:stop].

Tuples

  • Tuples are ordered, immutable collections of items.
  • Tuples can be created using parentheses () and elements can be accessed using indexing.

Dictionaries

  • Dictionaries are unordered collections of key-value pairs.
  • Dictionaries can be created using curly brackets {} and values can be accessed using keys.
  • Keys must be unique and immutable.

Sets

  • Sets are unordered collections of unique items.
  • Sets can be created using curly brackets {} and can be manipulated using union, intersection, and difference operations.

Object-Oriented Programming

Classes

  • Classes are blueprints for creating objects.
  • Classes can be defined using the class keyword.
  • Classes can have attributes and methods.

Objects

  • Objects are instances of classes.
  • Objects can have attributes and methods.
  • Attributes can be accessed using dot notation.
  • Methods can be called using dot notation.

Inheritance

  • Inheritance is a mechanism for creating a new class from an existing class.
  • Single inheritance allows a class to inherit from one parent class.
  • Multiple inheritance allows a class to inherit from multiple parent classes.

Exception Handling

Try-Except Blocks

  • Try-except blocks are used to catch and handle exceptions.
  • The try block contains code that may raise an exception.
  • The except block contains code that handles the exception.

Raising Exceptions

  • Exceptions can be manually raised using the raise keyword.
  • Exceptions can be raised with a custom error message.

Custom Exceptions

  • Custom exceptions can be defined by creating a new class that inherits from the Exception class.
  • Custom exceptions can be raised using the raise keyword.

File Input/Output

Reading Files

  • Files can be read using the open() function in read mode ('r').
  • The read() method can be used to read the contents of a file.

Writing Files

  • Files can be written using the open() function in write mode ('w').
  • The write() method can be used to write to a file.

CSV and JSON Files

  • The csv module can be used to read and write CSV files.
  • The json module can be used to read and write JSON files.

Web Development

Flask

  • Flask is a micro web framework.
  • A Flask app can be created using the Flask class.
  • Routes can be defined using the @app.route() decorator.

Django

  • Django is a high-level web framework.
  • A Django project can be created using the django-admin command.
  • Models can be defined using the models module.

Templates

  • Templates are used to separate presentation logic from application logic.
  • Template engines such as Jinja2 and Mustache can be used to render templates.
  • Template inheritance can be used to extend templates using the extends keyword.

This quiz covers the basics of data structures in Python, including lists, tuples, and dictionaries. Learn how to create, index, and manipulate these data structures.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser