• Nov 18, 2022 •AustinLeath
0 likes • 4 views
mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':0} # How to sort a dict by value Python 3> sort = {key:value for key, value in sorted(mydict.items(), key=lambda kv: (kv[1], kv[0]))} print(sort) # How to sort a dict by key Python 3> sort = {key:mydict[key] for key in sorted(mydict.keys())} print(sort)
• Jun 1, 2023 •CodeCatch
0 likes • 2 views
filename = "data.txt" data = "Hello, World!" with open(filename, "a") as file: file.write(data)
• Nov 19, 2022 •CodeCatch
0 likes • 6 views
# function which return reverse of a string def isPalindrome(s): return s == s[::-1] # Driver code s = "malayalam" ans = isPalindrome(s) if ans: print("Yes") else: print("No")
• Feb 26, 2023 •wabdelh
0 likes • 0 views
#You are given a two-digit integer n. Return the sum of its digits. #Example #For n = 29 the output should be solution (n) = 11 def solution(n): return (n//10 + n%10)
• May 31, 2023 •CodeCatch
0 likes • 5 views
my_list = [1, 2, 3, 4, 5] removed_element = my_list.pop(2) # Remove and return element at index 2 print(removed_element) # 3 print(my_list) # [1, 2, 4, 5] last_element = my_list.pop() # Remove and return the last element print(last_element) # 5 print(my_list) # [1, 2, 4]
def print_x_pattern(size): i,j = 0,size - 1 while j >= 0 and i < size: initial_spaces = ' '*min(i,j) middle_spaces = ' '*(abs(i - j) - 1) final_spaces = ' '*(size - 1 - max(i,j)) if j == i: print(initial_spaces + '*' + final_spaces) else: print(initial_spaces + '*' + middle_spaces + '*' + final_spaces) i += 1 j -= 1 print_x_pattern(7)