Grammar of a programming language - A look into Python (Basics)

in #python7 years ago

Python is undeniably one of the most powerful programming languages, yet simple enough for beginners to grasp it. It's quite easy to read the codes as it somehow resembles the structure of a normal English language. Nevertheless, one has to properly construct the codes as the computer is quite unforgiving and will give error messages if something is not properly written. This is the opposite of English language where a mess up grammar or a broken sentence or a misspelled word can still be understood.

Syntax

Every type of programming language has its own syntax. In other words, its own 'grammar' rules. (i.e-how to write, what order to write, etc) The best way to start understanding the syntax is by experimenting it with IDLE, which is the editor and compiler for Python. When IDLE is opened, you will be greeted with following interface:

Note that the symbol >>>  means that the program is ready and awaiting for the user's input. You basically have to write something (Python code) followed by an Enter.

Python as Calculator

Now, let's try it out. Perhaps the most simple line of code would be in the form of arithmetic/mathematical expression. In other words, Python can be used as a calculator. You can use Python for addition, subtraction, multiplication, division and more advanced stuffs like the power of or square root, to name a few. Just type the mathematical expression as you would in a calculator and see the results. Refer the image below for example of this. 

That's the basic math expression. How about if you want to find out the square root or the power of a number? In Python, the symbol for the power of is **. Say for example 3^3 will be written as 3**3 while cube root of 27 will be written as 27**(1/3) in Python.

Now you might wonder why some of the numbers have decimal point and some are not. This is just how Python treats mathematical expression differently, where division of 2 integers will give floating point value while multiplication of 2 integers will give integer result. To check the type, try the following command:

As you can see, the number 27 is an integer while the number 2.0 is a floating point value.

Variables and Assignment

Another great thing about Python is that we can store a memory and retrieve the value later on. This can be done by using variable, where we assign a value to a variable. This feature is quite helpful especially when it comes to performing complex calculations. Below are some examples of using variables:

From the code above, the first 4 lines show how we perform assignment in Python, where the variable a is assigned to a value 3, variable b is assigned to a value 6 and so on.

Strings

In previous examples, we have dealt with numbers. Now, let's deal with text. For any programming language, (including Python) a string is any text/characters that you can type with the keyboard. Python will recognise a string when you put either single quote ' ' or double quotes " " around the text. Let's throw some examples here:

From the above example, we can see that 2 assigned string variables can be added together to form a new string (line 3). Again, we can check the type by using the type command as in line 6 above. Apart from adding string, we can also multiply the string. This can be seen from second last line where the word 'spam' is multiplied by 5 which resulted it to be repeated 5 times in a single line.

There are loads more of stuffs in Python but I think its best for me to write them up in separate post. I'll be adding more topics on Python soon.

Do let me know what do you think. Cheers.

Sort:  

Good motivation to learn Python. Very good!
So I give my vote.

Thank you very much for the feedback & upvote. Feel free to read my other posts on Python.

Congratulations @rahim.rahman! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 91530.30
ETH 3127.22
USDT 1.00
SBD 3.07