Hi ya!
Previously on:
Today's contents
- Python Syntax
- Indentation
- Comments In Python
- Variables
- Intro to data types.
Syntax
(i)Indentation
space-bar key
is equivalent to a tab click
- depend on configuration) at the beginning of every line of code. Python takes indentation very seriously. You should ensure proper indentation in your program else Python will return error message which mostly noticed if you already configured VSCode to auto-run.From above window, you can see Python was not happy with our code, hence it returns IndentationError: unexpected indent
in the terminal being the error type it encountered while trying to process our input. On the left pane, our example.py
file displays in red and the "print" word is underlined in red - an indication there is anomaly in our code. I had moved the second line of code print("Welcome " + userName)
four spaces from left (position x) to right(position y). Oops! that's wrong because we are trying to continue the first sentence from line 2 without using a conjunction. compare this to our first program.
What Is A Line Of Code?
'1' and '0's
. Earlier inventors of computer languages had tried to make it speak English(as the means they communicate) in a different tongue using 'one' and 'zero'. As a programmer, computer is never the smart. You are the smart head. You could make it do anything you want. So every line of code is equivalent to a statement or sentence in English.
.
- A sentence in English is a combination of
subject
,verb
, andobject
. If "Verb " as an action word is missing, then one has not written a sentence.
Jeff
fruit.
Jeff is the "subject" while fruit is the receiving side i.e "object". The sense amidst the two words is missing which could be any action word such as eats, loves etc.The sensible sentence could be
Jeff loves fruit.
. This makes sense. Isn't it?and
, including
, etc, and combination of clauses and phrases. Please be patient with this long write up as it is needed to get the full gist. action word, a subject and an object
or a combination of an object and a verb
, or subject and verb
. Let us do some practicals.first_name = "Carnegie"
print(userName)
()
. In this case. It can either be a subject or an object. Our "print" function acted on user_name
which is the object that was the subject in line 4
in our VSCode.()
is very important as it is the one completing the print function hence it can be said to be the verb. if 10 > 3:
print("True")
if 10 > 3
is a phrase with incomplete meaning while print("True")
is a clause because it has a meaning. The semicolon :
acts as a conjunction. It actually predisposes if 10 > 3
as a phrase. (ii)Comment
Comment, as the name implies is system of explaining what your code does most especially where complex code or calculation is involved. It gives readability to your code. that is, other developers reading your code will be able to understand it. Alternately, comments are used to deactivate codes when you need to test them. Python ignores every characters enclosed in comment.
(iii)Variable
const
userName;
let
user_name;
var
username;
This is statically typed which is adopted in solidity as well. Solidity is statically typed language since you will have to declare the type of data you want to assign a value to each time to need to do so. However Python does not support this. You are required to assign a value to the variable for it to be valid.
user_name
= "Bobman"
==> String data type (holds a string of text)number_in_row
= 100
==> Integer data type (holds number)Facts About Variables In Python
- A variable can have a short name (such as a and b).
- It can contain a more descriptive name (color, colortype, net_worth etc).
- A variable cannot start with a number
- It must begin with a letter or the underscore character. It can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (name, Name and NAME are three different variables)
I hope that you enjoy every piece of information here. Are you still confused? You can go over it again. Python official Website is a good place to check for references and further reading.