Since I started Python, I have summarized the points to stumble when switching from another language. Even if I say other languages, I assume C ++ or C #.
/ Is always calculated as a floating point number. If you want an integer, use //.
print(5/2) # 2.5
print(5//2) #2
or / and is manma or / and. As an aside, pass can be done without any block part.
if a == 0 and b == 0:
pass
if a == 0 or b == 0:
pass
It's a very unfamiliar format.
int min = a < b ? a : b; //For C
min = a if a < b else b #How to write python
for i in range(10):
print(i) # 0,1, ... ,9
Array
array = [10, 20, 30, 40, 50]
for value in array:
print(value)
#I also want the value of index
for i, value in enumerate(array):
print(i, value)
dic = {'k1': 100, 'k2' : 200}
#Get the key
for key in dic:
print(key) # k1, k2
#Get value
for value in dic.values():
print(value) # 100, 200
#Simultaneous acquisition of key and value
for key, value in dic.items():
#Select in LINQ
#I want an array that is too much divided by 100
[(p % 100) for p in list]
#Where in LINQ
#I want an even-numbered array
[i for i in list if i % 2 == 0]
Recommended Posts