1-1: Create a tuple with the four elements "a," 3, 6, and [1, 2]:
In [6]:
Copied!
my_tuple=('a',3,6,[1,2])
my_tuple=('a',3,6,[1,2])
1-2: Assign the hourly wages of Sam, Ryan, and Eli to a dictionary called wage and then get the keys, values, and items of the dictionary.
In [1]:
Copied!
# Assign hourly wages to a dictionary
wage = {'Sam': 15, 'Ryan': 25, 'Eli': 30}
# Get keys (names), values (wages), and items (key-value pairs)
names = wage.keys()
wages = wage.values()
items = wage.items()
# Print the results
print("Names:", names)
print("Wages:", wages)
print("Items:", items)
# Assign hourly wages to a dictionary wage = {'Sam': 15, 'Ryan': 25, 'Eli': 30} # Get keys (names), values (wages), and items (key-value pairs) names = wage.keys() wages = wage.values() items = wage.items() # Print the results print("Names:", names) print("Wages:", wages) print("Items:", items)
Names: dict_keys(['Sam', 'Ryan', 'Eli'])
Wages: dict_values([15, 25, 30])
Items: dict_items([('Sam', 15), ('Ryan', 25), ('Eli', 30)])
1-3: Change the salary of Sam to 16, and add new person, john, 22, and delete Ryan.
In [8]:
Copied!
wage['sam']=16
wage['john']=22
del wage['Ryan']
wage['sam']=16 wage['john']=22 del wage['Ryan']
1-4: Write a loop to iterates through the numbers from 1 to 5 and prints them, but it skips the number 4
In [9]:
Copied!
count = 1
while count < 6:
if count == 4:
count = count + 1
continue
print(count)
count = count + 1
count = 1 while count < 6: if count == 4: count = count + 1 continue print(count) count = count + 1
1 2 3 5
1-5: Write a function to check if the number is even or not.
In [11]:
Copied!
def is_even (n):
if n % 2 == 0:
return print('it is even')
return print('it is odd')
def is_even (n): if n % 2 == 0: return print('it is even') return print('it is odd')
it is even
In [12]:
Copied!
is_even (10)
is_even (10)
it is even
1-6: Write a comprehension; get a list and drop 1st, and 3th obs.
In [ ]:
Copied!
obs=range(1,10)
[x for (i,x) in enumerate(obs) if i not in (0,2)]
obs=range(1,10) [x for (i,x) in enumerate(obs) if i not in (0,2)]
1-7: Write a comprehension that select number that are divisible by 3 and 5 between 1 and 100
In [ ]:
Copied!
obs=range(1,101)
[x for x in obs if x%3==0 and x%5==0]
obs=range(1,101) [x for x in obs if x%3==0 and x%5==0]
1-8: generate even numbers list in range 0 to 15
In [ ]:
Copied!
[i for i in range(15) if i % 2 == 0]
[i for i in range(15) if i % 2 == 0]