Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

variables and expression

The document discusses the fundamentals of variables, expressions, and statements in Python, including the definition of constants, reserved words, and the rules for naming variables. It explains how to assign values to variables, the importance of operator precedence, and the distinction between different data types such as integers and strings. Additionally, it covers user input and type conversions in Python.

Uploaded by

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

variables and expression

The document discusses the fundamentals of variables, expressions, and statements in Python, including the definition of constants, reserved words, and the rules for naming variables. It explains how to assign values to variables, the importance of operator precedence, and the distinction between different data types such as integers and strings. Additionally, it covers user input and type conversions in Python.

Uploaded by

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

Variables, Expressions,

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

False class return is finally

None if for lembda Continue

True def from while nonlocal

and del global not with

as elif try or yield

assert else import pass

break except in raise


Variables
• A variables is a named please in the memory where a programmer
can store data and later retrieve the data using the variable “name”
• Programmers get to choose the names of the names of the variables
• You can change the contents of a variable in later statement

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 = X + 2 Assignment with Expression

print Print Statement


Mnemonic Variable Names
• Since we programmers are given a choice in how we choose our
variable names, there is bid of “ best Practice’
• We name variables to help us remember what we intent to store in
them “mnemonic” = “ memory aid”)
• This can confuse beginning programmers because well-named
variables often “sound” so good that they must be keywords
Code 1 Code 2 Code 3

xlq3z9ocd =35.0 a= 35.0 hours = 35.0

xlq3z9afd 12.50 b = 12.50 rate =12.50

xlqrp9afd= xlq3z9ocd * xlq3z9afd c= a * b pay = hours * rate

print (xlqrp9afd) print ( c ) print (pay)


Assignment statements
• We assign a value to a variable using the assignment statement ( = )
• As assignment statement consists of an expression on the right-hand
side and a variable to store the result

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

>>>yy = 440 * >>> print (4**3) - Subtraction


12
>>> print (yy) * Multiplication
/ Division
>>> zz = yy /
1000 ** Power
>>> print (zz)
% remainder
Order of Evaluation
• When we use string operators together with numeric , python must
know which on to do first

• This is called “operator precedence”


• Which operator “takes precedence” over the others?

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

• The input() function returns a


string

You might also like