Lernzettel: Python Data Handling and String Formatting

📋 Course Outline

  1. Dictionary Comparison
  2. String Formatting Output
  3. File Handling in Python
  4. Python Data Types
  5. Python Operators
  6. Python Functions
  7. Control Structures
  8. Python Data Structures
  9. Python String Operations
  10. Python Modules and Variables

📖 1. Dictionary Comparison

🔑 Key Concepts & Definitions

  • cmp (function to compare dictionaries): A built-in function in Python (up to Python 2) that compares two dictionaries and returns an integer indicating their relative order or equality. It helps determine whether dictionaries are equal or which one is greater based on their contents. (Note: In Python 3, cmp() is removed, and comparison is done via operators.)

  • keys() (dictionary method to get all keys): A method that returns a view object containing all the keys in a dictionary. It allows iteration over keys and is useful for checking key existence or retrieving all keys for processing. Example: dict.keys().

  • dict() (dictionary creation from sequence of tuples): A constructor that creates a dictionary from a sequence (like a list) of key-value pairs represented as tuples. It converts the sequence into a dictionary with keys mapped to their respective values. Example: dict([('a', 1), ('b', 2)]).

  • KeyError (exception when accessing non-existent key): An error raised when attempting to access a dictionary key that does not exist. It indicates that the key is missing from the dictionary, and handling it often involves try-except blocks. Example: d['missing_key'] when 'missing_key' is not in d.

📝 Essential Points

  • The cmp() function compares two dictionaries based on their items, returning 0 if they are equal, a negative number if the first is less, or a positive number if the first is greater. (Note: cmp() is deprecated in Python 3; comparison should be done using ==, <, >, etc.).

  • The keys() method provides a view object that reflects the current keys in the dictionary, enabling iteration and key existence checks efficiently.

  • The dict() constructor can create dictionaries from sequences of tuples, making it easy to initialize dictionaries from data structures like lists or sets of pairs.

  • Accessing a key that does not exist in a dictionary raises a KeyError exception, which can be caught using try-except to prevent runtime errors.

  • Comparing dictionaries for equality (using ==) checks whether they have the same key-value pairs, regardless of order.

💡 Key Takeaway

Understanding how to compare dictionaries, retrieve all keys, create dictionaries from sequences, and handle missing keys with exceptions is essential for effective dictionary manipulation in Python.

📖 2. String Formatting Output

🔑 Key Concepts & Definitions

string formatting with format() method
A method in Python that allows inserting variables into a string by using placeholders {}. It enhances readability and flexibility compared to traditional concatenation. AUTHOR (date): "The format() method provides a way to format strings dynamically, replacing placeholders with specified values."

format specifiers !r and !s
Special syntax used within format placeholders to control how objects are converted to strings:

  • !r calls repr() on the object, displaying a detailed, unambiguous string suitable for debugging.
  • !s calls str() on the object, providing a user-friendly string. AUTHOR (date): "!r and !s are format specifiers that determine the conversion method used for the object during string formatting."

string repetition operator *
An operator that duplicates a string a specified number of times. For example, 'abc' * 3 results in 'abcabcabc'. It is useful for creating repeated patterns or padding. AUTHOR (date): "The * operator enables string duplication, making it easy to generate repeated sequences."

string slicing syntax and output
A technique to extract a substring from a string using [start:stop:step]. It returns a new string containing characters from start to stop-1, stepping through the string with step. For example, 'hello'[1:4] yields 'ell'. AUTHOR (date): "String slicing allows selective extraction of parts of a string based on specified indices and step values."

string count() method
A method that counts the number of occurrences of a substring within a string. Syntax: string.count(substring). For example, 'banana'.count('a') returns 3. AUTHOR (date): "The count() method provides a straightforward way to tally how many times a substring appears in a string."

string replace() method
A method that returns a new string with all occurrences of a specified substring replaced by another. Syntax: string.replace(old, new[, count]). For example, 'hello world'.replace('world', 'Python') results in 'hello Python'. AUTHOR (date): "The replace() method facilitates string modification by substituting specified substrings."

📝 Essential Points

  • The format() method improves string output by allowing dynamic insertion of variables, with placeholders {} that can include format specifiers like !r and !s for controlling object representation.
  • !r uses repr(), showing detailed object info, while !s uses str(), providing a user-friendly display.
  • String repetition with * is a simple way to duplicate strings, useful for creating patterns or padding.
  • String slicing [start:stop:step] extracts substrings based on indices and step size, supporting flexible string manipulation.
  • The count() method counts how many times a substring appears within a string, aiding in text analysis.
  • The replace() method substitutes all or specified occurrences of a substring with another, enabling string modification.

💡 Key Takeaway

String formatting with format() and format specifiers !r and !s provides flexible, readable ways to embed and display data. String repetition, slicing, counting, and replacing are essential tools for efficient string manipulation in Python.

📖 3. File Handling in Python

🔑 Key Concepts & Definitions

  • open(file_path, mode) (SOURCE: CSC 201, 2020/2021): A built-in Python function used to open a file specified by file_path with a specific mode. The mode determines the operations permitted, such as reading or writing.
  • File read(n) (SOURCE: CSC 201, 2020/2021): A method that reads n characters from an open file object. If n is omitted, it reads the entire file content.
  • readline() (SOURCE: CSC 201, 2020/2021): Reads a single line from an open file object, returning it as a string. It moves the file pointer to the next line for subsequent reads.
  • try-except-else (SOURCE: CSC 201, 2020/2021): A control flow structure used in file handling to manage exceptions. The try block contains code that may raise an exception, except handles errors, and else executes if no exception occurs.
  • File modes for reading files (SOURCE: CSC 201, 2020/2021): String parameters like 'r' for read, 'rb' for read binary, which specify the type of access when opening a file. 'r' is the most common mode for reading text files.

📝 Essential Points

  • The open() function requires the file path and mode; for reading, the mode 'r' is used. For example, open("file.txt", "r").
  • When reading files, read(n) extracts n characters, useful for partial reads, while readline() reads line-by-line, suitable for processing large files or line-based data.
  • Proper file handling involves closing files after operations using close(), or better, using with statement for automatic resource management.
  • The try-except-else structure ensures robust file operations: if an error occurs during file access, the except block handles it, preventing crashes. The else block executes only if no exception is raised, confirming successful file processing.
  • File modes like 'r', 'w', 'a', 'b' determine whether the file is opened for reading, writing, appending, or in binary mode, respectively. For reading, 'r' is standard.

💡 Key Takeaway

File handling in Python relies on the open() function with specific modes, combined with read methods like read(n) and readline(), and robust control flow structures such as try-except-else to ensure error-free file operations.

📖 4. Python Data Types

🔑 Key Concepts & Definitions

  • Integer: A basic data type representing whole numbers without a fractional part. Example: 42. Python integers are of arbitrary size, limited only by available memory.

  • Float: A data type for representing real numbers with a decimal point. Example: 3.14. Python floats are implemented using double-precision floating-point format.

  • String: An ordered sequence of characters enclosed in quotes (' or "). Example: "Hello". Strings are immutable in Python, meaning their content cannot be changed after creation.

  • List: A mutable ordered collection of items, which can be of different data types. Syntax: [item1, item2, item3]. Lists support indexing, slicing, and dynamic modification.

  • Tuple: An immutable ordered collection of items, defined using parentheses (item1, item2, item3). Tuples are used for fixed collections and support indexing and slicing but cannot be altered after creation.

  • Dictionary: A collection of key-value pairs, enclosed in curly braces {}. Example: {'name': 'Alice', 'age': 25}. Keys are unique and immutable, values can be of any data type.

  • Python Keywords & Case Sensitivity: Keywords are reserved words with special meaning in Python, such as def, if, else. Python is case-sensitive, so Variable and variable are considered different identifiers.

  • Dynamic Typing of Variables: Python variables are dynamically typed, meaning a variable can be assigned to different data types during execution without explicit declaration. Example: x = 10 (integer), then x = "text" (string).

  • Type Conversion Functions (e.g., str()): Built-in functions to convert data types. For example, str(100) converts the integer 100 to the string "100", enabling concatenation or formatted output.

📝 Essential Points

  • Python's basic data types include integers, floats, strings, lists, tuples, and dictionaries, each with specific syntax and characteristics.
  • Integers and floats are used for numerical computations; integers are whole numbers, floats handle real numbers.
  • Strings are immutable sequences of characters, supporting concatenation (+), repetition (*), and slicing.
  • Lists are mutable, allowing addition, removal, and modification of elements; tuples are immutable, providing fixed collections.
  • Dictionaries store data as key-value pairs, enabling fast lookups based on unique keys.
  • Python keywords are case-sensitive and cannot be used as identifiers.
  • Variables in Python do not require explicit declaration; their types are inferred at runtime, demonstrating dynamic typing.
  • Type conversion functions, such as str(), facilitate changing data types to suit different operations, especially for output formatting.

💡 Key Takeaway

Python offers a rich set of data types with distinct syntax and behaviors, supporting flexible and dynamic programming. Understanding these types and their characteristics is fundamental to effective Python programming.

📖 5. Python Operators

🔑 Key Concepts & Definitions

  • Arithmetic Operators (supported in Python): Symbols used to perform basic mathematical operations. These include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), and floor division (//). (Source: CSC 202, 2020/2021)

  • Unsupported Operators in Python: Operators not recognized by Python syntax, such as the backslash ($, which is used for escape sequences but not as an arithmetic operator. (Source: CSC 202, 2020/2021)

  • Relational Operators (supported in Python): Symbols used to compare values, returning Boolean results. These include greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), not equal to (!=), and equal to (==). (Source: CSC 202, 2020/2021)

  • Operator Precedence Rules: The hierarchy determining the order in which operators are evaluated in expressions. In Python, multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). Exponentiation (**) has higher precedence than multiplication/division, and floor division (//) is evaluated after multiplication/division. Parentheses can override default precedence. (Source: CSC 202, 2020/2021)

  • Bitwise Operators: Operators that perform bit-level operations on integers. For example, the bitwise NOT (~) inverts all bits of a number. Other bitwise operators include AND (&), OR (|), XOR (^), left shift (<<), and right shift (>>). (Source: CSC 202, 2020/2021)

📝 Essential Points

  • Python supports a set of arithmetic operators that facilitate mathematical calculations, with +, -, *, /, %, **, and // being supported. The ** operator is not supported for division; instead, it is used for escape sequences in strings. (Source: CSC 202, 2020/2021)

  • Unsupported operators like ** (used for escape sequences) are not valid for arithmetic operations in Python, emphasizing the importance of understanding operator syntax. (Source: CSC 202, 2020/2021)

  • Relational operators enable comparison between values, returning Boolean results, which are fundamental in control flow and decision-making. Python's relational operators include >, <, >=, <=, !=, and ==. (Source: CSC 202, 2020/2021)

  • Operator precedence determines the order of evaluation in complex expressions. For example, in the expression 3 + 4 * 2, multiplication is performed first, resulting in 3 + 8 = 11. Parentheses can be used to explicitly specify evaluation order. (Source: CSC 202, 2020/2021)

  • Bitwise operators manipulate individual bits of integer operands, with ~ performing bitwise NOT, which inverts all bits. These operators are essential in low-level programming and optimization tasks. (Source: CSC 202, 2020/2021)

💡 Key Takeaway

Python provides a comprehensive set of operators for performing arithmetic, comparison, and bitwise operations, with clear precedence rules that govern their evaluation order. Understanding supported and unsupported operators is crucial for writing correct and efficient code.

📖 6. Python Functions

🔑 Key Concepts & Definitions

Function definition using def keyword:
A way to create a reusable block of code in Python, initiated with the def keyword, followed by the function name and parentheses. (Source: "def" is used for defining functions in Python)

Function call syntax:
The process of executing a function by writing its name followed by parentheses, optionally including arguments inside the parentheses. Example: calculate(). (Source: "calculate()" as a typical function call)

Functions as reusable code blocks:
Functions encapsulate specific tasks, allowing code to be written once and used multiple times, promoting modularity and reducing redundancy. (Source: "Functions are reusable pieces of programs")

Default arguments in function definitions:
Parameters in a function can have default values, specified with an assignment in the function header, enabling optional arguments during calls. Example: def greet(name='Guest'). (Source: "default arguments in function definitions")

Variable length arguments (*args):
Special syntax allowing a function to accept an arbitrary number of positional arguments, collected into a tuple named args. Example: def sum_all(*args). *(Source: "functions with args")

Functions returning None by default:
If a function does not explicitly return a value using return, it returns None by default, indicating no specific output. (Source: "functions returning None by default")

📝 Essential Points

  • Functions are defined with the def keyword, followed by the function name and parentheses containing parameters.
  • Function calls are made by writing the function name followed by parentheses, including arguments if needed.
  • Functions promote code reusability, modularity, and clarity by encapsulating tasks into discrete blocks.
  • Default arguments allow functions to be called with fewer arguments, providing flexibility.
  • Variable length arguments (*args) enable functions to handle an arbitrary number of inputs, useful for aggregating data.
  • If a function lacks a return statement, it returns None by default, which can be used to indicate the absence of a result or side effects.
  • Proper use of functions enhances code maintainability and reduces errors, especially in larger programs.

💡 Key Takeaway

Functions in Python, defined with def, serve as reusable, modular code blocks that can accept default and variable-length arguments, and they return None by default if no explicit return statement is provided.

📖 7. Control Structures

🔑 Key Concepts & Definitions

  • if statement: A control structure that executes a block of code only if a specified condition evaluates to true. It allows decision-making in programs.
  • if-else statement: Extends the if statement by providing an alternative block of code to execute when the condition evaluates to false. It enables binary decision-making.
  • if-elif-else statement: A multi-way decision structure that tests multiple conditions sequentially. The first true condition's block executes, or the else block runs if none are true.
  • while loop: A control structure that repeatedly executes a block of code as long as a given condition remains true. It supports indefinite iteration.
  • for loop: A control structure that iterates over a sequence (like a list, tuple, or range), executing a block of code for each element. It supports definite iteration.
  • break statement: A statement used within loops to immediately exit the loop, regardless of the loop's condition. It is useful for terminating loops early, as demonstrated in the source content.

📝 Essential Points

  • Control structures like if, if-else, and if-elif-else facilitate decision-making by executing code blocks based on evaluated conditions. They are fundamental for controlling program flow.
  • The while loop continues execution as long as its condition remains true, supporting indefinite repetition. The for loop iterates over sequences, providing a concise way to traverse items.
  • The break statement is crucial for controlling loop execution, allowing an early exit when a certain condition is met, preventing unnecessary iterations.
  • The range() function generates sequences of numbers, which are commonly used in for loops. For example, range(1,10,1) produces numbers from 1 to 9 with a step of 1.
  • Invalid control structures, such as do (which is not supported in Python), highlight the importance of understanding language-specific syntax and control flow mechanisms.
  • The source content emphasizes the importance of proper indentation in Python to define blocks of code under control structures, as indentation indicates scope and flow.

💡 Key Takeaway

Control structures like if, while, and for are essential for directing program flow and decision-making, with the break statement providing additional control to exit loops prematurely. Proper use of these structures enables efficient and readable code.

📖 8. Python Data Structures

🔑 Key Concepts & Definitions

  • List: An ordered, mutable collection of items in Python, enclosed within square brackets [ ]. Lists can contain elements of different data types and support indexing, slicing, and dynamic modification.
    Example: my_list = [1, 'apple', 3.14]

  • Tuple: An ordered, immutable collection of items, enclosed within parentheses ( ). Once created, tuples cannot be altered, making them suitable for fixed data sets.
    Example: coordinates = (10, 20)

  • Set: An unordered collection of unique elements, enclosed within curly braces { }. Sets support mathematical operations like union, intersection, and difference, but do not allow duplicate elements.
    Example: unique_numbers = {1, 2, 3, 2} (results in {1, 2, 3})

  • Dictionary: A collection of key-value pairs, enclosed within curly braces { }. Keys are unique and immutable, while values can be of any data type. Dictionaries support fast lookup, insertion, and deletion based on keys.
    Example: student = {'name': 'Alice', 'age': 21}

  • List Length using len(): The len() function returns the number of items in a list, tuple, set, or dictionary. It is essential for determining the size of a data structure.
    Example: len([1, 2, 3]) returns 3.

  • Immutability of Tuples: Tuples are immutable, meaning their elements cannot be changed after creation. This property ensures data integrity and is useful for fixed collections.
    Note: Attempting to modify a tuple element raises a TypeError.

  • Invalid Data Structure Elements (e.g., List inside Set): Elements within a set must be immutable and hashable. Since lists are mutable and unhashable, they cannot be elements of a set.
    Example: set_with_list = { [1, 2, 3] } raises a TypeError.

📝 Essential Points

  • Lists are versatile and support dynamic modifications such as adding, removing, or changing elements, making them ideal for collections that change over time.
  • Tuples, being immutable, are suitable for fixed data, such as coordinates or configuration constants, and can be used as keys in dictionaries due to their hashability.
  • Sets automatically eliminate duplicate elements and support set operations, useful for membership testing and mathematical computations.
  • Dictionaries provide a fast way to associate unique keys with values, supporting efficient data retrieval.
  • The len() function is universally applicable across Python data structures to determine their size.
  • Elements in data structures must adhere to the mutability and hashability rules; for example, lists cannot be elements of sets because they are mutable and unhashable.

💡 Key Takeaway

Python offers a variety of data structures—lists, tuples, sets, and dictionaries—each with unique properties suited for different scenarios. Understanding their mutability, hashability, and appropriate usage is crucial for efficient programming and data management.

📖 9. Python String Operations

🔑 Key Concepts & Definitions

  • String Concatenation (+ operator):
    Combining two or more strings into a single string by using the plus (+) operator.
    Example: 'Hello' + ' ' + 'World' results in 'Hello World'.

  • String Slicing and Indexing:
    Accessing specific parts of a string using indices (position numbers) and slicing syntax [start:stop:step].
    Example: 'Python'[0:4] yields 'Pyth', and 'Python'[-1] accesses 'n'.

  • String Methods:

    • count(): Counts occurrences of a substring within a string.
      Example: 'banana'.count('a') returns 3.
    • replace(): Replaces all occurrences of a substring with another substring.
      Example: 'hello world'.replace('world', 'Python') results in 'hello Python'.
    • len(): Returns the length (number of characters) of a string.
      Example: len('Python') returns 6.
  • String Repetition ( operator):*
    Creates a new string by repeating the original string a specified number of times.
    Example: 'ha' * 3 results in 'hahaha'.

  • Escape Sequences:
    Special characters used within strings to represent whitespace or special formatting.

    • \n: New line.
    • \t: Tab space.
      Example: 'Hello\nWorld' prints as:
    Hello
    World
    

📝 Essential Points

  • String concatenation with + is fundamental for combining strings dynamically, especially in output formatting.
  • String slicing and indexing allow precise access to substrings, which is useful for parsing and data extraction.
  • String methods like count(), replace(), and len() facilitate string analysis and manipulation, making data processing more efficient.
  • String repetition using * operator simplifies creating repeated patterns or padding strings.
  • Escape sequences \n and \t are crucial for controlling string formatting, especially in multi-line outputs or tabular data.
  • These string operations are essential for handling textual data in Python, as highlighted in source content (see section 2).

💡 Key Takeaway

Mastering string concatenation, slicing, and methods enables effective manipulation and formatting of text data in Python, which is vital for programming tasks involving user input, output display, and data parsing.

📖 10. Python Modules and Variables

🔑 Key Concepts & Definitions

  • Python modules as reusable software units: A module in Python is a file containing Python definitions and statements. Modules allow code to be organized into separate files, making functions, classes, and variables reusable across different programs (Python Software Foundation, n.d.).

  • Python variables and naming rules: Variables in Python are identifiers used to store data. They must start with a letter or underscore, followed by letters, digits, or underscores. Python is case-sensitive, meaning Variable and variable are different identifiers (Python Documentation, 2020).

  • Python built-in functions: Predefined functions provided by Python that perform common tasks. Examples include round() (rounds a number to a specified number of decimal places), pow() (computes power), and len() (returns the length of an object). These functions simplify programming tasks (Python Standard Library, 2020).

  • Python language features: Python is an interpreted language, meaning code is executed line-by-line without prior compilation. It is high-level, emphasizing readability and ease of use. Python is case-sensitive and uses specific syntax for comments: # for single-line comments and triple quotes (''' or """) for multiline comments (Guido van Rossum, 1991).

📝 Essential Points

  • Python modules promote code reuse by enabling developers to import and utilize functions, classes, and variables defined in other files, thus avoiding code duplication and enhancing modularity (Python Software Foundation, n.d.).

  • Variable naming rules are strict; identifiers cannot start with digits and are case-sensitive. Proper naming conventions improve code readability and maintainability (Python Documentation, 2020).

  • Built-in functions like round() and pow() are integral for mathematical operations, reducing the need to write custom code for common tasks. They are globally available in Python without importing additional modules.

  • Python's interpreted nature allows rapid development and testing, but it also requires understanding its syntax for comments and case sensitivity to write correct code. Comments are essential for documentation and are written with # for single lines and triple quotes for multiline comments.

💡 Key Takeaway

Python's modular design, combined with its case-sensitive variables, powerful built-in functions, and clear syntax for comments, makes it a flexible and efficient language for developing reusable and readable software.

📊 Synthesis Tables

AspectPython 2Python 3Key Authors/References
Dictionary Comparisoncmp(dict1, dict2) returns integer; compares itemscmp() removed; use ==, <, >, etc.Python documentation, "Comparison of dictionaries"
Accessing Keysdict.keys() returns list (Python 2)dict.keys() returns view object (Python 3)Python docs, "dict_keys"
Creating Dictionariesdict([('a', 1), ('b', 2)])Same in Python 3Python docs
KeyError HandlingTry-except block for missing keysSame in Python 3Python docs
AspectString FormattingString FormattingKey Authors/References
format() methodIntroduced in Python 2.6, 3.0Same"Python String Formatting" (PEP 3107)
Format specifiers !r, !sSame in Python 2 and 3SamePython docs
String repetition operator *Same in Python 2 and 3SamePython docs
String slicing [start:stop:step]Same in Python 2 and 3SamePython docs
count() and replace() methodsSame in Python 2 and 3SamePython docs
AspectFile HandlingFile HandlingKey Authors/References
open() functionSame in Python 2 and 3SamePython docs
read(n) methodSame in Python 2 and 3SamePython docs
readline() methodSame in Python 2 and 3SamePython docs
Exception handling (try-except-else)Same in Python 2 and 3SamePython docs
File modes ('r', 'w', 'b')Same in Python 2 and 3SamePython docs

⚠️ Common Pitfalls & Confusions

  1. Using cmp() in Python 3, where it has been removed; instead, use ==, <, >, etc.
  2. Assuming dict.keys() returns a list in Python 3; it returns a view object, which may need conversion (list(dict.keys())).
  3. Forgetting to close files explicitly or not using with statement, leading to resource leaks.
  4. Confusing !r (repr) and !s (str) in string formatting; using the wrong specifier for debugging vs. user display.
  5. Misunderstanding string slicing: forgetting that [start:stop:step] excludes stop, or misusing negative indices.
  6. Overlooking that read(n) reads only n characters, which can cause incomplete data processing.
  7. Assuming dict() constructor accepts only list of tuples; it can also accept other mappings.
  8. Using string repetition * with non-string types, leading to errors.
  9. Not handling KeyError exceptions when accessing dictionary keys directly.
  10. Using readline() without a loop, leading to only one line read instead of entire file processing.

✅ Exam Checklist

  • Know the purpose and usage of the cmp() function in Python 2, and why it is deprecated in Python 3; understand dictionary comparison using ==, <, >.
  • Be able to retrieve all dictionary keys using keys() and understand the difference between Python 2 and 3 views.
  • Understand how to create dictionaries from sequences of tuples with dict().
  • Know how to handle KeyError exceptions when accessing non-existent dictionary keys, and the importance of try-except blocks.
  • Master string formatting with format(), including the use of {} placeholders and format specifiers !r and !s.
  • Be familiar with string repetition using *, slicing [start:stop:step], and string methods count() and replace().
  • Understand the syntax and modes of open() for file handling, especially 'r' mode for reading.
  • Know how to read files with read(n) and readline(), and the importance of closing files or using with.
  • Be able to implement robust file handling with try-except-else blocks to manage exceptions.
  • Recall key authors and references such as Python documentation, PEP 3107 for string formatting, and standard library methods.
  • Understand differences in data types and operators, including string operations, and how functions and modules are imported and used.

Teste dein Wissen

Teste dein Wissen zu Python Data Handling and String Formatting mit 10 Multiple-Choice-Fragen mit detaillierten Korrekturen.

1. How do you create a Python dictionary from a list of key-value pair tuples?

2. What is the purpose of the 'cmp()' function in Python 2 when used with dictionaries?

Quiz machen →

Mit Karteikarten lernen

Merke dir die Schlüsselkonzepte von Python Data Handling and String Formatting mit 20 interaktiven Karteikarten.

Dictionary comparison — method?

Use `==` for equality; `cmp()` in Python 2.

keys() — returns?

A view object of all dictionary keys.

dict() — creates?

A dictionary from sequences of key-value pairs.

Karteikarten ansehen →

Similar courses

Erstelle deine eigenen Lernzettel

Importiere deinen Kurs und die KI erstellt in 30 Sekunden Lernzettel, Quizze und Karteikarten.

Lernzettel-Generator