0% found this document useful (0 votes)
34 views

Unit1 Topic Variables, Expressions and Statements

Variables allow us to store and name values in Python. They have no type and no need to be declared. A variable is assigned a value using the = operator. Statements contain instructions while expressions combine values and operators. The tuple assignment feature allows assigning values from a tuple on the right to variables in a tuple on the left. This can be used to swap variables or unpack values from containers like lists and strings.

Uploaded by

thamizhvani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Unit1 Topic Variables, Expressions and Statements

Variables allow us to store and name values in Python. They have no type and no need to be declared. A variable is assigned a value using the = operator. Statements contain instructions while expressions combine values and operators. The tuple assignment feature allows assigning values from a tuple on the right to variables in a tuple on the left. This can be used to swap variables or unpack values from containers like lists and strings.

Uploaded by

thamizhvani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit 1

Topic - Variables
By
Ms.T.R.Thamizhvani
VARIABLES:

• A variable allows us to store a value by assigning it to a name,


which can be used later.
• Named memory locations to store values.
• Programmers generally choose names for their variables that
are meaningful.
• It can be of any length. No space is allowed.
• We don't need to declare a variable before using it. In Python,
we simply assign a value to a variable and it will exist.
Assigning value to variable:
 
Value should be given on the right side of assignment
operator(=) and variable on left side.
• >>>counter =45
• print(counter)
• Assigning a single value to several variables simultaneously:
• >>> a=b=c=100
• Assigning multiple values to multiple variables:
• >>> a,b,c=2,4,"ram"
Unit-1
Topic – Statements and Expressions
STATEMENTS AND EXPRESSIONS:

Statements:
• Instructions that a Python interpreter can executes are called
statements.
• -A statement is a unit of code like creating a variable or displaying a
value.
• >>> n = 17
• >>> print(n)
• Here, The first line is an assignment statement that gives a value to n.
• The second line is a print statement that displays the value of n.
Expressions:
An expression is a combination of values, variables, and operators.

A value all by itself is considered an expression, and also a variable.So the


following are all legal expressions:
• >>>  42
• 42
• >>>  a=2
• >>>  a+3+2
• 7
• >>>  z=("hi"+"friend")
• >>>  print(z)
• Hifriend
Topic – Tuple Assignment
TUPLE ASSIGNMENT
 
• An assignment to all of the elements in a tuple using a single assignment statement.
• Python has a very powerful tuple assignment feature that allows a tuple of variables
on the left of an assignment to be assigned values from a tuple on the right of the
assignment.
• The left side is a tuple of variables; the right side is a tuple of values.

Each value is assigned to its respective variable.
• All the expressions on the right side are evaluated before any of the assignments.
This feature makes tuple assignment quite versatile.

Naturally, the number of variables on the left and the number of values on the right
 

have to be the same.


• >>> (a, b, c, d) = (1, 2, 3, “Hi”)
  

• Value Error: need more than 3 values to unpack


Example:

• It is useful to swap the values of two variables. With conventional assignment


statements, we have to use a temporary variable. For example, to swap a and b:
 
• Swap two numbers
• a=2;b=3
• print(a,b)
• temp = a
• a=b
• b = temp
• print(a,b)
• Output:
• (2, 3)
• (3, 2)
• >>> 
Tuple assignment solves this problem neatly:
• (a, b) = (b, a)
One way to think of tuple assignment is as tuple packing/unpacking.

In tuple packing, the values on the left are ‘packed’ together in a tuple:
• >>> b = ("George", 25, "20000")    # tuple packing

In tuple unpacking, the values in a tuple on the right are ‘unpacked’ into the variables/names on the right:
• >>> b = ("George", 25, "20000") # tuple packing
• >>> (name, age, salary) = b # tuple unpacking
• >>>   name
• 'George'
• >>>   age
• 25
• >>>   salary
• '20000'

The right side can be any kind of sequence (string, list, tuple)
Example:

To split an email address in to user name and a domain


• >>>     mailid='[email protected]'
• >>>     name,domain=mailid.split('@')
• >>>     print (name)
• god
• print (domain)
• abc.org

You might also like