First Python

Run Settings
LanguagePython
Language Version
Run Command
# import utility # print(utility.divide(2,3)) # some_list = ['a','b', 'c', 'b', 'd', 'm','n','n'] # duplicates = list(set([x for x in some_list if some_list.count(x)>1])) # print(duplicates) # #list, set dictionary # simple_dict = { # 'a':1, # 'b':2 # } # my_dict = {k:v**2 for k,v in simple_dict.items()} # my_dict1 = {num:num*2 for num in [1,2,3]} # print(my_dict1) # my_list = {char for char in 'hello'} # my_list2 = {num for num in range(0,100)} # my_list4 = {num**2 for num in range(0,100) if num**2 % 2 == 0} # print(my_list4) # my_list = [char for char in 'hello'] # my_list2 = [num for num in range(0,100)] # my_list3 = [2*num for num in range(0,100)] # my_list4 = [num**2 for num in range(0,100) if num**2 % 2 == 0] # print(my_list4) # # map, filter, zip, and reduce # from functools import reduce # import math # # Finding the largest prime in a list # user_input = list(map(int, input("Enter integers separated by space: ").split())) # print("Your list of numbers is:\n", user_input) # # Finding the maximum value of the list # max = reduce(lambda x, y: x if x>y else y, user_input) # print("The maximum value is:", max) # # Finding the floor of the square root of the max # sieve=int(math.sqrt(max)) # print ("The maximum integer to check divisibility is", sieve) # # prime = filter(lambda x: x % range[sieve]!==0, user_input) # primes = user_input # # Checking which integers in the list are prime # For i in range(2,1,sieve+1): # filter(lambda x: x % i == 0, primes) # # Finding the maximum value in a list # numbers = [4, 6, 8, 2, 9, 3] # maximum = reduce(lambda x, y: x if x > y else y, numbers) # print(maximum) # Output: 9 # # Concatenating strings in a list # words = ["Hello", " ", "World", "!"] # sentence = reduce(lambda x, y: x + y, words) # print(sentence) # Output: Hello World! # a = [1, 2, 3, 4, 5] # res = reduce(lambda x, y: x + y, a) # print(res) # my_list = [1,2,3] # def accumulator(acc, item): # print(acc,item) # return acc + item # print(reduce(accumulator,my_list,10)) # print(my_list) # my_list = [1,2,3] # your_list=(10,20,30) # their_list=(5,4,3) # def multiply_by2(item): # return item*2 # def only_odd(item): # return item % 2 != 0 # new_list = list(zip(my_list, your_list, their_list)) # #new_list = list(filter(only_odd, my_list)) # print(new_list) # print(my_list) # new_list = list(map(multiply_by2, my_list)) # print(new_list) # print(my_list) # def multiply_by2(li): # new_list = [] # for item in li: # new_list.append(item*2) # return new_list # def highest_even(li): # evens = [] # for item in li: # if item % 2 == 0: # evens.append(item) # return max(evens) # print(highest_even([10,2,3,4,8,11])) # def highest_even(li): # return max([num for num in li if num % 2 == 0], default=None) # print(highest_even([10,2,3,4,8,11])) # def test(a): # ''' # Info: this function tests and prints param a # ''' # print(a) # # test('!!!!') # # test('a') # help(test) # print(test.__doc__) # def checkDriverAge(age=0): # #age = input("What is your age?: ") # if int(age) < 18: # print("Sorry, you are too young to drive this car. Powering off") # elif int(age) > 18: # print("Powering On. Enjoy the ride!"); # elif int(age) == 18: # print("Congratulations on your first year of driving. Enjoy the ride!") # checkDriverAge(92) #1. Wrap the above code in a function called checkDriverAge(). Whenever you call this function, you will get prompted for age. # Notice the benefit in having checkDriverAge() instead of copying and pasting the function everytime? #2 Instead of using the input(). Now, make the checkDriverAge() function accept an argument of age, so that if you enter: #checkDriverAge(92); #it returns "Powering On. Enjoy the ride!" #also make it so that the default age is set to 0 if no argument is given. # def sum(num1, num2): # def another_func(n1, n2): # return n1 + n2 # return another_func(num1,num2) # total = sum(10,20) # print(total) # print([1.2,3].clear()) # Default Parameters # def say_hello(name='Darth Vader', emoji='⚔️👿💀'): # print(f'hellllooooo {name} {emoji}') # say_hello() # say_hello('Timmy') # say_hello('Elliot', '👿') #keyword arguments # def say_hello(name, emoji): # print(f'hellllooooo {name} {emoji}') # say_hello(emoji='\U0001f600', name = 'Bibi') #parameteres # def say_hello(name, emoji): # print(f'hellllooooo {name} {emoji}') # #arguments # positional arguments # say_hello('Elliot', '😛') # call, invoke # say_hello('Buddy', '\U0001F923') # call, invoke # picture = [ # [0,0,0,1,0,0,0], # [0,0,1,1,1,0,0], # [0,1,1,1,1,1,0], # [1,1,1,1,1,1,1], # [0,0,0,1,0,0,0], # [0,0,0,1,0,0,0] # ] # def show_tree(): # for image in picture: # for pixel in image: # if pixel: # print('*', end ='') # else: # print(' ', end ='') # print('') # for i in range(3): # show_tree() # # Exercise: Check for duplicates in list: # my_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n'] # duplicates = [] # for value in my_list: # if my_list.count(value) > 1: # if value not in duplicates: # duplicates.append(value) # a = str(duplicates) # print(a) # res = ' '.join(duplicates) # print(res) # i=0 # while i < len(my_list): # j=i+1 # while j < len(my_list): # if my_list[i]==my_list[j]: # print(my_list[i], end = ' ') # j+=1 # i+=1
def multiply(num1,num2): return num1*num2 def divide(num1,num2): return num1/num2
Editor Settings
Theme
Key bindings
Full width
Lines