01 Appendix 1 Python and Anaconda
01 Appendix 1 Python and Anaconda
Method 3:
The third way of executing a Python program is to use an IDE. IDLE is the Python supported
IDE. Open the IDLE and we can see the Python shell opened. Use the File tab to open the
program. Then use the Run tab to execute. The program output can be seen in the console.
Variables
In Python, when values are assigned to a variable, the variable holds the reference of the
object or value. Python is also a type-inferred language, so we don’t need to explicitly define
the variable type. The type is automatically inferred by the value.
Example:
num1 = 15
num2 = 15.15
ch = ‘a’
str = “string”
print (num1)
print (num2)
print (str)
print(ch)
Literals
Literals are constant values of built-in data types.
i. String literals
Literal Collections
There are four types of literal collections called List literals, Tuple literals, Dictionary literals,
and Set literals.
List literals are enclosed within square brackets.
List_Items = ["I1", "I2", "I3"]
Tuple literals are enclosed within brackets.
Tuple_Items = (“I1”, “I2”, “I3”)
Dictionary literals are enclosed within curly braces.
gender = {'m':'male', 'f':'female'}
Set literals are enclosed within curly braces.
animals = {“cat, “dog”, “rabbit”}
print(List_Items)
print(Tuple_Items)
print(gender)
print(animals)
Operators
Python supports arithmetic operators, assignment operators, comparison operators, logical
operators, bitwise operators and special operators.
Arithmetic Operators:
Operator Description
+ Binary addition: Adds two operands
- Binary Subtraction: Subtracts two operands
* Binary Multiplication: Multiplies two operands
/ Binary division: Divides two operands
% Modulus: Returns the remainder
** Exponent: a**b is equal to ab
// Floor Division: Returns the quotient and rounds the decimal value
Comparison Operators:
Operator Description
== Returns true if LHS operand is equal to RHS operand
!= Returns true if LHS operand is not equal to RHS operand
> Returns true if LHS operand is greater than the RHS operand
< Returns true if LHS operand is less than the RHS operand
>= Returns true if LHS operand is greater than and equal to RHS operand
<= Returns true if LHS operand is less than and equal to RHS operand
Logical Operators:
Operator Description
and Returns true if LHS expression and RHS expression are true
or Returns true if LHS expression or RHS expression are true
not Takes single expression and returns the complement of it.
Special Operators:
Operator Description
is Identity operators
is not
in Membership operators
not in
Data types
Variables can hold values of any data type and depending on the type of value; the memory
will be allocated by the interpreter.
Numerical data type can be int, float or complex
String data type
Boolean data type takes only two values “True” and “False”.
type() keyword
type() is used to find the data type of a variable.
Structure of a program
A Python program is constituted of code blocks that are executed as a unit. These code blocks
can be a module, a function body, or a class definition. The main module for a Python script
is called __main__.
Control Statements
1. if statement
x =10
y =20
if x>y:
print("if stmt executed")
2. if…else statement
x =10
y =20
if x>y:
print("if stmt executed")
else:
print("else stmt executed")
3. if…elif…else statement
x = 40
y = 20
z = 50
if x>y and x>z:
print("x is greater")
elif y>x and y>z:
print("y is greater")
else:
print("z is greater")
Looping statements
1. while:
x = 10
while x>5:
print(x)
x = x-1
print("while loop executed")
3. for:
x=5
for i in range(x):
print(x)
print("for loop executed")
Built-in range() function is used in the for statement to iterate over a sequence of numbers.
It defines the start, stop and step size as range(start, stop,step_size). step_size defaults to 1 if
not provided.
Like while statement, for statement can also have an optional else clause.
pass Statement
The pass statement can be used when a statement is required syntactically but the program
requires no action.
Import statement
We can import Python modules . For example, Import a module numpy as
import numpy
Arrays
Arrays are used to store multiple values of homogeneous type in one single variable. We can
access an array element by referring to the index number.
names = [“Sam”, “Ayden”, “John”, “Paul”]
Defining Functions
The keyword def is used to define a function definition. The keyword is followed by the
function name and the parenthesized list of formal parameters. The statements within the
body of the function start at the next line, and must be indented. The return statement returns
a value from a function.
def add():
a=5
b=7
print(a+b)
class classname:
block of statements
Example:
class student:
def __init__(self, regno, name):
self.regno = regno
self.name = name
def display(self):
print(self.regno)
print(self.name)
s1 = student(227616, "Sam" )
print(s1.regno)
print(s1.name)
s1.display();
An __init__() method is used for initiating a newly-created class instance. The first argument
to this method is self.
Class object is created using function notation.
obj = Myclass()
Attributes are referenced using dot operator.
obj.attribute1
Data Structures
Python provides built-in data structures such as: lists, tuples, dictionaries, strings, sets and
frozensets.
Lists, strings and tuples are ordered sequences of objects. Strings can contain only characters
whereas lists and tuples can contain any type of objects. Lists and tuples are like arrays.
Conda is a special package manager. One can open the Anaconda command prompt and can
use either pip command or conda install for installing additional packages as:
>> conda install ‘package name’
There are many packages available in special channels. They can be installed as:
>> conda install -c channel name package
One can create a virtual environment and install packages. This helps to create a separate
environment for say deep learning programs or packages of certain versions that may be
necessary. The commands that can be used are given below: