Basics of namespaces & scope

Lesson Details:
June 29, 2020


I: Introduction

The language has been in existence since 1991, when Guido van Rossum started working on it. Now it is one of the widely used languages across the world. It is a popular programming language because it is easy to learn & use. Python is open sourced and its source code is available in public domain.

II: Body

The basic syntax in python is simple. There are no braces to start & end the statements like in C. The print statement is used to print any value on screen.

# This code will print Hello World on screen

print(“Hello World”)

# This code will print Hello World on screen print("Hello World") 1 2 3 4 5 # This code will print Hello World on screen print ( "Hello World" ) # This code will print Hello World on screen print ( "Hello World" )

# This code will print Hello World on screen # This code will print Hello World on screen print("Hello World") print("Hello World") 1 2 3 4 5 6 7 8 # This code will print Hello World on screen # This code will print Hello World on screen print ( "Hello World" ) print ( "Hello World" )

Now we can use other operators like ‘+’, ‘-‘, ‘*’, ‘/’, ‘%’, ‘<’, ‘>’ , ‘==’, ‘!=’, ‘<=’, ‘>=’ , ‘<>’ for example. This is called operator overloading. In python, these symbols are used as per their mathematical meaning. For example, + is used to add two values and * is used to multiply two values. In python, all operators are always treated as binary operators.

# This code will add 2 values and multiply them by 10 result = a + b * 10 print(result) 1 2 3 4 5 6 7 # This code will add 2 values and multiply them by 10 result = a + b * 10 print ( result )

# This code will add 2 values and multiply them by 10 result = a + b * 10 print(result) # This code will add 2 values and multiply them by 10 result = a + b * 10 print(result) 1 2 3 4 5 6 7 # This code will add 2 values and multiply them by 10 result = a + b * 10 print ( result ) # This code will add 2 values and multiply them by 10 result = a + b * 10 print ( result )

In python, we don’t have to use semicolons at the end of each statement. We can also use comments in our source code in python. Comments in python start with a hash symbol (#). Sometimes I see in some source codes that they start with a hash symbol and then they write their comments using another hash symbol! So I think you should avoid this way. Instead of writing your comments directly with hash symbol, you can also write it like this # This is my comment. Remember that comments are not executed by the interpreter. They are only used to make your program more readable and understandable. Python interpreter ignores them and goes ahead with the next line of code.

# This code will add 2 values and multiply them by 10 result = a + b * 10 print(result) # This code will add 2 values and multiply them by 10 result = a + b * 10 print(result) # This code will add 2 values and multiply them by 10 result = a + b * 10 print(result) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # This code will add 2 values and multiply them by 10 result = a + b * 10 print ( result ) # This code will add 2 values and multiply them by 10 result = a + b * 10 print ( result ) # This code will add 2 values and multiply them by 10 result = a + b * 10 print ( result )

Python allows us to create many different types of variables. We can declare variables using different types of declarators like int, float etc. Different types of data types i.e. String, Float, Boolean etc have their own rules of declaration/initialization. You can also give default value to variables or list of variables when declaring them instead of initializing them later in the source code. There are many other things about declaring variables which I won’t be able to cover here. If you are interested, you can learn about it from PEP8 or any other python book. These are just few topics that I wanted to show here to make you understand how variable works in python. Let me show you an example to make you understand it better.

# This is an integer value i = 3 # This is an integer value i = 3 # This is an integer value i = 3 1 2 3 4 5 6 7 8 9 # This is an integer value i = 3 # This is an integer value i = 3 # This is an integer value i = 3

# This is an integer value i = 3 # This is an integer value i = 3x3 # This is an integer value i = 3x3x3 1 2 3 4 5 6 7 8 9 # This is an integer value i = 3 # This is an integer value i = 3x3 # This is an integer value i = 3x3x3

# Declare array of integers arr1 = [0]*10 arr2 = [0]*10 arr3 = [-1]*10 arr4 = [-1]*10 arr5 = [-1]*10 1 2 3 4 5 6 7 8 # Declare array of integers arr1 = [ 0 ] * 10 arr2 = [ 0 ] * 10 arr3 = [ - 1 ] * 10 arr4 = [ - 1 ] * 10 arr5 = [ - 1 ] * 10

# Declare array of integers arr1 = [0]*10 arr2 = [0]*10 arr3 = [-1]*10 arr4 = [-1]*10 arr5 = [-1]*10 1 2 3 4 5 6 7 8 9 # Declare array of integers arr1 = [ 0 ] * 10 arr2 = [ 0 ] * 10 arr3 = [ - 1 ] * 10 arr4 = [ - 1 ] * 10 arr5 = [ - 1 ] * 10

III: Conclusion

loader
Course content