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.
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.
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.
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."
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.* is a simple way to duplicate strings, useful for creating patterns or padding.[start:stop:step] extracts substrings based on indices and step size, supporting flexible string manipulation.count() method counts how many times a substring appears within a string, aiding in text analysis.replace() method substitutes all or specified occurrences of a substring with another, enabling string modification.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.
file_path with a specific mode. The mode determines the operations permitted, such as reading or writing.n characters from an open file object. If n is omitted, it reads the entire file content.try block contains code that may raise an exception, except handles errors, and else executes if no exception occurs.'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.open() function requires the file path and mode; for reading, the mode 'r' is used. For example, open("file.txt", "r").read(n) extracts n characters, useful for partial reads, while readline() reads line-by-line, suitable for processing large files or line-based data.close(), or better, using with statement for automatic resource management.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.'r', 'w', 'a', 'b' determine whether the file is opened for reading, writing, appending, or in binary mode, respectively. For reading, 'r' is standard.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.
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.
+), repetition (*), and slicing.str(), facilitate changing data types to suit different operations, especially for output formatting.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.
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)
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)
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.
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")
def keyword, followed by the function name and parentheses containing parameters.*args) enable functions to handle an arbitrary number of inputs, useful for aggregating data.return statement, it returns None by default, which can be used to indicate the absence of a result or side effects.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.
range(1,10,1) produces numbers from 1 to 9 with a step of 1.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.
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.
len() function is universally applicable across Python data structures to determine their size.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.
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:
'banana'.count('a') returns 3.'hello world'.replace('world', 'Python') results in 'hello Python'.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.'Hello\nWorld' prints as:Hello
World
+ is fundamental for combining strings dynamically, especially in output formatting.count(), replace(), and len() facilitate string analysis and manipulation, making data processing more efficient.* operator simplifies creating repeated patterns or padding strings.\n and \t are crucial for controlling string formatting, especially in multi-line outputs or tabular data.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.
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).
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.
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.
| Aspect | Python 2 | Python 3 | Key Authors/References |
|---|---|---|---|
| Dictionary Comparison | cmp(dict1, dict2) returns integer; compares items | cmp() removed; use ==, <, >, etc. | Python documentation, "Comparison of dictionaries" |
| Accessing Keys | dict.keys() returns list (Python 2) | dict.keys() returns view object (Python 3) | Python docs, "dict_keys" |
| Creating Dictionaries | dict([('a', 1), ('b', 2)]) | Same in Python 3 | Python docs |
| KeyError Handling | Try-except block for missing keys | Same in Python 3 | Python docs |
| Aspect | String Formatting | String Formatting | Key Authors/References |
|---|---|---|---|
format() method | Introduced in Python 2.6, 3.0 | Same | "Python String Formatting" (PEP 3107) |
Format specifiers !r, !s | Same in Python 2 and 3 | Same | Python docs |
String repetition operator * | Same in Python 2 and 3 | Same | Python docs |
String slicing [start:stop:step] | Same in Python 2 and 3 | Same | Python docs |
count() and replace() methods | Same in Python 2 and 3 | Same | Python docs |
| Aspect | File Handling | File Handling | Key Authors/References |
|---|---|---|---|
open() function | Same in Python 2 and 3 | Same | Python docs |
read(n) method | Same in Python 2 and 3 | Same | Python docs |
readline() method | Same in Python 2 and 3 | Same | Python docs |
Exception handling (try-except-else) | Same in Python 2 and 3 | Same | Python docs |
File modes ('r', 'w', 'b') | Same in Python 2 and 3 | Same | Python docs |
cmp() in Python 3, where it has been removed; instead, use ==, <, >, etc.dict.keys() returns a list in Python 3; it returns a view object, which may need conversion (list(dict.keys())).with statement, leading to resource leaks.!r (repr) and !s (str) in string formatting; using the wrong specifier for debugging vs. user display.[start:stop:step] excludes stop, or misusing negative indices.read(n) reads only n characters, which can cause incomplete data processing.dict() constructor accepts only list of tuples; it can also accept other mappings.* with non-string types, leading to errors.KeyError exceptions when accessing dictionary keys directly.readline() without a loop, leading to only one line read instead of entire file processing.cmp() function in Python 2, and why it is deprecated in Python 3; understand dictionary comparison using ==, <, >.keys() and understand the difference between Python 2 and 3 views.dict().KeyError exceptions when accessing non-existent dictionary keys, and the importance of try-except blocks.format(), including the use of {} placeholders and format specifiers !r and !s.*, slicing [start:stop:step], and string methods count() and replace().open() for file handling, especially 'r' mode for reading.read(n) and readline(), and the importance of closing files or using with.try-except-else blocks to manage exceptions.Testez vos connaissances sur Python Data Handling and String Formatting avec 10 questions à choix multiples avec corrections détaillées.
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?
Mémorisez les concepts clés de Python Data Handling and String Formatting avec 20 flashcards interactives.
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.
Bases de données
Bases de données
Programmation
Programmation
Importe ton cours et l'IA génère fiches, QCM et flashcards en 30 secondes.
Générateur de fiches