Getting Started With Python
Getting Started With Python
Python is a popular programming language due to its liveliness and pleasant productivity. It offers
programming fun while coding. It was developed by Guido Van Rossum in February 1991 and
further developed by the Python software foundation. It is a general-purpose programming
language. Its syntax is easy and designed to give importance to the readability of code. It saves a
few lines of codes with easy syntax rules. It is a powerful programming middle-level language
but not so high-level language like C, C++, Java, etc.
Strengths –Python
1) Easy to use: The syntax of python is very simple to use compared to other programming
languages.
2) Fewer Lines code: It offers few lines of code, a programmer can write multiple lines of in
a single line.
3) Use of interpreter: Python is using an interpreter, not a compiler that executes code line by
line. So it is easy for a beginner.
4) No additional library required: When python is installed, all basic libraries installed with
it. So no need to install any standard library.
5) Platform Independent: Python can run on any platform such as Windows, Linux/UNIX,
Macintosh etc. Moreover, it can run on many devices like supercomputers, smartphones.
It is a portable language.
6) Free & Open Source: It’s free of cost and can be downloaded from www.python.org. It is
open-source, so source code may be available for redistribution.
7) Offers a variety of applications: Nowadays Python is used in many devices or applications
such as Scripting, Web Development, Gaming, AI, Database Development etc.
Limitations –Python
1) Slow execution: Python uses an interpreter that executes code line by line which is slower
in execution compared to a compiler-based programming language.
2) Less no. of libraries: Python offers limited libraries compared to other programming
languages like C, C++, etc.
3) Not strong for mobile development: It is good for desktop or server-based applications but
not efficient for mobile apps.
4) Memory Consumption: Python offers flexible data types so there is high memory
consumption.
5) Weak in database connectivity: It is not that much strong like JDBC or ODBC, its database
access layer has some barriers.
Python Installation
Before starting programming with python, it needs to be installed. There are few distributions of
python available like CPython (set of applications such as python interpreter, Python IDLE and
Pip), Anaconda Python distribution (Comes with preloaded packages and libraries with IDEs like
Spider, Jupyter, PyCharm etc.)
Python interpreter offers two modes for coding:
1) Interactive mode
2) Script mode
Working in Interactive Mode
Interactive mode allows one to type one statement and executes the same line when the enter key
is pressed. To start working in interactive mode follow these steps:
Step1. Click on Start → All Programs → Python 3.7 →IDLE (Python 3.7 64-bit).
A python Shell window will appear as shown in the below-given screen:
After typing a line or statement press enter, the interpreter will execute the line and gives the
result:
Step 5. It prompts to save the module. Save the file with .py extension.
Operators
An operator is used to perform specific mathematical or logical operation on values.
Operand
The values that the operator works on are called operands.
For example,
>>> 10 + num,
/ Gives quotient, when one value is divided by the other. Example 9/ 6 is 1.5
// (floor Gives integer part of quotient, when one value is divided by the other. Example
division) 9//6 is 1.
% (Remainder) Gives remainder when one value is divided by the other. Example 9 % 6 is 3
Assignment Operator
Operator Description
Add and Assign, Add the R-Value to the L-Value and assign result to L-value. Example
+= x=20, x += 30 it means x = x + 30 so the new value of x is 50.
Subtract and Assign, Subtract the R-Value from L-Value and assign result to L-value.
-= Example x=50, x -= 30 it means x = x – 30 so the new value of x is 20.
Multiply and Assign, Multiply the R-Value with L-Value and assign product to L-value.
*= Example x=50, x *= 3 it means x = x * 3 so the new value of x is 20.
Divide and Assign Quotient, Divide the L-value with R-Value and assign quotient to L-
/= value. Example, x = 50, x /= 6 its means x = x / 6, and the new value of x is 8.33333……
Floor and assign, Divide the L-value with R-Value and assign integer quotient (floor
division) to L-value. Example, x = 50, x //= 6 its means x = x // 6, and the new value of
//= x is 8.
Divide and Assign Remainder, Divide the L-value with R-Value and assign remainder to
%= L-value. Example, x = 50, x %= 6 its means x = x % 6, and the new value of x is 2.
Exponent and Assign, Calculate (L-Value)R-Value and assign the result to L-value. Example,
**= x = 3, x**=2 its means x = x2 and the result of x is 9
Relational Operator
Operator Description
Compares two values for equality, Returns True if they are equal,
== (Equality) otherwise returns False.
Compares two values for inequality, Returns True if they are not equal,
!= (Not equal) otherwise returns False.
Compares two values, Returns True if first value is less than the second,
< (Less than) otherwise returns False.
Compares two values, Returns True if first value is greater than the second,
> (greater than) otherwise returns False.
<= (Less than or equal Compares two values, Returns True if first value is less than or equal to the
to) second, otherwise returns False.
>= (greater than or Compares two values, Returns True if first value is greater than or equal to
equal to) the second, otherwise returns False.
Logical Operator
Operator Description
not Negate a condition and Returns True if the condition is false otherwise returns False.
Combines two conditions and returns True if both the conditions are true, otherwise
and returns False.
Combines two conditions and returns True if at least one of the condition is true,
or otherwise returns False.
Bitwise Operator
Operator Description
The AND (&) operator compares two bits and generates a result of 1 if both
& (bitwise and) bits are 1 otherwise, it returns 0
The OR (|) operator compares tow bits and generate a result 1 if the bits are
| (bitwise or) complementary, otherwise it returns 0.
The EXCLUSIVE-OR (XOR) operator compares two bits and return 1 if either
^ (bitwise xor) of the bits are 1 and it gives 0 if both bits are 0 or 1
~ (bitwise
complement) The COMPLEMENT operator is used to invert all of the bits of the operand.
Operator Description
Returns True if the given value is available in the sequence (like sting, tuples, lists,
in dictionary, etc.) otherwise returns False.
Returns True if the given value is not available in the sequence (like sting, tuples, lists,
not in dictionary, etc.) otherwise returns False.
Identity Operator
Operator Description
Returns True, if both operand have same identity i.e. memory reference , otherwise return
is False
Returns True, if both operand have not same identity i.e. memory reference , otherwise
Is not return False
Operator Precedence :
Execution of operator depends on the precedence of operator.
Highest () Parentheses
↑ ** Exponentiation
| ~ Bitwise nor
| +, – Addition, Subtraction
| ^ Bitwise XOR
| | Bitwise OR
| <, <=, >, >=, <>, !=, ==, is, is not Relational Operators, Identity Operators
Lowest or Boolean OR
Expression
In Python, An expression is a valid combination of operators, literals and variables.
There are three types of Expression in Python, These are-
•Arithmetic Expressions :- combination of integers, floating-point numbers, complex number,
and arithmetic operators. Example ; 2+9, 9.2/3
•Relational Expression :- combination of literals and/or variables of any valid type and
relational operators . Example; x > y, 5 < 8, z == y
•Logical Expression:- combination of literals, variables and logical operators.
Example; t and q, p or g
•String Expression :- combination of string literals and string operators ( + -> concatenate,
and * -> replicate). Example;
“Hello” + “Anjeev” = “Hello Anjeev”
“Like” * 3 = “LikeLikeLike”
input() function - This function is used accept value from user. Let’s look in the following:
print() function
As I have used print() function in the above codes. It allows generating output on the screen.
The syntax of the print() function is as follows:
Basically python print() a new line when the print function is used in the program.
Datatypes in python
Numbers: It holds numeric values in a program. Python allows following built-in data types for
numbers:
Integer: A Numeric value without decimal places is considered as an integer. The inbuilt class
‘int’ represent these type of numbers. Integers can be of any length in python.
Ex. : 12345, -45465, 234, -456, 10, 0
Float: Real numbers having decimal or floating points are considered are float. Ex.: 567.90,
234.90, 3456.898989 etc. Python prints a large value post decimal places by default. Have a
look on the following code:
Complex Number: It has two parts: i) Real Number and ii) Imaginary Part. It is available in the
form of ‘x + yJ’ or ‘x + yj’, where x is a float number (real number), yJ is the imaginary part,
small letter j indicates the square root of an imaginary number -1. Example:
Sequence: It accepts values as in a sequence or specific patters. Python offers the following
sequence data types:
str (String): It is a sequence of characters that is a combination of letters, numbers, and special
symbols. A string is enclosed with a quotation. It can be enclosed with single, double, or triple
quotes. Single line text is enclosed either with single or double quotes whereas multi-line text is
enclosed with triple quotes.
Python also offers a special escape character sequence to print some non-graphic character as
following:
Implicit Conversion
Implicit conversion, also known as coercion, happens when data type conversion is done
automatically by Python and is not instructed by the programmer.