Pytho
Fundamentals of n
“” #
Python
()
Agenda
( ) #
First Program Comments
First Program
Python - Fundamental Concepts
Comments Indentation Statements
Python uses
● Single-line indentation to ● Empty
● Multi-line indicate a block of ● Simple
● Docstring code. ● Compound
COMMENT
wHY DO WE USE
COMMENTS#
Pytho
n
Computer ignores any text
“‘’ # written after the hash.
”
Comments are not computed.
cOMM
ENT
COMMENT
wHat DOes a
COMMENTS do?
● Makes the source code more readable
and understandable by the users.
● To Explain the logic of the code.
● Comments are generally ignored by
compilers and interpreters.
Python COMMENT Line
Types of
comment
# # ‘‘‘’’
’
Single-line Multi-line Doc string
COMMENT COMMENT
COMMENT
Single-line
comment
# ●
●
Starts with a hash symbol (#)
The hash sign must appear at the start of
each line comment.
COMMENT
multi-line
comment
#
Can write comments of multiple lines using
hash at the beginning of each line.
Example
# THIS IS A MULTILINE
# COMMENT IN PYTHON
# USING THE SINGLE LINE
# COMMENT CONSECUTIVELY
COMMENT
Doc string
‘‘‘’’’
● Comment spans within multiple lines.
● Ignores everything in quotation marks.
“““””” ●
●
Written using 3 signal or 3 double quotes.
This is called Doc strings.
Example
“““You can write docstring like this”””
‘‘‘You can also write docstring like this’’’
COMMENT
When to use
comment?
● Programming is often done by teams of people
‘‘‘’’ ●
●
Projects you work on will be inherited by other
people
Comments make it easier for you to read your
’ ●
code!
Make sure the comment is short and relevant to
the code
LMS Assignment
Indentation
Importance of INDENTATION
INDENTATIOn ERROR
Indentation
wHAT IS
INDENTATION?
● Indentation refers to the spaces at the
INDENTATION
beginning of a code line. ERROR
● Whitespace is used for indentation in
Python.
● Python uses indentation to highlight the
blocks of code.
● All statements with the same distance to
the right belong to the same block of
code.
Indentation
Look at the
meme
All statements with the same distance to
the right belong to the same block of
code.
If a block has to be more deeply nested,
it is simply indented further to the right.
Indentation
Example
Program
Indentation
Example
For
statement
If statement
LMS Assignment
Primary Data Type
.py .txt
Numbers Strings
Data Type
True/False
Boolean
Numbers
What are Numbers
Python has three built-in numeric data types:
integers, floats and complex numbers.
Numbers - Integers
What are Integers?
● Integers are whole numbers,
● They have no fractional parts
● They can be positive, negative, or zero.
● For example, 50, 0, -2 are treated as integers.
We can perform simple calculations with integers such as
addition in these examples.
>>> 6 + 4
10
>>> 1 + 2 + 4 + 10 - 3
14
>>> print(1 + 2 + 3 + 4 + 5 + 10)
25
Numbers - Float
What are Float?
● Float data types are similar to integers but will have
a decimal point.
● The decimal point can move or “float” to various
positions within the number.
● For example, -3.2, 0.0, 4.5652 are treated as floats.
We can perform simple calculations with floats such as
addition in these examples.
>>> 1.5+2.3
3.8
>>> 1.1+2.2+3.3+4.4+5.5
16.5
Numbers - Complex
What are Complex
numbers?
● Complex numbers are constituted by 2 numbers.
They are represented as “ x + yj ”.
● They consist of real and imaginary parts, where x is a
real number and y is an imaginary number.
● The real imaginary part can be an integer or floating
point.
In engineering and research, complex numbers are
extremely essential. For examples.
>>> 3 +6j
(3+6j)
LMS Assignment
stRing
What is a string?
● A string is a sequence of characters.
● Strings most often contain non-numeric characters.
● It can be letters, numbers, any special symbols such as
exclamation signs or percentages.
● Python recognizes both single quotes (') and double
quotes (") as valid ways to use ‘strings’.
Example
>>> “I Love Python Coding”
‘I Love Python Coding’
strings
+ For string
● The plus operator (+) works differently for strings;
● Consider the numerical number. It simply does the
‘’ addition of the given two numbers.
>>> 5 + 10
“” ●
15
Whereas, with the string it does the string
concatenation:
>>> '51' + ‘20'
'5120'
>>> ‘Python' + ‘Programming'
' PythonProgramming '
Boolean
What is boolean?
● Booleans represent one of the values: True or False.
● In programming, you often need to know if an
expression is True or False.
● You can evaluate any expression in Python, and get one
of two answers, True or False.
Example
print(10 > 9)
Answer: True
print(10 < 9)
Answer: False
LMS Assignment
statement
What are
statements?
Statements are simple codes written for execution
statement
Types of
Simple Statement Complex Statement
statements
● Comprised in a single line. ● A collection of statements
working together.
● Never affect the flow of
program. ● May affect the flow of
Empty Statement program.
● Example:
● Example:
a=10, break, return, break,
pass if, elif, else, for, while, try, etc.
Values and variable
What is a
variable?
● Variables are containers or Memory Placeholders for storing data
values.
● It's like a box holding a character, an alphanumeric value, or
another variable that you can change later.
● For example a = 5. Here a is the variable and 5 is the value.
● Variables can have values such as integers, characters, and strings.
● Such as a = 5.2, name = “Sam”, greeting = “Hello World!”
Values and variable
Restriction for Variable
name
● Must start with a letter or the underscore character
● Cannot start with a number
● Can only contain alphanumeric characters and underscores
(A-z, 0-9, and _ )
● Variable names are case-sensitive (age, Age and AGE are
three different variables)
Values and variable
Restriction for Variable
name - Examples
Valid Variable(s) Invalid Variable(s)
Num Number 1
NUM Num 1
Num1 1Num
_Num Num.1
Num_1 Program to do addition
IF if
ELSE else
Values and variable
Application
Calculate BMI using Python Code
# height, weight and bmi are variables
# in this program
height =1.79
weight = 68.7
bmi = weight / height * height
print(bmi)
LMS Assignment
Identifiers
What are
identifiers?
A Python identifier is a name used to identify a variable,
function, class, module or other object.
An identifier starts with:
● A letter A to Z or a to z
● An underscore (_) followed by zero or more letters
underscores and digits (0 to 9).
NOTE: A variable name is one example of an identifier.
Example : first_name, phone_91
Assignments
What are
Assignments?
Variable Name = Value
(or)
Variable Name = Expression
Example : a = 10
Assignment operators are used to assigning value to a
variable.
The name of the variable is on the left, and the value
assigned to it is on the right.
Types of Assignments
Assign multiple
Simple Assignment values to multiple
variables
Example Example
a=100 a, b = 100, 200
print(a) print(a)
100 100
print(b)
200
Types of Assignments
Assign different Assign same value
types of value to to multiple variables
multiple variables
Example
Example
a, b, c = 0.1, 100, ‘Python’
a, b , c = 2021
print(a)
0.1
# all the variables a, b and
print(b)
c will be assigned with
100
the same value 2021
print(c)
‘Python’
Types of Assignments
Assign different Assign same value
types of value to to multiple variables
multiple variables
Example
Example
a, b, c = 0.1, 100, ‘Python’
a, b , c = 2021
print(a)
0.1
# all the variables a, b and
print(b)
c will be assigned with
100
the same value 2021
print(c)
‘Python’
LMS Assignment
Keywords or reserved words
What are keywords?
Python has a set of keywords that are reserved
words that cannot be used as variable names,
function names, or any other identifiers.
Example
and = 10
Will give error
Keywords or reserved words
Categories of keywords
Value Keywords: True, False, None.
● Operator Keywords: and, or, not, in, is.
● Control Flow Keywords: if, elif, else.
● Iteration Keywords: for, while, break,
continue, else.
● Structure Keywords: def, class, with, as, pass,
lambda.
● Returning Keywords: return, yield.
● Import Keywords: import, from, as.
Built-in Function
What are keywords?
● As of now, there are 68 built-in functions that
Python can interpret.
● Many built-in functions support mathematical
operations.
● Many useful functions can be used to speed up
the coding process for a variety of tasks.
Built-in Function
Examples
Built-in
Description Code Output
Function
Returns absolute value of a
abs() number
abs(-11) Answer: 11
Returns quotient and
divmod() remainder of integer divmod(15, 2) Answer: (7, 1)
division
Returns the largest of the
max(100, 20, 400)
max() given arguments or items Answer: 400
in an iterable
Built-in Function
Built-in
Description Code Output
Function
Returns the smallest of the
min() given arguments or items min(100, 20, 400) Answer: 20
in an iterable
Raises a number to a
pow() power
pow(10, 2) Answer: 100
Rounds a floating-point Answer: 101
round() value round(100.56) round(100.46)
round(100.56) 100
Sums the items of an
sum() iterable
sum(10, 20, 30) Answer: 60
LMS Assignment