CHAPTER 8 General Syntaxes PART 2

CHAPTER 8 General Syntaxes PART 2

If we try to push the outside print() function a little bit inside, it will give an error, 
because Python interpreter will think that it is inside the main() function. Actually this is 
not true. If we want to push that “outside print() function” inside the main() function, we 
need to place it on the same line of the inside print() function like this

Now the output changes. It looks like this:

//output
A line inside main function.
A line outside main function.
//output ended

We learn a very important lesson that we should learn by heart. The lesson is: white 
space or indentation in Python plays a major role. When we write a function and put 
some other functions inside it, they must fall on the same line. In any text editor or IDE, 
it is automatically done. When you press the “enter” or “return” key, the following lines 
keep falling on the same line. If you want to go outside that function, just follow the first 
example. Just to understand how indentation works in Python, we write a little lengthy 
code and see how it looks

# coding=utf-8

def main():

       # print('A line inside main function.')

     

     # print("A line outside main function.")

OutsideMainFunction()

def OutsideMainFunction():

     x = 0

    while x < 5:

    print(x)

    x = x + 1

if __name__ == main():main()

Look at the code. We have a main() function. Additionally, we have a function called 
“OutsideMainFunction()'”. It is really outside of the main() function. So they are different 
functions and they have their own indentations. Inside of the “OutsideMainFunction()” 
we see a “while loop.” That “while loop” also has its own indentation. Actually we better 
call it “block.” So every block of code has its own “white space” or the code inside 
that block is indented accordingly. If you don't use any IDE and try to write it on your 
terminal, you have to use the space bar. Inside a function, if you use “four spaces,” then 
whatever you write inside that function must fall on the same line. That is, whenever you 
write a new line, it must have “four spaces.” You cannot give two or three space suddenly. 
But if you write another function, you can change that rule. In that case, the new function 
has its own block of code and it has its own rule. You may use two spaces now.

Commenting

In any kind of programming, commenting is very important. Another programmer will 
read your program. Your every step should be readable. If there is any kind of twist or you 
try something special, you must explain that inside your code. Consider this code:

# this is main() function

def main():

         OutsideMainFunction()

# this function is outside main() function

def OutsideMainFunction():

     x = 0

        while x < 5:

        print(x)

        x = x + 1

if __name__ == main():main()

Normally any comment is written with a # (hash) mark. When Python interpreter 
sees #, it knows that is a comment and it ignores it. In our code, we clearly define what 
is the main() function and we also say in our comments that there is another function 
which is outside the main() function.
Normally a seasoned programmer never comments such simple stuff. But to begin 
with, you can add comments when you feel it is necessary. Because after some time, 
when you revisit your old codes, you can remember why you did that. Commenting is 
useful in that way. At the same time, you cannot trust all comments. Programmers often 
forget to change comments when they change their codes

Assigning Values

In Python, the assignment operator is an equal (=) sign. When you write “a = 10”, it means 
“a” is a variable or a container. This variable “a” is assigned to an integer value. What is 
that value? It is 10. This value could have been a string. What is a string? A string is an 
addition of characters. Suppose you write “b = Two”. It means the variable “b” is assigned 
to a string value, and that string is “Two”, which is nothing more than three characters: 
“T”+“w”+“o”. According to your assignment, Python interprets the value and keeps a 
definite storage place for them. It knows how many bits and bytes will be required for 
them.
In Python, everything is object. Python is an object-oriented programming language. 
As a beginner, you may not understand this concept. Don’t worry. We will discuss it in 
detail as we progress. You will learn it. Presently you just remember that an object means 
an instance of class. Imagine yourself as an object. In that case, you are an instance 
of “human” class. You have some properties like height, width, etc. You also can do 
something. The “human” class is a blueprint of you and other humans and in “human” 
class, everything has been well-defined. There are a lot of properties and a lot of action 
verbs defined. And according to that definition, you, me, and other humans keep doing 
things.
When we say in Python that everything is an object, it means everything has a class 
or blueprint behind it. We write like this:

#!/usr/bin/python3

# coding=utf-8

a = 1

print(a)

print(type(a))

print(id(a))

a = "One"

print(a)

print(type(a))

print(id(a))

And the output is like this:

//output

1

<class 'int'="">

139113568

One

<class 'str'="">

3073583584

//output ended

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow