This Python code demonstrates basic conditional statements (if
, elif
, else
) and introduces a newer pattern-matching approach (match-case
) for handling specific conditions, often preferred in Python 3.10 and later.
Checking If a Number Is Positive, Negative, or Zero:
a = 5
if a > 0:
print('The number is positive')
elif a < 0:
print('The number is negative')
else:
print('the number is zero')
Explanation:
- This block of code checks whether the variable
a
is positive, negative, or zero usingif
,elif
, andelse
statements. - The
if
condition checks if the number is greater than 0. If true, it prints"The number is positive"
. - If the number isn't positive, the
elif
condition checks if it's negative. If true, it prints"The number is negative"
. - If neither of the two conditions is true, the
else
statement is executed, printing"The number is zero"
.
Checking If It’s Raining:
is_raining = False
if is_raining:
print('Go out with an umbrella')
else:
print('Go out freely it is a shinny day!')
Explanation:
- This section checks the Boolean value
is_raining
. - If
is_raining
isTrue
, it advises going out with an umbrella. - If
is_raining
isFalse
, it prints a message suggesting going out freely because it’s a sunny day.
Weather Conditions Using if-elif-else
:
weather = input('What is the weather today? ').lower()
if weather == 'rainy':
print('Go with an unbrella or a raincoat')
elif weather == 'cloudy':
print('It may rain and consider a raincoat')
elif weather == 'snowy':
print('It may be slippery')
elif weather == 'foggy':
print('Visiblity might be hindered')
elif weather == 'sunny':
print('It is a great day to go to the beach')
else:
print('No one knows about the weather')
print('test it')
Explanation:
- This commented-out section (enclosed in triple quotes) takes the user's input for the weather and performs multiple checks using
if-elif-else
. - Based on the weather condition provided, it prints appropriate advice for going outside.
Weather Conditions Using match-case
:
weather = input('What is the weather today? ').lower()
match weather:
case 'rainy':
print('Go with an unbrella or a raincoat')
case 'cloudy':
print('It may rain and consider a raincoat')
case 'snowy':
print('It may be slippery')
case 'foggy':
print('Visiblity might be hindered')
case 'sunny':
print('It is a great day to go to the beach')
case _:
print('No one knows about the weather')
Explanation:
- This block uses the
match-case
structure, introduced in Python 3.10, which provides a more efficient and readable way to handle multiple conditions, replacing a chain ofif-elif-else
statements. - The
match
keyword checks the value of theweather
variable and matches it against specific cases ('rainy'
,'cloudy'
, etc.). - If no match is found, the default case (
case _
) is executed, printing"No one knows about the weather"
.
Key Takeaways:
- Conditional Statements (
if-elif-else
): Useful for checking multiple conditions and executing different blocks of code based on those conditions. - Pattern Matching (
match-case
): A more modern and efficient way to handle conditions that involve multiple specific cases, improving readability, especially for many branches.
- A string is a sequence of characters enclosed within single, double, or triple quotes.
- This documentation illustrates various string operations, slicing, methods, and formatting using Python.
Example: Basic String Declaration and Operations:
letter = 'a'
print(type(letter), len(letter)) # Output: <class 'str'> 1
alphabets = 'abcdefghijklmnopqrstuvwxyz'
print(alphabets, len(alphabets)) # Output: 'abcdefghijklmnopqrstuvwxyz', 26
print(list(alphabets)) # Converts the string into a list of characters
- Python strings support indexing, allowing access to individual characters by their position.
lang = 'Python'
print(lang[0]) # Output: 'P'
print(lang[-1]) # Output: 'n'
- Slicing allows for extracting a portion of the string.
print(lang[0:2]) # Output: 'Py' (slices from index 0 to 2, not including index 2)
print(lang[-4:-1]) # Output: 'tho' (slices from index -4 to -1)
Python provides a variety of string methods for common tasks:
- String Methods List:
'capitalize'
,'upper'
,'lower'
,'title'
,'strip'
,'replace'
,'find'
,'split'
,'join'
,'startswith'
,'endswith'
etc.
txt = 'Python for everyone'
print(txt.upper()) # Output: 'PYTHON FOR EVERYONE'
print(txt.lower()) # Output: 'python for everyone'
print(txt.capitalize()) # Output: 'Python for everyone'
- Python provides several ways to format strings:
-
Using
+
operator:full_name = first_name + ' ' + last_name print(full_name) # Concatenates strings
-
Using
format()
:print('I am {} {}. I am {} years old.'.format(first_name, last_name, age))
-
Using f-strings:
formated_string = f'I am {first_name} {last_name}. I teach {language}.' print(formated_string)
Working with DNA Sequence (String Operations):
- Count the occurrences of specific characters in a DNA string and calculate their frequency.
dna = '''CTAGCAAACTGCTGAT...''' # (trimmed for brevity)
total = len(dna)
a = dna.count('A')
c = dna.count('C')
t = dna.count('T')
g = dna.count('G')
print(a / total, c / total, t / total, g / total) # Output: Frequencies of A, C, T, G
Additional Examples:
-
String Replacement:
print('I love people'.replace('love', 'like')) # Output: 'I like people'
-
String Searching:
txt = 'Python for everyone' print(txt.find('y')) # Output: 1 (position of first 'y') print(txt.index('P')) # Output: 0
- A sample speech from Donald Trump is processed by splitting it into words, converting them to lowercase, and removing punctuation.
- Then, unique words are identified using a set.
donald_speech = '''Chief Justice Roberts, President...''' # (trimmed for brevity)
words = donald_speech.lower().replace('–', ' ').replace('.', ' ').split()
unique_words = set(words)
print(len(unique_words)) # Output: Count of unique words
This document covers the basics of string manipulation in Python, including indexing, slicing, methods, and formatting techniques. These concepts are essential for processing text data efficiently in Python.
This document explains basic operations and methods related to lists in Python, including list creation, accessing elements, and using built-in list methods.
- A list is a collection of items, which are indexed and ordered.
- Lists are mutable, meaning the elements can be changed after the list is created.
You can create an empty list using the list()
constructor.
empty_list = list()
print(len(empty_list), empty_list) # Outputs the length and the empty list
print(type(empty_list)) # Shows the type as <class 'list'>
print(dir(empty_list)) # Lists all methods available for the list object
A list of some of the most common list methods in Python:
lst_methods = ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
A list of numbers is created. You can access elements using their index.
nums = [1, 2, 3, 4, 5]
print(nums, len(nums)) # Prints the list and its length
print(nums[0]) # Accesses the first element
print(nums[1]) # Accesses the second element
print(nums[4]) # Accesses the last element using its positive index
# Accessing the last element using negative indexing
print(nums[-1])
# Slicing the list
print(nums[1:4]) # Prints elements from index 1 to 3
print(nums[2:]) # Prints from index 2 to the end
print(nums[:3]) # Prints elements from the start to index 2
You can modify lists by adding, inserting, or removing elements.
- append(): Adds an item to the end of the list.
- extend(): Adds multiple items to the list.
# nums.append(6)
# nums.extend([7, 8, 9, 10])
- pop(): Removes and returns an element at the given index. If no index is provided, it removes the last element.
- del: Deletes an element or a slice of the list.
# nums.pop() # Removes the last element
# nums.pop(0) # Removes the first element
# del nums[4] # Deletes the element at index 4
- insert(): Inserts an item at a given position.
nums.insert(3, 333) # Inserts 333 at index 3
nums.insert(6, 'the last item') # Inserts a string at index 6
- count(): Returns the count of occurrences of a value in the list.
- reverse(): Reverses the order of the list.
countries = ['Finland', 'Finland', 'Finland', 'Denmark', 'Norway', 'Finland', 'Finland', 'Sweden']
print(countries.count('Finland')) # Counts how many times 'Finland' appears
copy_lst = nums.copy() # Copies the list
copy_lst.reverse() # Reverses the copied list
print(copy_lst)
Lists can be sorted in ascending or descending order.
- sort(): Sorts the list in place.
- sorted(): Returns a sorted copy of the list.
new_num = [25, 20, 10, 3, 5, 0, 24]
print(sorted(new_num)) # Returns a sorted list in ascending order
print(sorted(new_num, reverse=False)) # Sorts without reversing
This document covered the following list operations:
- Creating and accessing elements in lists.
- Adding, inserting, and removing elements.
- Counting and reversing elements.
- Sorting lists using
sorted()
andsort()
.