לדלג לתוכן

1.3 מבני נתונים פתרון

עיצוב מחרוזות מתקדם

# Exercise: Use f-strings and string formatting to create a complex message.
name = "Alice"
age = 25
message = f"Hello, {name}! You are {age} years old."
print(message)

פצל אותי

my_string = "hello! my name is amit"
my_list = my_string.split(" ")
print(my_list)

my_string = " ".join(my_list)
print(my_string)

מיון רשימות ומניפולציה

# Exercise: Sort a list of words alphabetically and print the result.
word_list = ["banana", "apple", "orange", "grape"]
sorted_words = sorted(word_list)
print(sorted_words)

# Exercise: Remove duplicates from a list using a set and print the unique elements.
numbers_with_duplicates = [1, 2, 3, 1, 2, 4, 5]
unique_numbers = list(set(numbers_with_duplicates))
print(unique_numbers)

מניפולציה על מילון

# Exercise: Print the keys and values of a dictionary separately.
grades = {'Math': 'A', 'English': 'B', 'Science': 'A+'}
keys = grades.keys()
values = grades.values()
print("Keys:", keys)
print("Values:", values)

חיתוך מתקדם

# Exercise: Use step-based slicing to reverse a tuple.
original_tuple = (1, 2, 3, 4, 5)
reversed_tuple = original_tuple[::-1]
print("Reversed Tuple:", reversed_tuple)

יום בשבוע

days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

day_number = int(input("Enter the number of the day: "))
day = days[day_number-1]

print(f"Today is {day}!")

- הסיבה שאנחנו הורדנו את day_number באחד, זה בגלל שכאשר אנחנו נגשים לרשימה עם מספרים, המקום תמיד מתחיל ב0, כך שאם אנחנו רוצים לגשת לאיבר הריבעי אנחנו צריכים לגשת ל days[3] לא לdays[4] זכור את זה!