Getting started with Python - Day 2

in #python4 years ago (edited)

Hi ya!

image.png

src

Its day 2 of my tutorial on python development. Few hours ago, I started a blog series on becoming a Python developer - an initiative coined from Algorand's foundation. If you do not have previous experience of Python programming, I recommend that you read every previous articles before the other.


Previously on:


Today's contents

  • Python Syntax
  • Indentation
  • Comments In Python
  • Variables
  • Intro to data types.

Syntax

Generally, in all programming languages, Syntax is a set of rules displaying how a program should be written. Python syntax according to Wikipedia, is the set of rules that defines how a Python program will be written and interpreted (by both the runtime system and by human readers)

Think of it as Church called "Lord have mercy scepter" having its headquarter in Lagos and several other branches across nations and abroad. There will be protocols guiding how the branches should operate. The doctrine that is practiced in the parent church must not be different from the branches else there will be inconsistency. The structure of Python syntax is similar to the of "Lord have mercy scepter"'s. The environment in which you write your code (e.g VSCode) and other compatible tools are configured to understand same set of rules so as to have a uniform output provided the right input is supplied.

(i)Indentation

This is whitespace/space (usually four clicks of a 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.

image.png

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.

(2)
(1)

What Is A Line Of Code?

Its all about language. Computer does not understand English but '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, and object. If "Verb " as an action word is missing, then one has not written a sentence.

Example: 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?

Additionally, we can make a statement by combining multiple sentences using conjunctions such as 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.


As explained, likewise a line of code must have action word, a subject and an objector a combination of an object and a verb, or subject and verb. Let us do some practicals.


Example 1

first_name = "Carnegie"
print(userName)

image.png

"print()" is the subject and action word acting on whatever is passed within the parentheses (). In this case. It can either be a subject or an object. Our "print" function acted on user_namewhich is the object that was the subject in line 4 in our VSCode.

NB: The parentheses () is very important as it is the one completing the print function hence it can be said to be the verb.

In short, being able to identify a line of code and a block of code would influence your ability to indent properly.


Example 2

 if  10 > 3:
                       print("True")

If 10 is greater than 3, say it is "true". The 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.

image.png


(iii)Variable

In programming, variable is common to all. Some of the PLs allow for declaration of variable for later use. Javascript is a good example.

---Declaring a variable in Javascript---.

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).

--->Syntax for Python variables<---

  • 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.


In upcoming series, I will dive into how you can set up your Python environment with Algorand's dependencies including necessary tools so we can code along while using Algorand's Software Development Kits (SDKs).

image.png

src

Algorand is a technology company that built and developed the world’s first open, permissionless, pure proof-of-stake blockchain protocol that, without forking, provides the necessary security, scalability, and decentralization needed for today’s economy. With an award-winning team, we enable traditional finance and decentralized financial businesses to embrace the world of frictionless finance.