View Single Post
Old 2019-11-04, 00:36   #4
Dylan14's Avatar
Mar 2017

2·293 Posts
Default Basic Syntax

In order to code, you must understand two things:
1. The functions that make up the language (otherwise you aren't going to do anything).
2. The syntax used in the language. This is arguably more important than knowing the functions. You may have a nice program that spans 10000 lines, but if you have simply one syntax error, the compiler/interpreter will complain with a syntax error, and you will be forced to scan through your entire code to find the error.

Now, each function has its own syntax depending on what it takes as arguments. For a simple example, let's take a look at SeedSequence from the random module in numpy:

class numpy.random.SeedSequence(entropy=None, *, spawn_key=(), pool_size=4)
This function has 3 arguments, all of them optional, since each of them have a default value. The default value for each of these is defined by using the equals sign and then after the equals sign a value is given. Each of these arguments also takes a certain type of input. For example, the "entropy" argument may take the value of "None", an integer, or a sequence of integers.
We'll get to functions later on in the guide. For now, we will deal with the basic syntax of Python. Firstly, we can't just enter random characters into a prompt(*). Typing

will yield an error:

NameError                                 Traceback (most recent call last) 

<ipython-input-2-6fcf9dfbd479> in <module>
 ----> 1 x  

NameError: name 'x' is not defined
In fact, before you can reference a "name", you need to define it first. So, say we want to initialize x to 0 (as for a counter). We can do this by typing the following:

x = 0
Now, x is defined. If we type x now in a new cell, we get the following

The next thing that we need to be wary of is the types of our variables(**). It will not end well if you try to add, say, an integer and a string. Try

x + "a"
Here, if you defined x = 0 as above, then x is an integer and "a" is a string. Clearly addition or concatenation doesn't work between these two types, and we get a TypeError:

TypeError                                 Traceback (most recent call last) 
<ipython-input-5-3fb36790c1e0> in <module> 
----> 1 x + "a"  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
But it clearly works if we have an integer. Typing this

should yield 1, and no errors.
Now another thing to note is that in Python, we use indents to denote control flow blocks, and we do not need to end lines with the semicolon character. This is in stark comparison to C, where you use curly braces to denote control flow blocks, and you use semicolons. Here's a symbolic example:
while(x <= 10):

C (****):
while(x <= 10) 
With this style of demarcation, you must be careful with your indents. Editors may have different styles. For example, on Colab, it uses 2 spaces for an indent, whereas on Kaggle and notepad++ it uses 4 spaces. On others a tab is used. I will use 4 spaces for the code examples.
More specific syntax will be introduced when we need it.

(*) Well, you can, but first you need to make the line a comment line first. See the section on Comments for more information.
(**) Some information on some common types in plain Python are located in the post labeled "Basic Constructs".
(***) Don't actually try to run this: this is only here as an example of the indent style.
(****) This follows the Allman brace placement. There are several other ones, but this is outside the scope of this guide.

Last fiddled with by Dylan14 on 2019-11-10 at 02:45 Reason: initial draft of this section
Dylan14 is offline