Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Dictionary
- favourite_languages = {
- 'jen': 'python',
- 'sarah': 'c',
- 'edward': 'ruby',
- 'phil': 'python',
- }
- # Printing keys and values
- print("=== Ключи и значения: ===")
- for key, value in favourite_languages.items():
- print(key + ": " + value)
- # Printing only keys
- print("\n=== Ключи ===")
- for key in favourite_languages.keys():
- print(key)
- # Printing only values
- print("\n=== Значения: ===")
- for value in favourite_languages.values():
- print(value)
- # Using set() function for unqiue values only
- print("\n=== Вывод только уникальных значений: ===")
- for value in set(favourite_languages.values()):
- print(value)
- # Creating new alien dictionary for example of using
- # dictionaries in games
- alien_0 = {'color': 'green', 'points': 5}
- # Adding new key-value pairs to alien with positions
- print("\n=== Добавление новой пары значения и ключа ===")
- print(alien_0)
- alien_0['x_pos'] = 0
- alien_0['y_pos'] = 25
- print(alien_0)
- # Creating empty dictionary
- alien_1 = {}
- # Editing value in dictionary
- print("\n=== Изменения значения ===")
- alien_2 = {'color': 'green'}
- print("The alien is " + alien_0['color'] + ".")
- alien_2['color'] = 'yellow'
- print("The alien is now " + alien_0['color'] + ".")
- # Moving alien
- print("\n=== Перемещение пришельца ===")
- alien_0 = {'x_pos': 0, 'y_pos': 25, 'speed': 'medium'}
- print("Original x-position: " + str(alien_0['x_pos']))
- # Пришелец перемещается вправо.
- # Вычисляем величину смещения на основании текущей скорости.
- if alien_0['speed'] == 'slow':
- x_increment = 1
- elif alien_0['speed'] == 'medium':
- x_increment = 2
- else:
- # Пришелец двигается быстро.
- x_increment = 3
- # Новая позиция равна сумме старой позиции и приращения.
- alien_0['x_pos'] = alien_0['x_pos'] + x_increment
- print("New x-position: " + str(alien_0['x_pos']))
- # Deleting key-value pair
- print("\n=== Удаление пары ключа и значения")
- alien_0 = {'color': 'green', 'points': 5}
- print(alien_0)
- del alien_0['points']
- print(alien_0)
- # List of dictionaries
- print("\n=== Список словарей ===")
- alien_0 = {'color': 'green', 'points': 5}
- alien_1 = {'color': 'yellow', 'points': 10}
- alien_2 = {'color': 'red', 'points': 15}
- aliens = [alien_0, alien_1, alien_2]
- for alien in aliens:
- print(alien)
Advertisement
Add Comment
Please, Sign In to add comment