ProgFund Lect Week 2
ProgFund Lect Week 2
ProgFund Lect Week 2
Department of CS&IT
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"
RQ 18
Multiple variables in one line
You can assign the same value to multiple
variables in one line:
Example:
x = y = z = "Orange "
#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
RQ 29
Arithmetic Expressions
3 4 x 10( y 5)(a b c) 4 9
9(
x
)
5 x x y
is translated to
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