Variables and
Assignments
Unit 1, Lesson 3
a
r
a
c
Lesson 3: Variables and
t
e
r
s
Assignments
r
e
Variables in Python:
q
u
i
r
e
d
f
o
r
•In Python, a variable is a reserved memory location used
s
e
a
to store values.
r
c
h
•Python being a dynamically typed language, there is no
•0
need to2
:
declare variables or declare their types before
using them.
2
3
•Python has no command for declaring a variable. A
variable is created the moment a value is assigned to it.
•The equal-to (=) operator is used to assign value to a
variable.
a
r
a
c
Lesson 3: Variables and
t
e
r
s
Assignments
r
e
q
u
i
r
e
d
• Python interpreter automatically determines the type of the data,
f
o
based on the data assigned to it.
r
s
e
a
r
c
h
•0
• In Python, we can even change the type of the variable after it has
2
:
been set once (or initialized with some other type). This can be done
2
3
by assigning a value of a different type to the variable.
• A variable in Python can hold any type of value
like 12 (integer), 560.09 (float), "CodeTantra" (string) etc.
Lesson 3: Variables and
Assignments
• Associating a value with a variable using the assignment operator (=) is
called as binding.
In Python, when you assign a value to a variable, you're actually creating
a reference to that value, not making a copy.
Python uses reference semantics, which means that variables
essentially point to objects rather than containing the objects themselves.
We cannot use Python variables without assigning any value.
If we try to use a variable without assigning any value then, the Python
interpreter shows an error saying "name is not defined".
Lesson 3: Variables and
Assignments
• Python variables do not need explicit declaration to reserve memory
space.
•
The declaration happens automatically when you assign a value to a
variable.
•
The equals to sign (=) is used to assign values to variables.
•
The operand to the left of the = operator is the name of the variable
and the operand (or expression) to the right of the = operator is the
value stored in the variable.
Lesson 3: Variables and
Assignments
In Python, assignment statements do not return a value.
Chained assignment is recognised and supported as a
special case of the assignment statement.
a = b = c = x is called a chained assignment in which the
value of x is assigned to multiple variables a, b, and c.
Expressions, and Statements,
Indentation
Unit 1, Lesson 4
Understanding Expressions
• An expression is a combination
of values(Constants), variables and operators.
An expression may also include call to
functions and objects.
Operators are symbols that represent particular
actions. Operands participate in the action performed
by the operator.
Understanding Expressions
• Any expression evaluates to a single value, which
becomes the value of the expression.
Instructions that a Python interpreter can execute are
called statements. For example, a = 1 is an assignment
statement.
Understanding Statements
A statement in Python is a logical instruction which can be read and executed by Python
interpreter.
In Python, we have different kinds of statements :Print statements
1. Assignment statements
2. Selective statements
3. Iterative statements
4. Control Flow statements
5. Function declaration statements
The purpose of Python's assignment statement is to associate variables with values in your
program.
It is the only statement that does not start with a keyword.
An assignment statement contains at least one equal sign (=), to the left hand side of = we have
a variable and to the right hand side we have an expression.
Types of Assignment Statements
There are two types of assignment
statements in Python. They are:
1. Basic assignment statements
2. Augmented assignment statements
Indentation
• Python utilizes white spaces to define control flow blocks. This concept
is inherited from its predecessor, ABC. Instead of relying on punctuation
or keywords, Python uses indentation to signify the scope of a block of
code.
The first line of the function definition is called the header, the rest is
called the body.
The header has to end with a colon (:) and the body has to be indented.
By convention, the indentation is always four spaces or one Tab
space.
The body can contain any number of statements.
Interpreter returns Indentation Error when we are not providing correct
Indentation.
Data types - Numbers and Strings
Unit 1, Lesson 5
Understanding types of Data Types
• Computer programming is all about processing data. In computer
programming, the data is always represented in the binary form
(0's and 1's), i.e. groups of Bits called as Bytes.
In the real world, we come across different types of data like age of a
person(integer), number of rooms in a house (integer),
price(floating-point number), height (floating-point number),
Names of people, places and things (strings), inventory list (list) etc.
Data types categorize data for efficient processing in Python and all
programming languages. They assign specific characteristics to data,
enhancing accuracy and control in operations.
What does it determine ?
The data type of the variable determines :
• possible values it can be assigned.
• possible operations that can be performed.
• (Arithmetic operations can be applied on numeric data and not strings)
• The format in which it is stored in the memory.
• The amount of memory allocated to store the data.
Python built in data types
Some of the built-in data types supported in Python are:
1. Number
2. String
3. List
4. Tuple
5. Set
6. Dictionary
type() function in Python is used to know
which datatype of
value the variable holds.
Numbers
We have three different categories of numbers in Python. They are
1. int
2. float
3. complex
Int
It stands for integer. This Python data type stores signed integers.
In Python an integer can be of any length, with the only limitation
being the available memory.
Numbers
We have three different categories of numbers in Python. They
are
1. int
2. float
3. complex
float
• It stands for floating-point numbers. This Python data type
stores floating-point real values.
• For example : An int can only store the number 20, but float can
also store numbers with decimal fractions like 20.25.
Numbers
We have three different categories of numbers in Python. They
are
1. int
2. float
3. complex
complex
• It stands for complex numbers. A complex number is a
combination of a real number and an imaginary number.
• It takes the form of a + bj Here, a is the real part and b*j is the
imaginary part.
String
• In Python, a string is a sequence of characters
enclosed inside a pair of single quotes(‘) or double
quotes(“). Even triple quotes (''') are used to represent
multi-line strings.
The computer doesn’t see letters at all. Every letter you
use is represented by a number in memory.
For example, the letter A is actually the number 65. This
is called encoding. There are two types of encoding for
characters – ASCII and Unicode.
String
ASCII uses 8 bits for encoding whereas Unicode uses 32
bits. Python uses Unicode for character representation.
An individual character within a string is accessed using
an index.
Index starts from 0 to n-1, where n is the number of
characters in the string.
Python allows negative indexing in strings. The index of -
1 refers to the last item in the string, -2 refers to
the second last item and so on.
Strings
Ways of creating strings in Python are:
1. Using single quotes (‘ ‘)
• If single quote (') is part of the string, then the characters of the string are enclosed
between double quotes(").
• If single quote (') is part of the string, and the characters are enclosed between single
quotes, then a backslash (\) should be added before (') of the string.
2. Using double quotes (" ")
3. Using triple double quotes(""" """)
We can also use triple single quotes(''' ''') to create multi-line strings in Python.
Using triple single quotes or triple double quotes, we can also create single line strings