0% found this document useful (0 votes)
12 views34 pages

ProgFund Lect Week 2

Here are the steps to solve the expressions based on operator precedence rules: i) 3 + 4 * 4 + 5 * (4 + 3) - 1 = 3 + 16 + 5 * 7 - 1 = 3 + 16 + 35 - 1 = 53 ii) 2 * 2 - 3 > 2 or 4 - 2 > 5 = 4 - 3 > 2 or 2 > 5 = 1 > 2 or 2 > 5 = False or False = False iii) 2 % 2 + 2 * 2 - 2 / 2 = 0 + 4 - 1 = 3

Uploaded by

Mohi Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views34 pages

ProgFund Lect Week 2

Here are the steps to solve the expressions based on operator precedence rules: i) 3 + 4 * 4 + 5 * (4 + 3) - 1 = 3 + 16 + 5 * 7 - 1 = 3 + 16 + 35 - 1 = 53 ii) 2 * 2 - 3 > 2 or 4 - 2 > 5 = 4 - 3 > 2 or 2 > 5 = 1 > 2 or 2 > 5 = False or False = False iii) 2 % 2 + 2 * 2 - 2 / 2 = 0 + 4 - 1 = 3

Uploaded by

Mohi Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Programming Fundamentals

Department of CS&IT

Variable and Operators


Multiple Print() statement in python
print("Hello ”)
print(“Python ”)
print(“World”)
print(“Sir Syed
”+”University”)
Output
Hello
Python
World
Sir Syed
Using end= option in print() statement
end= in print() statement prints upcoming text
on the same line rather than printing on the
newline.
print("Hello”,end=“”
) print(“Python”)
print(“World”)

Output
HelloPytho
n World
Comments in Python
 Comments are use for documentation and
better understand of source in native language
Single Line Comments in Python
# Prints Hello Python World on to
screen print("Hello Python world!!!")
Multi-Line Comments in Python
Triple double quotations (“””) or Triple single
quotations (‘’’) can be
use for Multi-Line comment in python
You can comment multiple lines as follows −
“”” ‘’’
Comments Comments
“”” ‘’’
4
Escape Sequences in Python
Escape Sequences are use for formatting text
in print() statement.
Type of escape sequences are \n,\t,\b,\r,\’,\”
print(“Hello\nWorld”)
Hello
World
print(“Hello\tWorld”) >> Hello World (8 spaces)
print(“Hello\bWorld”) >> HellWorld (one space back)
print(“Hello \’World\’ ”) >> Hello ‘World’ (single quote)
print(“Hello \”World\” “) >> Hello “World”(double quotes)
(N5 OTE:These escape sequences can be use in end= option in
Python Literals
 Literalscan be defined as the data is given to
the variables and constants
 There are many types of Literal in the
Python:
 Numeric Literals
 String literals
 Boolean literals
 Special literals

6
Numeric Literals
 Numeric Literals are
immutable(unchangeable).
 Numeric literals can belong to 3 different
numerical types:
integer , float , complex
Example: how to use Numeric
literals in Python
a = 0b1010 #Binary
Literals
b = 103 #Decimal Literal

#Float Literal
 float_1 = 10.5
 float_2 = 1.5e2
#Complex
Literal
 x = 3.14j
String literals
A string literal is a sequence of characters
surrounded by quotes.
 We can use both single, double or
triple quotes for a string.
 A character literal is a single character
surrounded by single or double
quotes.
Example: how to use String literals
in Python
 strings = “This is Python”
 char = "C"
 multiline_str = """This is a multiline string
with
more than one line code.""“
Boolean literals
A Boolean literal can have any of the
two values:True or False.
 Example: How to use Boolean literals in
Python:
Special literals
 Python contains one special literal i.e. None.
We use it to specify that the field has not
been created.
 Example: How to use Special literals in
Python:
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.
Rules for Python variables:
 A variable name must start with a letter or the
underscore character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )
 Variable names are case-sensitive (age, Age and AGE are
three different variables)
 In computer programming CamelCase or camelCase is
often used as a naming convention for naming compound
words variables, arrays, and other elements e.g maxNumber or
MaxNumber, midTerm or MidTerm, fullName, FullName.
Assigning Values to Variables
 Python variables do not need obvious
declaration to reserve memory
space.
 The declaration happens automatically when
you assign a value to a variable.
 The equal sign (=) is used to assign values
to variables.
variable_name = assign value(Literals)
E.g. : number = 10
name = “John”
Example
#Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

#Illegal variable names:


2myvar = "John"
my-var = "John"
my var =
"John"
Reserved words
 You can not use reserved words as
variable names / identifiers.
and , del ,for , is , raise , assert , elif ,
from lambda , return , break , else ,
global , not , try ,class , except ,
if , or ,while Continue, exec, import ,
pass, yield,def , finally, in , print

RQ 18
Multiple variables in one line
 You can assign the same value to multiple
variables in one line:
Example:
 x = y = z = "Orange "

x,y,z = " Orange " , " Banana " , " Apple


" print(x)
print(y
)
print(z)
RQ 19
Constants
 A constant is a type of variable whose value cannot
be changed.

Assigning value to a constant:


 Constants are usually declared and assigned on a module,
module means a new file containing variables, functions etc.
which is imported to main file. Or also declared same existing
file.

Example: Declaring and assigning value to a constant


#Create a constant.py
 PI = 3.14
 GRAVITY = 9.8
Example
 Objective: Compute the area of a
circle.= 3
radius # Assign 3 to variable radius
PI=3.14 # declare a constant

#compute the area


area = PI * (radius * radius)

#Display result
print("The area of circle is" ,area,” sq cm”)
Python Data Types
 In programming, data type is an
important concept.
 Variables can store data of different types,
and different types can do different things.
 Python has the following data types built-in by
default, in these categories:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Boolean Type: bool
Getting the Data Type
 You can get the data type of any object
by using the type() function:
Example:
#Print the data type of the variable
x: x = 5
print(type(x))
Operators
 Operators are used to perform operations on
variables and values.
Python divides the operators in the
following groups:
 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Membership operators
 Bitwise operators
Arithmetic Operators
Arithmetic operators are used with numeric
values to perform common mathematical
operations:
Operator Name Example
+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y
Relational/Comparison operators
 Comparison operators are used to
compare two values:
Operator Name Example

== Equal x==y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or x >= y


equal to
<= Less than or equal to x <= y
Logical Operators
 Logical operators are used to
combine conditional statements:
Operator Name Example

and Return True if x < 5 and x < 10


both statements are true
or Return True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns not (x<5 and x< 10)
False if the result is true
Assignment Operators
 Assignment operators are used to
assign values to variables:
Operator Example Same As
= x=5 x=5
+= x += 3 x= x+3
-= x -= 3 x= x-3
*= x *= 3 x= x*3
/= x /= 3 x= x/3
%= x %= 3 x= x%3
**= x **= 3 x = x ** 3
Scientific Notation
 Floating-point literals can also be specified
in scientific notation, for example,
 1.23456e+2, same as 1.23456e2, is
equivalent to 123.456, and
 1.23456e-2 is equivalent to 0.0123456.
E (or e) represents an exponent and it can
be either in lowercase or uppercase.

RQ 29
Arithmetic Expressions
3  4 x 10( y  5)(a  b  c) 4 9 
  9( 
x
)
5 x x y

is translated to

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)


Order of Evaluation
 When we string operators together - Python
must know which one to do first
 This is called “operator precedence”
 Which operator “takes precedence” over
the others.
x = 1 + 2 * 3 - 4 / 5 ** 6

RQ 31
Operators Precedence Rules
• Highest precedence rule to
lowest precedence rule
• Parenthesis are always respected Parenthesis
Power
• Exponentiation (raise to a power) Multiplication
Addition
• Multiplication, Division, and Left to Right
Remainder
• Addition and Subtraction
• Left to right
1 + 2 ** 3 / 4 * 5

1+8/4*5 Parenthesis
Power
Multiplication
Addition
1+2*5 Left to Right

1 + 10

11 >>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11
>>>
Exercise (Homework)
Perform the following with Operators
Precedence Rules:
i) 3 + 4 * 4 + 5 * (4 + 3) – 1
ii) 2 * 2 - 3 > 2 or 4 – 2 > 5
iii) 2 % 2 + 2 * 2 - 2 / 2

You might also like