Python started by C programmers

How did you decide to study Python?

Time has passed, as it has been a hot topic for a long time and I wanted to touch it ... Just recently, there were children around me who touched Python, and I got advice from people in the company to try other languages, so I feel that my motivation for studying has increased.

Digression

This image was drawn by Chomado. Python is cute, isn't it? pythonChan

Before reading this article

Please note that the poster has already touched ** C / C ++**, ** Java **, ** C # **, so I will write the article from the perspective of comparing with such C languages.

I want to touch Opaison

I'm sorry for the off-color headline. I just want to say. Here is a summary of the specific basic Python grammar. Since the content of the reference article is omitted in my own way, please see the reference article for details.

No need for a semicolon

As the title says, I don't have to write any more, but I don't have to add a semicolon at the end of the sentence, so one type of work is reduced! !! Personally, I usually touch C languages, so I feel something is wrong, but lol

No need to specify type

In C / C ++ etc., the type must be specified when declaring a variable.

C++


int num = 100;

If this is Python, it will be like this.

Python


num = 100

It seems that Python does auto and var (type inference) things in C ++ and C # by default. ** (Dynamic typing) ** However, there are some advantages and disadvantages because it is sometimes better to clarify. ** By the way, function arguments and return values also have no type specification. ** **

No need for curly braces (no parentheses for if or for)

It's full of no lol

Python


num = 77
if num % 2 == 0:
  print("Is an even number")
else:
  print("Is odd")

Instead of having no curly braces, it seems that the condition and processing are separated by **: (colon) **. ** It seems to judge the hierarchy by indenting ** Also, it seems that parentheses are not added to conditions such as if, for, and while. In this case, it's peaceful because there is no war in the position of the curly braces ...

There is no switch ~ case and do ~ while

Not as the title says. I rarely use do-while. It seems that if ~ elif ~ else is used instead of switch. Note that it looks like ** elif **, not else if. (Why did you omit it ...)

There are no type constraints on the list

It's an array in C, but unlike arrays, Python lists have no type restrictions **, so it seems okay to have a mixture of numeric types and strings.

Python for is an extension for (foreach in C #)

Python


arrayList = ["cat",100,25.25,"Python"]

#Turn around in the list
for array in arrayList:
   print(array)

#If you want to specify by index and turn for
for index in range(0,10):
  print(index)

As per the comments in the code above. Arrays from 0 to 9 are prepared by using range. By the way, the comment is # instead of ** // and'''~'''** instead of / * ~ * /.

Operator system

--Increment decrement operator does not exist ** (can be used with + = and-=) ** -// (Truncate after the decimal point and adjust the integer value to the lower one) and ** (power) -Logical operation&&Or|| 、!Butand、 or、 notIt is treated as a reserved word.

function

Python functions are treated as function objects.

Python


def Add(x,y):
  return x + y

As mentioned above, you don't need to specify the type, so you can draw like this. If you add ** def ** to the front, it is a function. I like it because it's so simple. What's more, the great thing about this is that there is no type specification, so you can do this as well.

Python


print(Add(2,5))
print(Add(1.6,4.8))
print(Add("Hello","World"))

It can be handled as a template function in C ++ or C #. (It is too convenient not to specify the type with <>) However, it seems that the function cannot be overloaded. If you want to do something similar, you can use the default arguments.

pass statement

There is a statement that passes without error without writing a process called pass statement.

Python


def AddPosition():
  #TODO:Implemented later
  pass

It seems that the pass statement is used when you want to implement it later like this. I see.

Other

--import is C as #include --Associative array (dict) and tuple can be used as standard --There are no constant keywords such as const, readonly, and final. (It seems that uppercase variables are treated as constants by convention) --There are no access specifiers such as public or private. (Something like adding one _ to something that is customary and private) --No virtual or override. (It is possible if you inherit and define a method with the same name) --Multiple inheritance is possible. (Instead, there is no interface in java)

Summary

I think this is the basic grammar. Features that are also C ++ or C # are omitted. See the reference article for details. After studying lightly, I felt that it was easy to write highly readable code. In the future, I would like to learn about automation and AI using Python.

Also, if you have any comments or advice about Python, feel free to comment!

Reference article

An introduction to Python for C programmers https://qiita.com/shiracamus/items/fd35c685e9679323471f

Differences C # engineers felt when learning python for the first time https://qiita.com/CEML/items/29944cbeb8e38171a630

Comparison of the two to switch from C # to Python https://qiita.com/kent-u/items/e6b210d38ca39b4cd107

Since C # er studied Python, let's talk about the differences while comparing them. Part 1 Syntax https://hiroronn.hatenablog.jp/entry/20170717/1500281854

Getting Started with Python https://python.keicode.com/

Recommended Posts

Python started by C programmers
started python
python, openFrameworks (c ++)
Socket communication by C language and Python
An introduction to Python for C programmers
Primality test by Python
Django 1.11 started with Python3.6
Visualization memo by Python
Communication processing by Python
Python C / C ++ Extension Pattern-Pointer
1.1 Getting Started with Python
Getting Started with Python
Next Python in C
Beamformer response by python
C API in Python 3
Getting Started with Python
ABC147 C --HonestOrUnkind2 [Python]
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
Memo of "Cython-Speeding up Python by fusing with C"
Extend python in C ++ (Boost.NumPy)
Getting Started with Python Functions
ABC163 C problem with python3
Python, Java, C ++ speed comparison
PyTorch C ++ VS Python (2019 Edition)
EXE Web API by Python
Newcomer training program by Python
C / C ++ programmer challenges Python (class)
Pin python managed by conda
ABC memorandum [ABC163 C --managementr] (Python)
Getting Started with Python Django (4)
Binary search in Python / C ++
Getting Started with Python Django (3)
Separate numbers by 3 digits (python)
Markov switching model by Python
Multi-stage selection (C # / Python) (old)
I tried Python C extension
Getting Started with Python Django (6)
Get started with Python! ~ ② Grammar ~
ABC188 C problem with python3
IfcOpenShell python bindings get started
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
Platform (OS) determination by Python
ABC187 C problem with python
Sort by date in python
Pass a list by reference from Python to C ++ with pybind11
Prolog Object Orientation for Python Programmers
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Solve ABC163 A ~ C with Python
Getting Started with Python responder v2
Get started with Python! ~ ① Environment construction ~
Call C from Python with DragonFFI
Create Awaitable with Python / C API
Link to get started with python
[Python] Sort iterable by multiple conditions
Multi-stage selection (Go / C # / Ruby / Python)
Expansion by argument of python dictionary
ABC127 A, B, C Explanation (python)
Getting Started with Python Web Applications
Machine learning summary by Python beginners
ABC166 in Python A ~ C problem