Python for beginners part I

Saroj Humagain
5 min readOct 7, 2018

Python is an open source, high-level and object-oriented programming language. It was created in the year 1991 by the Dutch programmer Guido Van Rossum. He named python after a sitcom “Monty Python Flying Circus”. Python is very popular and the reason behind its popularity is its clear syntax, easy to learn and readability. Some even suggest that Python is the most famous programming language of 2018. If we compare Python with other programming languages like Java and C ++, we’ll find that same thing can be done in one line in python whereas other programming language takes multiple lines. For eg., java takes four line just to take inputs from the users whereas python can do it on one line.

why python is so popular?

  • Easy and simple to learn: Python is very easy to learn since its syntax is very clear and readable.
  • Free and open source: Python is free to use and it has the enormous users community to help you out.
  • High-level language: Python is a high-level language, its syntax is very familiar to English. So even a non-programmer can understand the code.
  • Supports different programming paradigm: Python supports all of the following programming paradigms
    1. OOP(Object Oriented Programming)
    2. POP(Procedure Oriented Programming)
    3. Functional Oriented Programming
    4. Imperative Oriented programming
  • Portable: Python is portable, you can install and use it in Windows(in any windows), Linux and MacOs.

Setting up the environment

You can download python from its official website. There are two versions of python, Python2, and Python3. I recommend you to download Python3. Follow the several steps to install and you are ready to go.

After installing python, you will have three options. Either you can go with the Pythons command line interface(CLI) and write your code on it or you can use any of the text editors like a notebook or you can just install a Python IDE(Integrated development environment). IDE will give you a proper programming environment which combines for a coding facility with a debugging environment. I highly recommend Jupyter notebook, which is a web-based architecture and it will allow you to write your Python code on it. , Also use Anaconda distribution which includes Python, Jupyter notebook and other commonly used packages for starting with python, scientific computing, data science, and machine learning.

Download anaconda from its official webpage. Follow the several steps to install. When the download is completed, open anaconda navigator you’ll see the list of several IDEs. Launch Jupyter notebook and your notebook will open under the localhost.

Python variable and their data types

Assigning a value to a variable in Python is very easy. You don’t have to add any data types before a variable, you just have to specify the name of a variable.

a = 15

So, depending on the data Python will automatically detect the data type of a variable, which in above example is an integer.

name = “Saroj” #string data type
height = 5.4 #float data type

For Multiple assignment, suppose I have two variables a = 5 and b = 5. We can assign them as

a=b=5

If multiple variables have different values, we can assign as follows

a,b,c = 1,5,9

Python Tokens

Python tokens is a basic component of the source code whose characteristics are categorized as one of the five classes or the tokens that describe their functions:

  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators
  1. Keywords:
    Keywords are special reserved words that convey a special meaning to compiler/interpreter. Each keyword has a special meaning and a specific operation. Keywords cannot be used as variables. Eg def, class, else, as, and, None etc.
  2. Identifiers:
    Identifiers are the names to identify a variable, function, class or an object.num = 10
    Here num is identifying a variable, so num is an identifier.

There are several rules for naming an identifier. They are listed below:

  • No special character, except underscore(_) can be used
  • Keyword should not be used as an identifier name
  • Python is case sensitive. ie Var and var are two different identifiers.
  • Identifiers must start with either alphabet or underscore but not digit.
_name = “Saroj” #legal
_12name = “Saroj” #legal
12name = “Saroj” #Illegal

3. Literals
Literals are data given in a variable or constant. There are four types of literals in python. They are

  • String literals: String literals are those written within either single quote or in multiple quote.
eg. ‘Hello world’ 
“Hello world”

You can also use a triple quote to show them in multiline.

str = ‘’’ first line 
second line
third line ‘’’
print(str)
Output
first line
second line
third line
  • Numeric literals: The value of an integer is not restricted by the number of bits and can expand to the limit of the available memory. No special arrangement is required for storing large numbers.
int: eg 100, -54
long: eg 25486975L
float: eg 5.12, -2514
complex: eg 3.14j
  • Boolean literals: Boolean literals can have only two values.
    Either True or False.
  • Special literal: In Python, there is only one special literal. ie None and used to specify to the field that is not created
eg. 
val1 = 23
val2 = None
print(val2)
output
None

4. Operators
Operators are some specific characters having some specific tasks and functions to perform. Based on the function, operators have been further divided into seven different operators.

  • Arithmetic operators: Takes two operand and performs an operation on them. Arithmetic operators are +, -, *, / and %
  • Assignment operators: Assignment operators are used to assign a value to a variable. Assignment operators are = , +=, -= , *=
a+= 10 is equivalent to a = a+10
  • Comparison operators: Comparision operators are used to compare two values and return either True or False as an output. Comparison operators are <, >,!=,==
  • Logical operators: Logical operators are used to perform some logical calculations. They are and, or and not.
eg. 
a = 7>6 and 2>1
print(a)
output
True
  • Bitwise operators: Bitwise operators are used to perform bitwise calculations. They are &, |, >>, << and ~
eg.
a = 7|5
print(a)
output
7

First, it converts given numbers in binary and does or operation on them.
111+101 gives 111.
Similarly,
7&5 gives 5
10>>2 ( Shifts two bits to right), initially binary equivalence of 10 is 1010 and after shifting two bits to right it becomes 0010, which is 2. So, 10>>2 gives 2.

  • Identify operators: Identify operators are used to find out if the two operands share an identity. They are is and is not.
eg.
x = 5
x is 5
output
True
  • Membership operators: Membership operators are used to test whether a value is a member of a sequence. Sequence may be a list, a string or tuple. Membership operators are in and not in.
eg.
animals = [‘cat’ , ‘dog’ , ‘snake’ , ‘cow’]
‘snake’ in animals
output
True

We can also check a particular word present in a string.

eg.
‘ro’ in ‘Saroj’
output
True

I will write the second part as well. If you don’t want to miss it follow me.

--

--

Saroj Humagain

I basically write on data science, ML and AI and sometimes random things.