Python_Lecture_2(Variables & Exprission)
Python_Lecture_2(Variables & Exprission)
• 2.1 Introduction
• 2.2.1 Variables
• 2.5 Expressions
• 2.8 Summary
• 2.9 References
Lecture Objectives
After reading through this chapter, you will be able to :
INTRODUCTION
• Variables in a computer program are not quite like mathematical variables. They are placeholders for
locations in memory.
• Memory values consists of a sequence of binary digits (bits) that can be 0 or 1, so all numbers are
• Python is case sensitive, so myVariable is not the same as Myvariable which in turn is not the same
as MyVariable.
• With some exceptions, however, the programmer should avoid assigning names that differ only by
• These values belong to different types: 2 is an integer, and 'Hello, World!' is a string, so-called because it contains a
“string” of letters. You can identify strings because they are enclosed in quotation marks.
• If you are not sure what type a value has, the interpreter can tell you.
• Not surprisingly, strings belong to the type str and integers belong to the type int. Less obviously, numbers with a
decimal point belong to a type called float, because these numbers are represented in a format called floating-point.
• What about values like '17' and '3.2'? They look like numbers, but they are in quotation marks like strings.
• The above example makes three assignments. The first assigns a string to a new variable
named message, the second gives the integer 17 to n, the third assigns the (approximate)
value of π to pi.
• A common way to represent variables on paper is to write the name with an arrow pointing to
the variable’s value. >>> type(message) >>> type(n) >>> type(pi)
Python Programming : Lecture Two
Dr. Khalil Alawdi 7
• Variable names can be arbitrarily long. They can contain both letters and numbers, but they
have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to begin
variable names with a lowercase letter.
• The underscore character, _, can appear in a name. It is often used in names with multiple
words, such as my_name or airspeed_of_unladen_swallow.
• If you give a variable an illegal name, you get a syntax error: >>> 76mumbai= 'big city' Syntax
Error: invalid syntax
• >>> more@ = 1000000 Syntax Error: invalid syntax
• >>> class = 'Advanced python' Syntax Error: invalid syntax
Python Programming : Lecture Two
Dr. Khalil Alawdi 8
• It turns out that class is one of Python’s keywords. The interpreter uses keywords to
recognize the structure of the program, and they cannot be used as variable names.
• Python has a lot of keywords. The number keeps on growing with the new features coming in
python.
• Python 3.7.3 is the current version as of writing this book. There are 35 keywords in Python
3.7.3 release.
• We can get the complete list of keywords using python interpreter help utility.
• $ python3.7
• >>> help () help> keywords
Python Programming : Lecture Two
Dr. Khalil Alawdi 9
• Here is a list of the Python keywords. Enter any keyword to get more help.
TYPE CONVERSION
• The process of converting the value of one data type
(integer, string, float, etc.) to another data type is called
type conversion.
TYPE CONVERSION
• Implicit Type Conversion
• In Implicit type conversion, Python automatically converts one
data type to another data type. This process doesn't need any
user involvement.
TYPE CONVERSION
• Implicit Type Conversion
• Example 1:
• Converting integer to float
• num_int = 123
• num_flo = 1.23
• num_new = num_int + num_flo
• print ("datatype of num_int:”, type(num_int))
• print ("datatype of num_flo:”, type(num_flo))
• print ("Value of num_new:”, num_new)
• print ("datatype of num_new:”, type(num_new))
• When we run the above program, the output will be:
• datatype of num_int: <class int>
• datatype of num_flo: <class ‘float’>
• Value of num_new: 124.23
TYPE CONVERSION
• Implicit Type Conversion
• Example 1:
• In the above program, we add two variables num_int and num_flo, storing
the value in num_new.
• We will look at the data type of all three objects respectively.
• In the output, we can see the data type of num_int is an integer while the
data type of num_flo is a float.
• Also, we can see the num_new has a float data type because Python
always converts smaller data types to larger data types to avoid the loss
of data.
Python Programming : Lecture Two
Dr. Khalil Alawdi 14
TYPE CONVERSION
• Implicit Type Conversion
• Example 2:
• num_str = “456”
• print(num_int+num_str)
TYPE CONVERSION
• Implicit Type Conversion
• Example 2:
• When we run the above program, the output will be:
• Data type of num_int: <class ‘int’>
• Data type of num_str: <class ‘str’>
• Trace back (most recent call last):
• File "python", line 7, in <Module>
• Type Error: unsupported operand type(s) for +: 'int' and 'str'
• In the above program, we add two variables num_int and num_str.
• As we can see from the output, we got Type Error. Python is not able to use Implicit Conversion in
such conditions.
• However, Python has a solution for these types of situations which is known as Explicit
Conversion.
TYPE CONVERSION
• Explicit Type Conversion
• In Explicit Type Conversion, users convert the data type of an object to required
data type. We use the predefined functions like int(), float(), str(), etc to perform
explicit type conversion.
• This type of conversion is also called typecasting because the user casts
(changes) the data type of the objects.
• Syntax:
• <required_datatype> (expression)
• Typecasting can be done by assigning the required data type function to the expression
TYPE CONVERSION
• Explicit Type Conversion
• Example 1
• Addition of string and integer using explicit conversion.
• num_int = 123
• num_str = "456"
• print (“Data type of num_int:”, type(num_int))
• print (“Data type of num_str before Type Casting:”, type(num_str)) num_str = int(num_str)
• print (“Data type of num_str after Type Casting:”, type(num_str))
• num_sum = num_int + num_str
• print (“Sum of num_int and num_str:”, num_sum)
• print (“Data type of the sum:”, type(num_sum))
TYPE CONVERSION
• Explicit Type Conversion
• Example 1
• When we run the above program, the output will be:
• Data type of num_int: <calss ‘int’>
• Data type of num_str before Type Casting: <class ‘str’>
• Data type of num_str after Type Casting: <class ‘int’>
• Sum of num_int and num_str: 579
• Data type of the sum: <class ‘int’>
• In the above program, we add num_str and num_int variable.
• We converted num_str from string(higher) to integer(lower) type using int() function to perform the addition.
• After converting num_str to an integer value, Python is able to add these two variables.
• We got the num_sum value and data type to be an integer.
• Example: 4 + 5 = 9 Here 4 and 5 are Operands and (+), (=) signs are the operators.
• Arithmetic Operators.
• Relational Operators.
• Logical Operators.
• Membership Operators.
• Identity Operators
• >>> 10+20 30
• >>> 20-10 10
• >>> 10*2 20
• >>> 10/2 5 print(round(22/3,3))
• >>> 10%3 1
• >>> 2**3 8
• >>> 10//3 3
• >>>5!=6 True
• b=20
• list= [10,20,30,40,50]
• if (a in list):
• print (“a is in given list”)
• else:
• print (“a is not in given list”)
• if (b not in list):
• print (“b is not given in list”)
• else:
• print (“b is given in list”)
• Output:
• >>>
• a is in given list
• b is given in list
• b=20
• if (a is b):
• print (“a, b has same identity”)
• else:
• print (“a, b is different”)
• b=10
• if (a is not b):
EXPRESSIONS
• An expression is a combination of values, variables, and operators.
• A value all by itself is considered an expression, and so is a variable, so the following are all
legal expressions (assuming that the variable x has been assigned a value):
• 17
• x
• x + 17
• A statement is a unit of code that the Python interpreter can execute. We have seen two kinds
of statement: print and assignment.
you can test bits of code in interactive mode before you put them in
a script. But there are differences between interactive mode and
script mode that can be confusing.
• For example, if you are using Python as a calculator, you might type
• But if you type the same code into a script and run it, you get no
output at all.
• For example
• print 1
•x=2
• print x produces the output 1 2
ORDER OF OPERATIONS
• 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:
• Parentheses have the highest precedence and can be used to force an expression to
evaluate in the order you want. Since expressions in parentheses are evaluated first,
• 2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can also use parentheses to make an
expression easier to read, as in (minute * 100) / 60, even if it doesn’t change the result.
• Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3,
not 27.
ORDER OF OPERATIONS
• Multiplication and Division have the same precedence, which is higher than Addition and
Subtraction, which also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
• Operators with the same precedence are evaluated from left to right (except exponentiation).
So, in the expression degrees / 2 * pi, the division happens first and the result is multiplied by pi.
To divide by 2π, you can use parentheses or write degrees / 2 / pi.
• Example for Operator Precedence
• >>> 200/200+(100+100) 201.0
• >>>
• >>> a=10
• >>> b=20
• >>> c=15
• >>> (a+b)*(c+b)-150 900
• >>> (a+b)*c+b-150 320 >>> a+b**2 410
Python Programming : Lecture Two
Dr. Khalil Alawdi 36
ORDER OF OPERATIONS
• >>> a or b + 20 10
• >>> c or a + 20 15
• >>> c and a + 20 30
• >>> a and b + 20 40
• >>> a>b>c False
SUMMARY
• In this chapter we studied how to declare variables, expression and types of
variables in python.
• We are more focuses on type conversion of variables in this chapter basically
two types of conversion are implicit type conversion and explicit type
conversion.
• Also studied types of operators available in python like arithmetic, logical,
relational and membership operators.
• Focuses on interactive mode and script mode in python and order of
operations.
Python Programming : Lecture Two
Dr. Khalil Alawdi 38
REFERENCES
• Think Python by Allen Downey 1st edition.
• Python Programming for Beginners By Prof. Rahul E. Borate, Dr. Sunil Khilari,
• https://learning.rc.virginia.edu/
• www.programiz.com
• www.itvoyagers.in