PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 3 - PYTHON SYNTAX | WEEK 1

in CampusConnect2 years ago (edited)

python day 3 banner.PNG


Good day everyone!
Hope you're doing great!! Today is the third day of the Python Programming class week 1. If you read my first introductory article for week 1, you will know our third topic for the day 3 and all we have to cover for day 3. Please if you have not read the first article please do well to read it here so that you will know what we have for each day of the week.

I brought out this idea of teaching python on steemit because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on steemit communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.
Note: The English language used in this class is literally understandable by a layman. So let's begin...


PYTHON SYNTAX

Some of us here might be familiar with the word "syntax" and it's meaning. Syntax just simply means rule of language. Every spoken language in the world has set of rules that governs how the words are combined to form phrases and sentences. English language for example, has some syntax that any phrases or sentences should conform with.
Example;
a) I am learning python programming language now - correct syntax
b) Am now python language programming learning - Incorrect syntax

If you are asked from the above English languages, which one conforms to the rule of English language which one will you say? I know majority of us if not all will say letter 'a' because by mere looking at it, that's the correct syntax of English language.

The same syntax applies to different computer programming languages. Every Programming language developed has set of rules that governs how program statements are written in the language. So, this is what I have for you today, to teach you how python syntax works so that we can avoid syntactical errors during our coding.

The AI-driven tool used for syntax checking in every programming languages is the Parser. Parser breaks sentences word by word to check if the sentence conforms to the rule of the language. The function can be seen during compilation process, i.e. converting from high-level language to low-level language by the compiler. If you want to read more about compilation process, click here.

All the different programming languages has their own syntax, and as a programmer, you must write your program statements to conform to the rule of the language... if not, a syntactical error will be thrown to you by the compiler or the interpreter.

Alright, so having known what a syntax is all about, let's now dive into Python Syntax which is our topic for the day.

PYTHON PROGRAMMING LANGUAGE SYNTAX
Python has a very simple syntax which is easy to learn and understand by programmers and non-programmers alike. In this class, we're going to learn some of these python syntax that you have to observe during coding.

throughout our classes henceforth, we will be making use of a text editor called Visual Studio Code to write our python programs. If you've not downloaded and installed the software on your PC, click here now to download and install from their official website.

I forgot to tell you in our last class that during the python installation process, make sure you add python to path by checkboxing the appropriate box for that. It is very important! See the image below:

python add to path.PNG
source

Please make sure you add python to path during the installation process because it is very important, if not you have to add it manually from your system settings through the system "Environment Variables". Use any of these materials listed here to learn how to add python path to environment variables. Material 1 or Material 2.

Let's execute our first Python syntax by printing "Hello World!"
Before we start our coding in our newly downloaded Vs code, we need to create a folder first in our desktop directory and name it "Steemit Python Class", then open our Vs code and open the folder we just created by clicking on the File tab, when the dropdown appears, click on Open Folder, and when it opens, select the folder we just created. Just follow the below steps to get everything done:

vs code file tab.PNG
Click on the File tab to open our folder


After importing the folder into our Vs code, create a new python file by right-clicking on the folder and name it "tutorial.py" as shown in the screenshot below:

vs code new file.PNG
Right-click on the folder to create a New file


After doing everything correctly as explained, you're now set to kickstart your coding on Vs code. Now let's create our first python program and see it's syntax.

Write a Python program to Print "Hello World"! to the screen

Hello world.PNG
Python program that prints Hello World to the screen

As we all can see from the above program, the syntax for printing an output says that the string characters to be printed must be enclosed in two double quotation marks "...". If we forget to enclose the string characters to be printed, the program will give us a syntax error as shown below.

Without the Quote
omitting quote.PNG
When we forgot to enclose the string in a quote

Syntax error
syntax error.PNG
The Syntax error thrown to us during execution

Looking at the screenshot above, we can see the syntax error thrown to us as a result of the omitted quote in our source program.

Note: We can execute our Python program from the Vs code terminal. To open the terminal, go to "Terminal" from the menu bar at the top and select "New Terminal".

Note: To run our python code from the terminal, type "python tutorial.py" i.e. "python filename".


Omitting a bracket from the python "Print" function will also give us a syntax error. And we have other kinds of errors we can encounter in our programs such as; NameError, TypeError, KeyError, IndentationError, ImportError, e.t.c. So we have to master Python syntax very well to avoid syntactical errors. As we proceed through the course, I will be introducing you to other possible syntax errors to encounter in python programs. The one explained earlier is the basic syntax error to encounter in python.


PYTHON INDENTATION

Indentation in python is used to indicate a block of code. Indentation is very important in python, while in other programming languages like C or C++, indentation is just used for readability of code. Indentation is mainly used during function definition and during loop statement (e.g. for loop) and control statement (e.g. if statement). It is also used in object oriented programming to indicate class blocks, and method blocks of codes.

Example;

No error with Indentation
python if statement.PNG

If we observe the if statements above, we can see that after the comparison, we moved four spaces (= 1 tab) to the right in the next line before we print our result in the IF STATEMENT block to the screen. And note that every if statement ends with a colon ':', and if we omit the colon, we will get a syntax error. The above code is just an if statement and the code to be printed is indicated in the block of the if statement.

However, if we look at the terminal we can see that the program executed successfully without any error, but if we mistakenly forgot to add the indentation, we will get an IndentationError as shown in the screenshot below.

Indentation Error occurred without Indenting the block
python indentation error.PNG
Indentation Error occurred

If we look at the code in the screenshot above, we can see that indentation error occurred in the terminal when we executed the program because we omitted indentation in our IF STATEMENT block.

As we progress in this course, we will see other areas where we can make use of indentation.


PYTHON COMMENTS

Comments in python are simply used to lay more emphasis on what a particular lines of code does in a program. It is just for readability and understandability of code. The concept behind comment in programming generally is to make it easy for another programmer or a layman to read and understand our code. We use it to explain our codes to others who are reading the code. It's purpose is for in-code documentation. It describes the components of the codes written.

Comment line usually starts with a "#" sign and it is not executed in the program. Any line that starts with "#" will not be executed by the python interpreter because it sees it as a comment line.

Example;

Line 1 is a comment line
python comment.PNG
Line 1 is a comment line

As we can see from the above screenshot, line 1 is treated as a comment line by the python interpreter because of the '#' sign that precedes the statement, and it is not executed during the program execution. Screenshot shown below...

Line 1 is not executed in the terminal because it is a comment line
python comment 1.PNG
The comment line is not executed in the program

We can see from our terminal that Line 1 which is a comment line is not executed in the program. This is what the '#' sign does in a program, it renders a line statement to a comment line. So, dear brothers, it is pertinent to use comments in our codes for proper documentation of our codes. Note that the comment in the above screenshot is a single line comment. We have two types of comments in python; Single line comment and Multiline comment. Multiline comment is shown in the screenshot below..

Multiline comment
python multiline comment.PNG
Multiline comment


PYTHON DOCSTRING
Docstring is also a type of comment in python that uses three quotation marks to enclose lines of statements. It can be one line or multi line. However, It provides an extended documentation capability for python. Read more about python comments here.

Multiline Docstring
python multiline docstring.PNG
Multiline Docstring

From the screenshot above, the statements in the opening and closing three quotation marks will not execute during the program execution in the terminal. This is what Python comments is all about. Subsequently, you will get to know more about comment usage in a program. But for now, let's proceed to the next class!



Conclusion

Learning python syntax very well will improve your programming skills. Indentations and comments are also very important in python. Indentation as we discussed earlier in the class is very important to use when writing python codes. Indentation, if omitted, will throw an IndentationError by the python interpreter. Comments as we also discussed provides documentation capability for our codes. It makes our codes more readable by another programmer or a layman.

This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please am so sorry I couldn't cover python variables in this class as it is written in our course outline for the day 3, but I promise to start with it in our tomorrow's class. Thanks for your understanding!

Please do well to follow this blog, and also don't forget to resteem this post so that it can reach larger number of steemians who wants to learn this skill. Tomorrow, we will look into "Python Data Types and Python Casting". Visit here to see the topics we have for this week.



Am glad you participated in this class! I believe you have learnt something new today.

I am @anyiglobal, a Computer Scientist, Software Engineer and a Blogger!



Cc:
@campusconnectng
@whitestallion
@steem-database
@ponpase
@ngoenyi
@srrebullient
@mato445
@kouba01
@pelon23
@reminiscence01
@swaylee
@starrchris

Sort:  
 2 years ago 

Am a computer science student and currently am stucked on this python programing, makes lots of mistakes daily while trying to write my coding🤣🤣🤣🤣...

This is really helpful dear thanks for sharing🙏🙏

 2 years ago 

You're welcome! Follow my blog always to get my class updates everyday! You will learn alot to improve your programming skill to an intermediate level. We will also learn how to use python for web application development (i.e. on the server-side) in our subsequent classes.

Which level are you in the university? 200l right?

 2 years ago 

My brother you are really working really hard oo, I am also trying to learn java programming language, and I can tell you it's really not easy to learn any programming language.

Thanks for sharing sir
Wishing you success

 2 years ago 

No programming language is hard to learn, you just need your time and apply logics

 2 years ago 

That one follow sha

 2 years ago 

But shaa the only time they're hard is when using them to build projects. That is where you have to apply logics. Programmers are calculative people also... that's why it's good to solve algebraic equations sometimes in mathematics.

 2 years ago 

Thats right man, with determination we will get to the peak

Thank you for contributing to #LearnWithSteem theme. This post has been upvoted by @daytona475 using @steemcurator09 account. We encourage you to keep publishing quality and original content in the Steemit ecosystem to earn support for your content.

Club Status: #Club5050

Sevengers Comment GIF.gif

Regards,
Team #Sevengers

The #learnwithsteem tag focuses on teaching through tutorials and lessons some knowledge, skill or profession that you have. Please avoid using it if it's not about that. Thank you!

 2 years ago 

Thanks! I appreciate your support always!

Hi, @anyiglobal
Thank you very much for taking part in the contest!