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.
This image was drawn by Chomado. Python is cute, isn't it?
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'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.
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
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. ** **
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 ...
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 ...)
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
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 / * ~ * /.
--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.
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.
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.
--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)
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!
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