Python: Basic Syntax
Now we should talk about some python things. How we could work in python, what are major rules and protocols use in python to write any code. In this post we would try to write some basic code and try to understand and learn what are basic syntax used to write some of them which is used in any python code
Python has its own interpreter which is really useful for any programmer in any point of time and also feature that enable it to write sample code to test before make it final code for some stable code method that could used as program.
Python first program
Writing a Hello world code in python with both version new and old. This is not matter that how code different from each other. But its just matter how easy it is.
root@jarvis:~# python Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello Word" Hello Word >>>
root@jarvis:~# python3.7 Python 3.7.0 (default, Sep 12 2018, 18:30:08) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello World") Hello World >>>
We Just need to use print keyword to carry what need to print with same. That could slit different in visual but how easy is this to print something in python.
Python Simple Syntax
This is not that only case python is quit easy in many sense. sometime it looks like it’s writing some instruction in English language. we can take some other examples with some simple mathematical calculation. see below.
>>> 6 > 4 True >>> 10 > 12 False
See above it was easy to do some numeric comparison, it as easy look like someone doing it on calculator for us. Just writing it on and get result instantly. let’s do some operations on same.
>>> 6+10 16 >>> 8*11 88
Same as above. nothing like working for some number crunching , Just write down and get your answer on same prompt, ask nothing just print your number.
It’s same for some conditions, no ugly curly brackets before and after code complete. We just have take care of indentation, like code statements should comes under code heading like below.
>>> if 6 > 4: ... print "6 is greater than 4" ... 6 is greater than 4
See, how simple is this, but did you understand what mean indentation. i could understand for English speakers , it could normal word. indentation means every code body after if condition should put some more blank space in start, so it appear like comes under it and python able to identify that this code block should need to execute once above condition turns true. let me make it more complex with ifelse condition.
>>> if 6 > 4: ... print "6 is greater than 4" ... else: ... print "6 is smaller than 4" ... 6 is greater than 4
See code is same with another else statement, but this time i used four space instead of one, this make more visible to viewer. I hope this time its clearly visible to you that we are writing code statements inside condition and also next condition is also written on same point which make python clear that second condition is also part of same code block that need to evaluate in same way.
With below code, we could understand it that we need to use proper indentation for code blocks in python program no matters how long is code is.
>>> a=6 >>> b=10 >>> if a > b: ... print "a is greater than b" ... elif b > a: ... print "b is greater than a" ... else: ... print "a is equal to b" ... b is greater than a
if we not use proper indentation in python code , what could happen . Let’s see how it will react.
>>> if a > b: ... print "a is greater than b" File "
", line 2 print "a is greater than b" ^ IndentationError: expected an indented block
So now we know python indentation is very important and no way to skip , we have to follow it anyway to write code in python language.
As we used some keyword in above codes, we have some pre-defined keywords in python which we should not used in any variable or in general statement code. In python2.7 we have 31 keywords. same way in python3.7 we have 35 keywords.
>>> import keyword >>> keyword.kwlist ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'] >>> len(keyword.kwlist) 31 >>>
We could also used multi-line statements in python code like below, which helps to design code in such way that make it more visible to anyone new to that code, this fashion also implement with long SQL queries as well.
>>> print "This is multi-line\ ... print \ ... statement" This is multi-lineprint statement
So python allow line continuation character (\) which could use continue python code like above, please do understand this is not only with print statement we could use this in any complex if statement like below.
>>> a=10 >>> b=11 >>> if (a and b > 0) and \ ... b > a: ... print b, "is greater than 0 and", a ... 11 is greater than 0 and 10
So this above is clear example of Explicit Line Continuation, in which we could understand that this continuation character could use in multi-line statement.
There is another way to manage multi-line statement with any list,tuple or dictionary which would allow to write things in such way that we could create them with multi-line declare statements.
>>> alpha=["a","b","c","d", ... "e","f", ... "g","h","i"] >>> alpha ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
But we can also use to define things in much useful way like below , in which we could use this statements in some calculation.
>>> number=(7+8 ... *15 - 86 ... % 79 +100) >>> number 220
Quotation in Python
In python we can define any string or print statements with single(”) or double (“”) or triple double or single (“”” / ”’)to enclosed them, but we should consistent
while using it.
>>> print "Hello world" Hello world >>> print 'Hello world' Hello world >>> print """Hello ... world multiple line ... commment""" Hello world multiple line commment
This way we understand, we could use single(”) or double (“”) or triple double or single (“”” / ”’) for as above. Only triple quotation can use for multiple-line strings or print statement, not single or double.
>>> print ""Hello File "
", line 1 print ""Hello ^ SyntaxError: invalid syntax
We could also used to denote string variable like same.
>>> str="""Hi, this is multi-line ... string. please ... close it with """ >>> type(str) <type 'str'> >>> str 'Hi, this is multi-line \nstring. please\nclose it with ' >>> print str Hi, this is multi-line string. please close it with