variables and expression
variables and expression
and Statements
By
Dr. Muhammad Irfan
Dr. Amir Rafique
Constant
• Fixed values such as
• Numbers,
• Letters
• And string
are called Constants because their values does not change
• Numeric Constants are as you expect
• String constants use single quotes (‘) or double quotes(“)
• Example
• Print (123)
123
• Print (98.6)
98.6
Print (“Hello World”)
Hello World
Reserved Words
You cannot use reserved words as variable names / identifiers
X = 12.2
X 12.2 100
Y = 14
X = 100 Y 14
Python Variable Name Rules
• Must start with a letter or underscore _
• Must consist of letters, numbers, and underscores
• Case sensitive
Good Name: Spam Eggs Spam23 _speed
Bad Name: 23spam #sign Var.12
Different spam Spam SPAM
Sentences or Lines
X = 2 Assignment Statement
x = 3.9 * x * ( 1 - x )
A variable is a memory location
used to store a value. The value x = 0.6
store in a variable can be updated
by replacing the old value ( 0.6)
with a new value ( 0.936)
x = 3.9 * x * ( 1 - x )
The right side is an expression.
One the expression is evaluated,
the result is placed in ( assigned
to ) the variable on the left side
(i.e., x)
Expressions
• Because of the lack of mathematical Operator Operations
symbol on computer keyboards, we + Addition
use “computer-speak” to express the
classic math operations - Subtraction
* Multiplication
• Asterisk is multiplication / Division
** Power
• Exponentiation (raise to a power)
looks different than in math % remainder
Numeric Expressions
>>> xx = 2 >>> jj 23 Operator Operations
>>> xx = xx + 2 >>> kk = jj % 5
>>> print (xx) >>> print (kk) + Addition
X = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence
• Remember the rules top to bottom
• When writing code use parentheses
• When writing code, keep mathematical expressions simple enough
that they are easy to understand
• Beak long series of mathematical operations up to make them more
clear
DMAS
What does “Type” Mean
• In Python variables literals and >>> ddd = 1+4
constant have a “type” >>> print (ddd)
• Python know the difference
between an integer number and a >>> eee = ‘Hello’ + ‘there’
string >>> print(eee)
• For example “+” mean “addition” if
something is a a number and
“concatenate” if something is a
string
Type Matters
>>> eee = ‘hello’ + there’
>>>eee= eee+ 1
• Python knows what “type Traceback (most recent call last): file
everything is “<stdin>”, line 1 in <module>
TypeError: can’t covert ‘int’ object to
• Some operations are prohibited str implicitly
• You cannot “add 1” to a string >>>type (eee)
<class ‘str’>
• We can ask Python what type >>>type (‘hello’)
something is by using the type () <class ‘str’>
function >>>type (1)
<class ‘int’>
Several Types of Numbers
• Numbers have two main types
• Integers are whole numbers:
• 14, -2, 0, 1, 100, 401233
• Floating Point Numbers have decimal parts: -2.5, 0.0, 98.6, 14.0
• There are other number types
• They are variations on float and integer
String Conversions
>>>sval = ‘123’
>>> type (sval)
• You can also use int() and float() to <class ‘str’>
convert between strings and >>> print (sval + 1)
integers >error
>>> ival = int(sval)
• You will get an error if the string >>>type (ival)
does not contain numeric >class ‘int’)
characters >>>print (ival + 1)
User Input
• We can instruct Python to pause >>>Name = input (Who are You?)
and read data from the user using >>>print (‘welcome to Python’, name)
the input() function