Python to switch from another language

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 #.

division

/ Is always calculated as a floating point number. If you want an integer, use //.

print(5/2) # 2.5
print(5//2) #2

if statement

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

Ternary operator

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 statement

Ordinary guy

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)

Associative array

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():

LINQ-ish guy

#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

Python to switch from another language
Switch from python2.7 to python3.6 (centos7)
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Introduction to Python language
Post from Python to Slack
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
[Python] Another way to import
Connect to sqlite from python
Switch python to 2.7 with alternatives
Call Matlab from Python to optimize
Call C language functions from Python to exchange multidimensional arrays
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Introduction to Protobuf-c (C language ⇔ Python)
Connect to utf8mb4 database from python
Python (from first time to execution)
How to access wikipedia from python
Go language to see and remember Part 8 Call GO language from Python
Call c language from python (python.h)
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
Ported from R language of "Sazae-san's rock-paper-scissors data analysis" to Python
[It's not too late to learn Python from 2020] Part 3 Python Language Basic (1)
[Python] Fluid simulation: From linear to non-linear
From Python to using MeCab (and CaboCha)
How to update Google Sheets from Python
Send a message from Python to Slack
Private Python handbook (updated from time to time)
Convert from katakana to vowel kana [python]
Push notification from Python server to Android
Connecting from python to MySQL on CentOS 6.4
Porting and modifying doublet-solver from python2 to python3.
How to access RDS from Lambda (python)
How to switch python versions in cloud9
Python> Output numbers from 1 to 100, 501 to 600> For csv
Generate C language from S-expressions in Python
Convert from Markdown to HTML in Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
Updated to Python 2.7.9
Switch Python versions
Sum from 1 to 10
sql from python
C language to see and remember Part 1 Call C language from Python (hello world)
MeCab from Python
C language to see and remember Part 4 Call C language from Python (argument) double
"Backport" to python 2
C language to see and remember Part 5 Call C language from Python (argument) Array
Edit Excel from Python to create a PivotTable
How to open a web browser from python
Is sys.settrace, a python genius feature, another language?
[Python] Convert from DICOM to PNG or CSV
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
Ported a naive homebrew language compiler to Python
From file to graph drawing in Python. Elementary elementary
Writing logs to CSV file (Python, C language)
[Python] How to read data from CIFAR-10 and CIFAR-100