Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces...
Transcript of Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces...
![Page 1: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/1.jpg)
SIP: Artificial Intelligence & Machine Learning Module 1: Introduction to Python
By : Ankit Vijayvargiya
1
![Page 2: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/2.jpg)
Brief Introduction of Python :
Python is a programming language created in 1991 by Guido van
Rossum.
General-purpose language as well as scripting language.
Interpreted
Increasingly popular
2
![Page 3: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/3.jpg)
Why Python ????
Python is Open Source Language.
It works on different platform (Windows, Mac, Linux, Raspberry Pi
etc.)
Python has syntax that allows developers to write programs with fewer lines
than some other programming languages.
Python has a simple syntax similar to the English language.
3
![Page 4: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/4.jpg)
Python vs Other Language :
On an average Python code is smaller than JAVA/C++ codes by 3.5 times.
Python is interpreted while C/C++, etc. are compiled.
• Compiler : spends a lot of time analysing and processing the program,
faster program execution
• Interpreter : relatively little time is spent analysing and processing the
program, slower program execution
4
![Page 5: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/5.jpg)
5
How to write a program in jupyter ???
![Page 6: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/6.jpg)
6
How to write a program in jupyter ???
![Page 7: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/7.jpg)
7
How to write a program in jupyter ???
![Page 8: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/8.jpg)
8
Python First Program: Print Hello World
C++ code:
#include <iostream>
int main( )
{
cout << "Hello, World!";
return 0;
}
C Code
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
![Page 9: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/9.jpg)
9
Python First Program: Print Hello World
![Page 10: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/10.jpg)
10
1. Print(‘write any message’) or
Print(“write any message”)
Print in Python:
2. Print(Item1 , Item2)
![Page 11: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/11.jpg)
11
Print in Python:
3. print('The value of x is {} and y is {}'.format(5,10))
4. print("%s %s" %('hello', 'world'))
![Page 12: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/12.jpg)
12
Exercise
Write a program(WAP) to print your name two
times.
![Page 13: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/13.jpg)
13
Exercise
WAP to print the following string :
"Twinkle, twinkle, little star, How I wonder what you are! Up above
the world so high, Like a diamond in the sky. Twinkle, twinkle, little
star, How I wonder what you are"
![Page 14: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/14.jpg)
14
Exercise
WAP to print the following string in the given
format : "Twinkle, twinkle, little star, How I wonder what you are! Up above
the world so high, Like a diamond in the sky. Twinkle, twinkle, little
star, How I wonder what you are"
![Page 15: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/15.jpg)
15
Exercise
WAP to print the following string in the given
format : "Twinkle, twinkle, little star, How I wonder what you are! Up above
the world so high, Like a diamond in the sky. Twinkle, twinkle, little
star, How I wonder what you are"
![Page 16: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/16.jpg)
16
Comments in Python:
For single line comment hash (#) symbol is used.
For multi-line comments use triple quotes, either "' or """.
![Page 17: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/17.jpg)
17
Python Indentation:
Python uses indentation instead of braces to determine the scope of
expressions.
All lines must be indented the same amount to be part of the scope (or
indented more if part of an inner scope).
This forces the programmer to use proper indentation since the indenting is
part of the program.
![Page 18: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/18.jpg)
18
Variables in Python:
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)
Reserved words such as break, for,
continue, etc. can't be used as
variable's names.
In Python, multiple assignments
can be made in a single statement
as follows:
![Page 19: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/19.jpg)
19
User Input in Python:
![Page 20: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/20.jpg)
20
User Input in Python:
![Page 21: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/21.jpg)
21
User Input in Python:
![Page 22: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/22.jpg)
22
Exercise
Write a Python program which accepts the radius of a circle from the user
and compute the area.
![Page 23: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/23.jpg)
23
Exercise
Write a Python program which accepts the user‟s first and last name and print
them in reverse order.
![Page 24: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/24.jpg)
24
Control Flow in Python:
The if else statement is used in python for decision making.
if test expression:
body of if
else:
body of else
To test more than one condition following syntax is used- if test expression:
body of if
elif test expression:
body of elif
else:
body of else
![Page 25: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/25.jpg)
25
Control Flow in Python:
![Page 26: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/26.jpg)
26
Python Basic Operators:
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise operators
Membership Operators
Identity Operators
![Page 27: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/27.jpg)
27
Arithmetic Operators:
![Page 28: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/28.jpg)
28
Arithmetic Operators:
![Page 29: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/29.jpg)
29
Arithmetic Operators:
![Page 30: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/30.jpg)
30
Comparison Operators:
![Page 31: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/31.jpg)
31
Comparison Operators:
![Page 32: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/32.jpg)
32
Comparison Operators:
![Page 33: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/33.jpg)
33
Logical Operators:
![Page 34: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/34.jpg)
34
Logical Operators:
![Page 35: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/35.jpg)
35
Bitwise Operators:
![Page 36: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/36.jpg)
36
Membership Operators:
in and not in are the membership operators in Python. They are used to test whether
a value or variable is found in a sequence (string, list, tuple, set and dictionary).
![Page 37: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/37.jpg)
37
Identity Operators:
is and is not are the identity operators in Python. They are used to check if two values
(or variables) are located on the same part of the memory.
![Page 38: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/38.jpg)
38
Basic Datatypes:
Integers (default for numbers)
Z=5
Floats
Z=5.1
Strings : strings are enclosed within either single quotes or („ „) or double quotes (“ “).
Z = „python‟
Z = “python”
![Page 39: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/39.jpg)
39
Python List:
List is an ordered sequence of items. It is one of the most used datatype in Python
and is very flexible. All the items in a list do not need to be of the same type.
# empty list
mylist = []
# list of integers
mylist = [1, 2, 3]
# list with mixed datatypes
mylist = [1, "Hello", 3.4]
# nested list
mylist = ["mouse", [8, 4, 6], ['a']]
![Page 40: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/40.jpg)
40
Accessing List Element:
![Page 41: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/41.jpg)
41
Slicing Operator for Python List:
![Page 42: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/42.jpg)
42
Add Element to Python List:
1. append:
![Page 43: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/43.jpg)
43
Add Element to Python List:
2. insert: 3: extend
![Page 44: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/44.jpg)
44
Add Element to Python List:
4. + operator:
![Page 45: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/45.jpg)
45
Delete Element from Python List:
![Page 46: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/46.jpg)
46
Python Tuple:
A tuple is similar to a list. The difference between the two is that we cannot
change the elements of a tuple once it is assigned whereas in a list, elements
can be changed.
Advantages of Tuple over List
• Since tuple are immutable, iterating through tuple is faster than with
list. So there is a slight performance boost.
![Page 47: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/47.jpg)
47
Creating a Tuple:
![Page 48: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/48.jpg)
48
Assessing Element in a Tuple:
![Page 49: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/49.jpg)
49
Changing a Tuple:
Tuples are immutable which means you cannot update or change the values of tuple
elements. You are able to take portions of existing tuples to create new tuples
![Page 50: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/50.jpg)
50
Built in function with Tuple and list: Creating List and Tuple: Maximum Value (max):
Length (len): Minimum Value (min):
![Page 51: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/51.jpg)
51
Built in function with Tuple and list:
Sorting (sorted):
Summation (sum):
![Page 52: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/52.jpg)
52
Python Dictionaries:
Dictionary is an unordered collection of key-value pairs. It is generally used
when we have a huge amount of data. Dictionaries are optimized for retrieving
data.
Creating a Dictionaries:
![Page 53: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/53.jpg)
53
Access Element from a Dictionary:
Change /Add and Delete in a Dictionaries:
![Page 54: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/54.jpg)
54
Python Nested Dictionary:
![Page 55: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/55.jpg)
55
Python Nested Dictionary:
![Page 56: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/56.jpg)
56
While Loop:
while loop is used to execute a block of statements repeatedly until a given a
condition is satisfied.
when the condition becomes false, the line immediately after the loop in
program is executed.
Syntax :
while expression:
Body of while
![Page 57: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/57.jpg)
57
For Loop:
A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
![Page 58: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/58.jpg)
58
For Loop:
![Page 59: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/59.jpg)
59
break:
The break statement terminates the loop containing it. Control of the program
flows to the statement immediately after the body of the loop.
If break statement is inside a nested loop, break will terminate the inner most
loop.
![Page 60: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/60.jpg)
60
Continue:
The continue statement of is used to skip the rest of the code inside the loop
for the current iteration only.
Loop does not terminate but continues on with next iteration.
![Page 61: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/61.jpg)
61
Functions:
In python, function is a group of related
statements that perform a specific task.
Syntax:
def functionname(parameters):
statements
Keyword def marks the start of function header.
Parameters through which we pass value to a
function. They are optional.
A colon (:) to mark the function of header.
An optional return
![Page 62: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/62.jpg)
62
Assignments
![Page 63: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/63.jpg)
63
Assignment – 1
Print the following pattern
1
2 2 3 3 3 4 4 4 4 5 5 5 5 5
![Page 64: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/64.jpg)
64
Assignment – 1
Print the following pattern
1
2 2 3 3 3 4 4 4 4 5 5 5 5 5
![Page 65: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/65.jpg)
65
Assignment – 2
WAP to print the first N Fibonacci numbers. N should be user
defined.
0, 1, 1, 2, 3, 5, 8, 13, 21,…………………
![Page 66: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/66.jpg)
66
Assignment – 2
WAP to print the first N Fibonacci numbers. N should be user
defined.
0, 1, 1, 2, 3, 5, 8, 13, 21,…………………
![Page 67: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/67.jpg)
67
Assignment – 3
Calculate the Area of triangle, given the sides are 5,6,7
![Page 68: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/68.jpg)
68
Assignment – 3
Calculate the Area of triangle, given the sides are 5,6,7 Use Heron's formula with sides a, b and c
s=(a+b+c)/2
Area= (s(s−a)(s−b)(s−c))^0.5
![Page 69: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/69.jpg)
69
Assignment – 3
Calculate the Area of triangle, given the sides are 5,6,7
![Page 70: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/70.jpg)
70
Assignment – 4
Find Armstrong Number From 100 to 2000
![Page 71: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/71.jpg)
71
Assignment – 4
Find Armstrong Number From 100 to 2000
An Armstrong number of three digits is an integer such that the sum of the cubes
of its digits is equal to the number itself.
For example, 371 is an Armstrong number since 3*3*3 + 7*7*7 + 1*1*1= 371.
![Page 72: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/72.jpg)
72
Assignment – 4
Find Armstrong Number From 100 to 2000
![Page 73: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/73.jpg)
73
Assignment – 5
Print all Prime Numbers from 100 to 1000
![Page 74: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/74.jpg)
74
Assignment – 5
Print all Prime Numbers from 100 to 1000
A prime number is a whole number greater than 1 whose only factors are 1 and
itself.
The first few prime numbers are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29…………..
![Page 75: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/75.jpg)
75
Assignment – 5
Print all Prime Numbers from 100 to 1000
![Page 76: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/76.jpg)
76
Assignment – 6
Write a program to find the sum of Natural Number given
by the User ?
Natural Numbers : all non negative numbers
0, 1, 2, 3, 4, 5, 6, 7, 8, ………………………….
![Page 77: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/77.jpg)
77
Assignment – 6
Write a program to find the sum of Natural Number given
by the User ?
![Page 78: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/78.jpg)
78
Assignment – 7
Write a program to Accept two int values from user and
return their product. If the product is greater than 1000,
then return their sum.
![Page 79: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/79.jpg)
79
Assignment – 7
Write a program to Accept two int values from user and
return their product. If the product is greater than 1000,
then return their sum.
![Page 80: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/80.jpg)
80
Assignment – 8
Write a program to check if the input that is given by the
user is a Leap Year.
Leap Year: a year, occurring once every four years, which has 366 days
including 29 February as an intercalary day.
![Page 81: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/81.jpg)
81
Assignment – 8
Write a program to check if the input that is given by the
user is a Leap Year.
![Page 82: Module 1: Introduction to Python · Python Indentation: Python uses indentation instead of braces to determine the scope of expressions. All lines must be indented the same amount](https://reader036.fdocuments.in/reader036/viewer/2022062507/5fc4f28c977a260f0830bfa1/html5/thumbnails/82.jpg)
82
THANK YOU