Language description

Python is a scripting language. It was designed as a glue language to execute operating system commands and call C++ scripts. It has evolved into something more that that. Now Python is used as a general-purpose multi-paradigm computer langauge. It can be used to create console applications, services or automation scripts.

Hello world

This is the a simple program you can see in many languages as first example. This  will print “hello world” on the console. The purpose of this example is to introduce you to the most common syntax rules. Python is scripting and interpreted language so you can create a script file or use a command.

# compiler entry point
if __name__ == "__main__":
    print("hello world")

Syntax Notes:

  • python comments start with “#” symbol
  • the main procedure is named “__main__”
  • strings are enclosed in double quotes: “hello world”
  • function call is using rount parantheses print(…)
  • function result can be ignored
  • To start the “if” block we use “:” after the condition
  • Blocks of code are indented at equal number of spaces

Interpretor:

Python is an interpreted language. After you start python you can use a command and python will display the result. Therefore previous program has the same effect as the following simple command:

c:\>python

>>> print("hello world")
>>> hello world

Executing a script

You should install python and try this command. In my future snippets I will not use command prompt. All snippets are programs that can be saved with extension <program_name>.py and should work if you run them using a command. Replace <program_name> with a proper name of your chosing.

c:\>python <program_name>.py.

Punctuation

SymbolPurposeExample
#single line comment#comment
=assign value to variablex=10
;separate multiple statementsa=1; b=a + 1; c = a+2
.dot notation (member of)self.a = 1
“…”unicode strings=”this is a string”
‘…’strings=’this is a string’
“””triple quoted string (documentation)

“”” this is a large string

on multiple lines “””

()empty tuple literalt=(1,2,”a”)
[]empty list literall=[1,2,3]
{}empty set literals={1,2,3}
{:}empty dic (dictionary)d={“a”:1,”b”:2,”c”:3}
:define or pair-upd={“a”:1,”b”:2,”c”:3}
[n]subscript (n) for collections[0] == 1 #this is true
+adition a=1+2 #result is 3
*multiplication a=2*2 #result is 4
/division a=2/2 #result is 1.
**exponent (powera=2**3 #result is 8
//floor divisiona=9//2 #result is 4
%reminder a=1/2 #result is 0.5
<>not equal (divergent) 2<>3 #true
!=not equal (divergent) 2!=2 #false
==equality 1==1 #true
>=greater then or equal 1 >=0 #true
<=less then or equal 1 <=0 #false
>grater then 1 > 0 #true
<less then 1 < 0 #false
+=in place addition. (modifier). Other modifiers are available for all math operators: -=, *=, /=, **=, %=, //=a=1
a+= 1  #a = 2

Binary operators

Next operators operate at bit level. Tthat is changing the bits

OperatorNameDescriptionExample
&Binary ANDOperator copies a bit to the result if it exists in both operands(a & b)
|Binary ORIt copies a bit if it exists in either operand.(a | b)
^Binary XORIt copies the bit if it is set in one operand but not both.(a ^ b)
~Binary Ones ComplementIt is unary and has the effect of ‘flipping’ bits.(~a )
<<Binary Left ShiftThe left operands value is moved left by the number of bits specified by the right operand.a << 2
>>Binary Right ShiftThe left operands value is moved right by the number of bits specified by the right operand.a >> 2

Logic Operators

In python we work with two Boolean values True and False.

keywordsignificance
andLogical AND operator
orLogical OR operator
notLogucal NOT operator
inmembership operator
isobject identity operator

Reserved Keywords

andexecnot
assertfinallyor
breakforpass
classfromprint
continueglobalraise
defifreturn
delimporttry
elifinwhile
elseiswith
exceptlambdayield

 

Read next: Decision statement