# bad practice码 n1 = input("enter a number : ") n2 = input("enter a number : ") n2 = input("enter a number : ") print(n1, n2, n3)
# good practice n1, n2, n3 = input("enter a number : ").split() print(n1, n2, n3)
size = "lg" color = "blue" price = 50 # bad practice if size == "lg" and color == "blue" and price < 100: print("Yes, I want to but the product.")
# good practice conditions = [ size == "lg", color == "blue", price < 100, ] if all(conditions): print("Yes, I want to but the product.")
# bad practice size = "lg" color = "blue" price = 50 if size == "lg" or color == "blue" or price < 100: print("Yes, I want to but the product.")
# good practice conditions = [ size == "lg", color == "blue", price < 100, ] if any(conditions): print("Yes, I want to but the product.")
print('odd' if int(input('Enter a number: '))%2 else 'even')
v1 = 100 v2 = 200 # bad practice temp = v1 v1 = v2 v2 = temp
v1 = 100 v2 = 200 # good practice v1, v2 = v2, v1
print("John Deo"[::-1])
v1 = "madam" # is a palindrome string v2 = "master" # is not a palindrome string print(v1.find(v1[::-1]) == 0) # True print(v1.find(v2[::-1]) == 0) # False
name = "ali" age = 22 # bad practices if name: print(name) if name and age > 18: print("user is verified")
# a better approach print(name if name else "") """ here you have to define the else condition too""" # good practice name and print(name) age > 18 and name and print("user is verified")
lst = [1, 2, 3, 4, 3, 4, 4, 5, 6, 3, 1, 6, 7, 9, 4, 0] print(lst) unique_lst = list(set(lst)) print(unique_lst)
lst = [1, 2, 3, 4, 3, 4, 4, 5, 6, 3, 1, 6, 7, 9, 4, 0] most_repeated_item = max(lst, key=lst.count) print(most_repeated_item)
numbers = [1,2,3,4,5,6,7] evens = [x for x in numbers if x % 2 is 0] odds = [y for y in numbers if y not in evens] cities = ['London', 'Dublin', 'Oslo'] def visit(city): print("Welcome to "+city) for city in cities: visit(city)
def sum_of_squares(n1, n2) return n1**2 + n2**2 print(sum_of_squares(2,3)) # output: 13 """ what ever if you want to pass, multiple args to the function as n number of args. so let's make it dynamic. """ def sum_of_squares(*args): return sum([item**2 for item in args]) # now you can pass as many parameters as you want print(sum_of_squares(2, 3, 4)) print(sum_of_squares(2, 3, 4, 5, 6))
lst = ["blue", "lightblue", "pink", "orange", "red"] for idx, item in enumerate(lst): print(idx, item)
names = ["john", "sara", "jim", "rock"] print(", ".join(names))
d1 = {"v1": 22, "v2": 33} d2 = {"v2": 44, "v3": 55} d3 = {**d1, **d2} print(d3)
{'v1': 22, 'v2': 44, 'v3': 55}
keys = ['a', 'b', 'c'] vals = [1, 2, 3] zipped = dict(zip(keys, vals))
d = { "v1": 80, "v2": 20, "v3": 40, "v4": 20, "v5": 10, } sorted_d = dict(sorted(d.items(), key=lambda item: item[1])) print(sorted_d) 当然我们也可以使用itemgetter( )来替代上述 lambda函数,代码如下: from operator import itemgetter sorted_d = dict(sorted(d.items(), key=itemgetter(1)))
sorted_d = dict(sorted(d.items(), key=itemgetter(1), reverse=True))
from pprint import pprint data = { "name": "john deo", "age": "22", "address": {"contry": "canada", "state": "an state of canada :)", "address": "street st.34 north 12"}, "attr": {"verified": True, "emialaddress": True}, } print(data) pprint(data)
{'name': 'john deo', 'age': '22', 'address': {'contry': 'canada', 'state': 'an state of canada :)', 'address': 'street st.34 north 12'}, 'attr': {'verified': True, 'emialaddress': True}} {'address': {'address': 'street st.34 north 12', 'contry': 'canada', 'state': 'an state of canada :)'}, 'age': '22', 'attr': {'emialaddress': True, 'verified': True}, 'name': 'john deo'}
以上がPython に関する 17 の役立つヒントを共有しましょう。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。