0% found this document useful (0 votes)
5 views7 pages

Résumer Infor

The document provides an overview of fundamental programming concepts in Python, covering strings, lists, tuples, functions, and error handling. It explains string manipulation, list operations, tuple characteristics, function definitions and arguments, as well as exception handling techniques. Key differences between data structures and their use cases are also highlighted.

Uploaded by

zinabhibat7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Résumer Infor

The document provides an overview of fundamental programming concepts in Python, covering strings, lists, tuples, functions, and error handling. It explains string manipulation, list operations, tuple characteristics, function definitions and arguments, as well as exception handling techniques. Key differences between data structures and their use cases are also highlighted.

Uploaded by

zinabhibat7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter I

1. String Fundamentals :
A string is a sequence of characters used to store text, enclosed in either single (' ') or
double (" ") quotes.
Content: Strings can include letters, numbers, spaces, and symbols.
Length: Use the len() function to find the total number of characters.
Concatenation: Join strings together using the + operator.
Repetition: Repeat a string multiple times using the * operator.
2. Accessing and Slicing Strings:
Python uses a positioning system called indexing to locate characters within a string.
Indexing :
 Positive Indexing: Starts at 0 for the first character.
 Negative Indexing: Starts at -1 for the last character, moving backward.
Slicing:
Slicing allows you to extract a specific part of a string.
 Syntax: string[start:stop:step].
 Rules: The start index is included, but the stop index is excluded.
 Reverse: To reverse a string entirely, use the slice [::-1].
3. String Methods & Formatting :
Methods are built-in tools to manipulate text without changing the original string
(since strings are immutable).
Changing Case:
 .upper(): Converts all letters to uppercase.
 .lower(): Converts all letters to lowercase.
 .capitalize(): Capitalizes only the first letter.
 Cleaning: Use .strip(), .lstrip(), or .rstrip() to remove unwanted whitespace.
 Replacing: .replace("old", "new") swaps specific parts of the text.
 Counting: .count("x") finds how many times a character appears.
 f-Strings: The cleanest way to insert variables into text using curly braces:
f"Hello {name}".
4. Looping Through Strings:
You can iterate through a string character by character using different loop types.
Loop Type Method Description
Direct for for char in text: Most common; accesses each character
directly.
Indexed for for i in range(len(text)): Uses the range() function to loop through
positions/indices.
while Loop while i < len(text): Requires a manual counter i that increments
each turn.
Reverse for char in reversed(text): Iterates from the last character to the first.
Loop
Chapter II
1. Introduction to Lists:
A list is an ordered and mutable (changeable) collection that can store multiple
values of different data types.
 Creation: Use square brackets [].
 Example: mixed = [1, "Python", 3.14, True].
 Accessing: Use an index starting at 0.
 Positive: fruits[0] (first item).
 Negative: fruits[-1] (last item).
 Modifying: You can change an element by its index: fruits[1] = "mango".
2. Adding & Removing Elements:
Python provides specific methods to manage list content:
Adding:
 .append(value): Adds an item to the end.
 .insert(index, value): Adds an item at a specific position.
Removing:
 .remove(value): Deletes the item by its name/value.
 del list[index]: Deletes the item at a specific position.
Counting: len(list) returns the total number of items.
3. List Slicing:
Slicing extracts a "slice" of a list without changing the original.
Syntax: list[start : end : step].
Key Rules:
The start is included, but the end is excluded.
 list[1:3] gets items at index 1 and 2.
 list[::-1] reverses the entire list.
 Bulk Update: You can replace multiple values at once using slicing: numbers[1:3] =
[99, 88].
4. Looping Through Lists:
Iterating means going through items one by one.
Method Syntax Use Case
Direct Loop for item in list: Simplest way to access every value.
range() Loop for i in range(len(list)): Best when you need the index number.
With if (num % 2 == 0): Used inside a loop to filter data (e.g.,
Conditions finding even numbers).

5. Introduction to Tuples:
Tuples are very similar to lists but with one critical difference: Immutability.
 Creation: Use parentheses () instead of square brackets.
 Immutable: Once created, you cannot change, add, or remove values.
 Example: t[0] = 5 will result in an Error.
List vs. Tuple Comparison :
Feature List Tuple
Mutable ✅ Yes ❌ No
Syntax [] ()
Speed Slower Faster
Methods Many Few
Use Case Data that changes Fixed data (e.g., coordinates)
Chapter III
A function is a named block of code designed to perform a specific task, which can be
reused multiple times to avoid repeating code and to organize programs into smaller,
readable parts.
1. Defining and Calling :
 Definition: Use the def keyword followed by the function name and parentheses ().
 Body: The instructions inside the function must be indented.
 Execution: To run the function, you must "call" it by writing its name followed by
parentheses: function_name()
2. Arguments (Parameters):
Functions can receive data to work with, known as arguments:
 Positional Arguments: Values passed in a specific order.
 Named Arguments: Passing values by specifying the parameter name (e.g.,
presentation(age=20, name="Alice")).
3. Return Values :
 Purpose: The return statement sends a result back to the caller and terminates the
function.
 Utility: You must use return if you want to store the result in a variable or use it for
further calculations outside the function.
 Example: Without return, a calculation like f = puis_x(1) + 1 would cause an error
because the function result is not captured.
4. Variable Scope :
 Local Variables: Created inside a function and exist only inside that function.
They cannot be accessed from the outside.
 Global Variables: Defined outside functions and can be accessed anywhere in the
code.
Chapter IV
Exceptions are errors that occur during execution and interrupt the program. Error
handling prevents these abrupt stops.
1. The Complete Try Block Structure:
 try: Contains the code to monitor for potential errors.
 except: Executes only if an error occurs. You can handle specific types or use a
general except (though defining the type is better).
 else: Runs only if no error occurred in the try block.
 finally: Runs always, regardless of whether an error happened or not, often used for
"End of program" messages.
2. Common Errors to Know :
 NameError: Occurs when using a variable or function that hasn't been defined.
 ZeroDivisionError: Occurs when attempting to divide a number by zero.
 ValueError: Occurs when a function receives a value of the correct type but
inappropriate content (e.g., passing text to int()).
 IndexError: Occurs when trying to access a position (index) that is out of the range
of a list or sequence.
 TypeError: Occurs when an operation is applied to an object of an inappropriate type
(e.g., adding a number to a string).
3. Practical Applications :
 Division Safety: Use except ZeroDivisionError and except ValueError to handle cases
where users enter 0 or non-numeric text during calculations.
 Input Validation: Wrapping input() in a try-except block ensures the program
handles invalid user entries gracefully.
1. Data Structures: Strings, Lists, and Tuples:
These are used to store data, but they behave differently.
Feature Strings (Chap 1) Lists (Chap 2) Tuples (Chap 2)
Definition Sequence of Ordered collection of Fixed collection of
characters. items. items.
Symbols Single or Double quotes Square brackets [ ] Parentheses ( )
''/""
Mutability Immutable (cannot Mutable (can change Immutable (cannot
change letters) items) change items)
Example s = "Hello" L = [10, 20, 30] T = (1, 2, 3)
Key [Link]() or [Link]() [Link](40) or Accessing: T[0]
Operation [Link](10)
The Difference Example:
 List: my_list = [1, 2]
my_list[0] = 9 → Result: [9, 2] (Works!)
 String: my_str = "Hi"
my_str[0] = "B" → Error! (You cannot modify it directly).

Comparison Key Difference


List vs. String Lists can be changed (L[0]=1), Strings cannot.
List vs. Tuple Use Lists for data that changes (shopping list); use Tuples
for data that stays the same (GPS coordinates).
for char in text vs. for i in The first gets the character directly; the second gets the
range(len(text)) position number (index).
except vs. finally except is the "Rescue" (only if failure); finally is the "Final
Step" (always happens).

You might also like