VARIABLES, EXPRESSIONS
AND STATEMENTS
Agenda
What is variable, How to declare and use a variable
Re-declare a variable
Concatenate variable
Delete a variable
Global and local variable
Keywords,statements,Expressions
Python Operators
Identifiers
Literals
Variable
Variables are nothing but reserved memory
locations to store values. This means that
when you create a variable you reserve some
space in memory.
Based on the data type of a variable, the
interpreter allocates memory and decides
what can be stored in the reserved memory
Variables and Keywords
Variables names can start with letters or
underscore , but not with numbers.
Keywords cannot be used as variables
Python reserves 33 keywords for its Use :
Assigning Values to Variables
The declaration happens automatically when
you assign a value to a variable. The equal
sign (=) is used to assign values to variables
Type of a variable
Re-declare a variable
You can re-declare the variable even after you
have declared it once.
Concatenate Variable
Let's see whether you can concatenate different data
types like string and number together
For example, we will concatenate “Focus" with number
“40".
Unlike Java, which concatenates number with string
without declaring number as string, Python requires
declaring the number as string otherwise it will show a
TypeError
Example
Delete a variable
You can also delete variable using the
command del "variable name"
Global and Local Variable
Global Variable:- when you want to use the same
variable for rest of your program or module you declare
it global variable
Local Variable:- while if you want to use the variable in
a specific function or method you use local variable.
Example
Explanation
Variable "f" is global in scope and is assigned
value 101 which is printed in output
Variable f is again declared in function and
assumes local scope. It is assigned value "I
am learning Python." which is printed out as
an output. This variable is different from the
global variable "f" define earlier
Once the function call is over, the local
variable f is destroyed. At line 12, when we
again, print the value of "f" is it displays the
value of global variable f=101
Statements
A statement is an instruction that the
Python interpreter can execute. We have seen
two kinds of statements: print and
assignment
Expressions
An expression is a combination of values,
variables, and operators
Operators and operands
Operators are special symbols that
represent computations like addition and
multiplication etc
The values the operator uses are
called operands.
Example: C=A+B
The operators +, -, *, / and ** perform
addition, subtraction, multiplication, division
and exponentiation
Here A ,B, C are Operands
Operators
Continue…
Order of precedence
When more than one operator appears in an
expression, the order of evaluation depends on
the rules of precedence. For mathematical
operators, Python follows mathematical convention.
The acronym PEMDAS is a useful way to
remember the rules:
Paranthesis, Exponentiation,Multiplication,Division,
Addition,Subtraction
Identifiers
Identifier is the name given to entities like
class, functions, variables etc. in Python.
It helps differentiating one entity from
another.
Rules For writing Identifiers
Identifiers can be a combination of letters in
lowercase (a to z) or uppercase (A to Z) or
digits (0 to 9) or an underscore (_). Names
like myClass, var_1 and print_this_to_screen,
all are valid example.
An identifier cannot start with a digit. 1
Variable is invalid, but variable1 is perfectly
fine.
Keywords cannot be used as identifiers.
We cannot use special symbols like !, @, #, $,
% etc. in our identifier.
Python Literals
Literals can be defined as a data that is given
in a variable or constant
String literals:String literals can be formed
by enclosing a text in the quotes. We can use
both single as well as double quotes for a
String.
Eg:"Aman" , '12345‘
Numeric literals:Numeric Literals are
immutable.
Types of Strings
There are two types of Strings supported in
Python:
a)Single line String- Strings that are
terminated within a single line are known as
Single line Strings.
Ex: a=’apssdc’
b)Multi line String- A piece of text that is
Continue…
There are two ways to create Multiline Strings:
1) Adding black slash at the end of each line
2)Using triple quotation marks
Numeric literals
Numeric Literals are immutable. Numeric
literals can belong to following four different
numerical types.
Boolean and Special literals
Boolean literals:A Boolean literal can have
any of the two values: True or False
Special literals:Python contains one special
literal i.e., [Link] is used to specify to
that field that is not created. It is also used
for end of lists in Python.
Literal Collections
Literal Collections:Collections such as tuples,
lists and Dictionary are used in Python.
List:List contain items of different data types.
Lists are mutable i.e., modifiable.
Tuple: A tuple is a sequence of immutable
python objects
Dictionary: Python dictionary is an unordered
collection of items. While other compound data
types have only value as an element, a dictionary
has a key: value pair.
Lists
List contain items of different data types.
Lists are mutable i.e., modifiable.
The values stored in List are separated by
commas(,) and enclosed within a square
brackets([]). We can store different type of
data in a List.
Value stored in a List can be retrieved using
the slice operator([] and [:]).
The plus sign (+) is the list concatenation and
asterisk(*) is the repetition operator.
Continue…
Tuple
A tuple is a sequence of immutable python
objects
Dictionaries
Python dictionary is an unordered collection
of items.
Continue…..