0% found this document useful (0 votes)
0 views

DSci-Lecture 02-w2-20240929-type of data - python

The document covers the types of data in data science, including categorical (nominal, ordinal, binary) and measurement data (discrete, continuous), along with their examples and significance in statistical analysis. It also provides an overview of Python programming, including environment setup, basic syntax, data structures (lists, tuples, dictionaries), and functions. The lecture emphasizes the importance of understanding data types for effective statistical analysis and programming in Python.

Uploaded by

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

DSci-Lecture 02-w2-20240929-type of data - python

The document covers the types of data in data science, including categorical (nominal, ordinal, binary) and measurement data (discrete, continuous), along with their examples and significance in statistical analysis. It also provides an overview of Python programming, including environment setup, basic syntax, data structures (lists, tuples, dictionaries), and functions. The lecture emphasizes the importance of understanding data types for effective statistical analysis and programming in Python.

Uploaded by

manchestermilf1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 134

Data Sciences

Lecture 2
Dr. Mohammed Marey

1
Agenda

Type of data
Python review 1
Python review 2

2
Types of data

• Categorical data
• Measurement data

3
Categorical Data

• The objects being studied are grouped into


categories based on some qualitative trait.
• The resulting data are merely labels or
categories.

4
Examples: Categorical Data

• Hair color
– blonde, brown, red, black, etc.
• Opinion of students about riots
– ticked off, neutral, happy
• Smoking status
– smoker, non-smoker

5
Categorical
data
classified as
Nominal,
Ordinal,
Categorical data
and/or
Binary
Nominal Ordinal
data data

Binary Not binary Binary Not binary

6
Nominal Data

• A type of categorical data in which objects


fall into unordered categories.

7
Examples: Nominal Data

• Hair color
– blonde, brown, red, black, etc.
• Race
– Caucasian, African-American, Asian, etc.
• Smoking status
– smoker, non-smoker

8
Ordinal Data

• A type of categorical data in which order is


important.

9
Examples: Ordinal Data

• Class
– fresh, sophomore, junior, senior, super senior
• Degree of illness
– none, mild, moderate, severe, …, going, going,
gone
• Opinion of students about riots
– ticked off, neutral, happy

10
Binary Data

• A type of categorical data in which there are


only two categories.
• Binary data can either be nominal or
ordinal.

11
Examples: Binary Data

• Smoking status
– smoker, non-smoker
• Attendance
– present, absent
• Class
– lower classman, upper classman

12
Measurement Data

• The objects being studied are “measured”


based on some quantitative trait.
• The resulting data are set of numbers.

13
Examples: Measurement Data

• Cholesterol level
• Height
• Age
• SAT score
• Number of students late for class
• Time to complete a homework assignment

14
Measurement
data classified as
Discrete or
Continuous
Measurement
data

Discrete Continuous

15
Discrete Measurement Data
Only certain values are possible (there are
gaps between the possible values).

Continuous Measurement Data


Theoretically, any value within an interval
is possible with a fine enough measuring
device.

16
Discrete data -- Gaps between possible
values

0 1 2 3 4 5 6 7
Continuous data -- Theoretically,
no gaps between possible values

0 1000

17
Examples: Discrete
Measurement Data
• SAT scores
• Number of students late for class
• Number of crimes reported to SC police
• Number of times the word number is used

Generally, discrete data are counts.

18
Exa
mples:
Continuous
Measurement
• Cholesterol level Data
• Height
• Age
• Time to complete a homework assignment

Generally, continuous data come from


measurements.

19
Who Cares?

The type(s) of data collected


in a study determine the type
of statistical analysis used.

20
For example ...

• Categorical data are commonly summarized


using “percentages” (or “proportions”).
– 11% of students have a tattoo
– 2%, 33%, 39%, and 26% of the students in
class are, respectively, freshmen, sophomores,
juniors, and seniors

21
And for example …

• Measurement data are typically summarized


using “averages” (or “means”).
– Average number of siblings Fall 1998 Stat 250
students have is 1.9.
– Average weight of male Fall 1998 Stat 250
students is 173 pounds.
– Average weight of female Fall 1998 Stat 250
students is 138 pounds.

22
Agenda

Type of data
Python review 1
Python review 2

23
Python review 1

Introduction to Python

24
Agenda

 Environment management and IDE setup


 Machine Learning Libraries in Python
 Python Basic Syntax
 Data-structures:
 List
 Tuples
 Dictionary
 Function
 File Handling
 Class

25
IDEs

• PyCharm
• Vim
• Eclipse with PyDev
• Sublime Text
• Emacs
• Komodo Edit
• Visual Code
• Visual Studio

26
Libraries

1. Numpy (the fundamental package for scientific computing with Python. It


mostly used for solving matrix problems).
2. Pandas (the most popular machine learning library written in python, for
data manipulation and analysis).
3. Matplotlib (used for Data Visualization purposes i.e plotting)
4. Scikit-Learn (library that provides a range of Supervised and
Unsupervised Learning Algorithms).

27
Libraries Installation

1. Open CMD
2. Write ‘Conda’ to make sure that the environment installed successfully.
3. Activate the environment ‘ Activate ml’
4. Write the following commands
I. Conda install numpy
II. Conda install matplotlib
III. Conda install -c anaconda pip
IV. Pip install pandas
V. Pip install scikit-learn

28
Agenda

 Environment management and IDE setup


 Machine Learning Libraries in Python
 Basic Syntax
 Data-structures:
 List
 Tuples
 Dictionary
 Function
 File Handling
 Class

29
BasicSyntax

Indentation is used in Python to delimit blocks. The number


of spaces is variable, but all statements within the same
block must be indented the same amount.

Common indentations uses 4 spaces or 2 spaces or tab

Error! 30
BasicSyntax

 Printing to the Screen:


 Reading Keyboard Input:
 Comments
• Single line:
• Multiple lines:

 Python files have extension .py


31
BasicSyntax
 Numbers are Immutable objects in Python that cannot change their values.
 There are three built-in data types for numbers in Python3:
• Integer (int)
• Floating-point numbers (float)
• Complex numbers: <real part> + <imaginary part>j (not used much in Python programming)

 Common Number Functions


Function Description
int(x) to convert x to an integer
float(x) to convert x to a floating-pointnumber
abs(x) The absolute value of x
cmp(x,y) -1 if x < y, 0 if x == y, or 1 if x >y
exp(x) The exponential of x: ex
log(x) The natural logarithm of x, for x>0
pow(x,y) The value of x**y
sqrt(x) The square root of x for x >0
32
BasicSyntax

 Python Strings are Immutable objects that


cannot change their values.

 You can update an existing string by (re)assigning a variable


to another string.

33
BasicSyntax

 Python does not support a character type; these are treated as


strings of length one.

 Python accepts single ('), double (") and triple (''' or """) quotes
to denote string literals.

34
BasicSyntax

 String indexes starting at 0 in the beginning of the string and


working their way from -1
at the end.

T = “Python”
T[-4] = ??
35
BasicSyntax

String Formatting

age = 15
myStr = “Hello, I am {age} years old”.format(age=age)
print(myStr)

36
BasicSyntax
 Common String Operators
Assume string variable a holds 'Hello' and variable b holds 'Python’

Operator Description Operation Output


+ Concatenation - Adds values on either side of the operator a+b HelloPython
* Repetition - Creates new strings, concatenating multiple copies of a*2 HelloHello
the same string
[] Slice - Gives the character from the givenindex a[1], a[-1] e, o

[ :] Range Slice - Gives the characters from the given range a[1:4] ell
in Membership - Returns true if a character exists in the givenstring ‘H’ in a True

37
BasicSyntax
 Common String Methods
Method Description
str.count(sub, beg= Counts how many times sub occurs in string or in a substring of string if
0,end=len(str)) starting index beg and ending index end are given.
str.isalpha() Returns True if string has at least 1 character and all charactersare
alphanumeric and False otherwise.
str.isdigit() Returns True if string contains only digits and Falseotherwise.
str.lower() Converts all uppercase letters in string to lowercase.
str.upper() Converts lowercase letters in string to uppercase.
str.replace(old, new) Replaces all occurrences of old in string with new.

str.split(str=‘ ’) Splits string according to delimiter str (space if not provided)


and returns list of substrings.
str.strip() Removes all leading and trailing whitespace of string.
str.title() Returns "titlecased" version of string.

 Common String Functions str(x) :to convert x to astring


len(string):gives the total length of thestring
38
Basic Syntax (Conditions)

 In Python, True and False are Boolean objects of class 'bool' and
they are immutable.
 Python assumes any non-zero and non-null values as True,
otherwise it is False value.
 Python does not provide switch or case statements as in other
languages.
 Syntax: if..elif..else statement
if statement if..else statement

39
Basic Syntax (Conditions)

Example:

40
Basic Syntax (Conditions)

 Using the conditional expression


Another type of conditional structure in Python, which is very convenient and easy to read.

41
Basic Syntax (Loops)

 The For Loop


range(N) is a method that generates a list of N numbers: [0, 1, 2,
…, N-1]

for i in range(5):
print (i)

42
Basic Syntax (Loops)

 The For Loop

43
Basic Syntax (Loops)

 The while Loop

44
Basic Syntax (Loops)
Loop ControlStatements
 break:terminates the loop statement and transfers execution to
the statement immediately following the loop.

45
Basic Syntax (Loops)
Loop ControlStatements

 continue: Causes the loop to skip the remainder of its body


and immediately retest its condition prior to reiterating.

46
Agenda

 Environment management and IDE setup


 Machine Learning Libraries in Python
 Basic Syntax
 Data-structures:
 List
 Tuples
 Dictionary
 Function
 File Handling
 Class

47
Lists

 A list in Python is an ordered group of items or elements,


and these list elements don't have to be of the same type.

 Python Lists are mutable objects that can change their


values.

 A list contains items separated by commas and enclosed


within square brackets.

 List indexes like strings starting at 0 in the beginning of the


list and working their way from -1 at the end.
48
Lists

 Similar to strings, Lists operations include slicing ([ ] and [:]),


concatenation (+), repetition (*), and membership (in).

 This example shows how to access, update and delete list


elements:

49
Lists

 Lists can have sublists as elements and these sublists may


contain other sublists as well.

50
Lists

 Common List Functions

Function Description
cmp(list1, list2) Compares elements of both lists.
len(list) Gives the total length of the list.
max(list) Returns item from the list with max value.
min(list) Returns item from the list with min value.
list(tuple) Converts a tuple into list.

51
Lists

 Common List Methods


Method Description
list.append(obj) Appends object obj to list
list.insert(index, obj) Inserts object obj into list at offset index

list.count(obj) Returns count of how many times obj occurs in


list
list.index(obj) Returns the lowest index in list that obj appears

list.remove(obj) Removes object obj from list


list.reverse() Reverses objects of list in place
list.sort() Sorts objects of list in place
52
Lists

 List Comprehensions
Each list comprehension consists of an expression
followed by a for clause.

 List comprehension

53
Tuples

 Python Tuples are Immutable objects that cannot be changed once they have been
created.
 A tuple contains items separated by commas and enclosed in parentheses instead of
square brackets.
 access

 Noupdate
 You can update an existing tuple by (re)assigning a variable to another tuple.
 Tuples are faster than lists and protect your data against accidental changes to thesedata.
 The rules for tuple indices are the same as for lists and they have the same operations,
functions as well.
 Towrite a tuple containing a single value, you have to include a comma, even though there
is only one value. e.g. t = (3, )

54
Dictionary

 Python's dictionaries are kind of hash table type which


consist of key-value pairs of unordered elements.
• Keys : must be immutable data types ,usually numbers or
strings.
• Values : can be any arbitrary Python object.
 Python Dictionaries are mutable objects that can change their
values.

55
Dictionary

 Example:
 ages_dict = {“Ramy”:1992, “Mona”:1996, “Ahmed”:2001}

 ages_dict[“Emy”] = 1998 # inserts a new key and value to


the dictionary

 ages_dict[“Ramy”] = 1993 # modifies the value for the key


“Ramy”

56
Dictionary
 This example shows how to access, update and delete dictionaryelements:

57
Dictionary
 The output:

58
Dictionary

 Common Dictionary Functions


• cmp(dict1, dict2) : compares elements of bothdict.
• len(dict) : gives the total number of (key, value) pairs in the dictionary.

 Common Dictionary Methods


Method Description
dict.keys() Returns list of dict's keys
dict.values() Returns list of dict's values
dict.items() Returns a list of dict's (key, value) tuple pairs
dict.get(key, default=None) For key, returns value or default if key not indict
dict.clear() Removes all elements of dict

59
Agenda

 Environment management and IDE setup


 Machine Learning Libraries in Python
 Basic Syntax
 Data-structures:
 List
 Tuples
 Dictionary
 Function
 File Handling
 Class

60
Functions

A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.

Defining a Function

• Function blocks begin with the keyword def followed by the function name and parentheses (
( ) ).
• Any input parameters or arguments should be placed within these parentheses. You can also
define parameters inside these parentheses.
• The first statement of a function can be an optional statement - the documentation string of
the function or docstring.
• The code block within every function starts witha colon (:) and is indented.
• The statement return [expression] exits a function, optionally passing back an expression to
the caller. A return statement with no arguments is the same as returnNone.

61
Functions
 Function Syntax

62
Functions

 Function Arguments
You can call a function by using any of the
following types of arguments:
• Required arguments: the arguments passed to
the function in correct positional order.
• Keyword arguments: the function call identifies
the arguments by the
parameter names.
• Default arguments: the argument has a default
value in the function declaration used when the
value is not provided in the function call. 63
Functions

• Variable-length arguments: This used when you need to process unspecified additional
arguments. An asterisk (*) is placed before the variable name in the function declaration.

64
Agenda

 Environment management and IDE setup


 Machine Learning Libraries in Python
 Basic Syntax
 Data-structures:
 List
 Tuples
 Dictionary
 Function
 File Handling
 Class

65
Python File Handling

66
File Handling

File Reading
file_path = "filename.txt"

with open(file_path, 'r') as fr:


Lines = fr.readlines()

with open(file_path) as fr:


for line in fr:
print (line)

with open(file_path) as fr:


for i in range(3):
l = fr.readline()

67
File Handling

File Reading

file_path = "filename.txt“

my_list = [1, 2, 3, "\nComputational Intelligence\n"]

with open(file_path,"w") as fw:


fw.writelines(str(my_list))

with open(file_path,"w") as fw:


for l in my_list:
fw.write(str(l))

68
Agenda

 Environment management and IDE setup


 Machine Learning Libraries in Python
 Basix Syntax
 Data-structures:
 List
 Tuples
 Dictionary
 Function
 File Handling
 Class

69
Python Classes

class Box:
def __init__(self, x1, x2, y1, y2, label):
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
self.label = label

def calc_area(self):
return abs(self.x2-self.x1)*abs(self.y2-
self.y1)

b = Box(10, 100, 10, 100, "car")


print (b.calc_area())
70
Python Classes

 Classvariable

 Classconstructor

71
Python Classes

 Data Hiding You need to name attributes with a double underscore prefix, and those
attributes then are not be directly visible to outsiders.

72
ClassInheritance

73
Python Reserved Words

A keyword is one that means something to the language. In other words, you can’t use a
reserved word as the name of a variable, a function, a class, or a module.All the Python
keywords contain lowercase letters only.

and exec not


assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield 74
Agenda

Type of data
Python review 1
Python review 2

75
Python review 2

Introduction to Python 3.X

76
Installing Python 3.X

• Python is an open source scripting language.


• It supports Object Oriented.
• Multi-purpose (Web, GUI, Scripting, etc.)
• Python is a case sensitive.
• You can download Python 3.7.2 from here and
Pycharm (IDE) from here.

77
Install Python

Run the exe for install Python then click on Install Now. When it finishes, you can
see a screen that says the Setup was successful. click on "Close".

78
Install PyCharm

Run the exe for install PyCharm. The setup wizard should have started. Click “Ne

79
How to create a new project?

80
Sample Run

81
How to install a Package

Packages are imported to use third party code.


(same as library usage)

You can install python package through:


• Pycharm Wizard
• Command Prompt

82
How to install a package using Pycharm
Wizard
Choose your Python Version

83
How to install Package using CMD
1) Run CMD and redirect to python path
2) Type CD Scripts
3) Type pip install PackageName

84
A Code Sample

85
Enough to
understand the
code
• Assignment uses = and comparison uses ==
• For numbers + - / % are as expected.
Special use of + for string concatenation.
Special use of % for string formatting (as with printf in C).
• Logical operators are words (and, or, not).
• The basic printing command is print(parameter).
• The first assignment to a variable creates it.
Variable types don’t need to be declared.
Python figures out the variables types on its own.

86
Basic Data-Types

• Integers
Z=5

• Floats
X = 3.456

• Strings
Can use “” or ‘’ to specify.
“abc” or ‘abc’ (Same thing).

87
Whitespace - Indentation

• Whitespace is meaningful in Python: especially


indentation and placement of newlines.

• No braces { } to mark blocks of code in Python.

• Use consistent indentation instead.


The first line with less indentation in outside of block.
The first line with more indentation starts a nested block.

88
Whitespace - Indentation

#Works Fine
if 5 > 2:
print("Five is greater than two!")

#Python will give you an error if you skip the


indentation:
if 5 > 2:
print("Five is greater than two!")

89
Comments

Start comment with # the rest of the line will be ignored.

For Multi-Line comment use treble quotes


""" """.
def myFunction():
""" this is my function
Function does ."""
pass

90
Assignment Operator

• An assignment operator assigns a value to its left


operand based on the value of its right operand.

• Collections are assigned by reference.

91
Example

a = [1,2,3] # a now references to the list


[1,2,3]
b = a # b now references to what a references.
a.append(4)
print(b) # prints [1,2,3,4]

92
Example

x = 3
y = x
y = 4
print (x) # prints 3, as X is not
affected

93
Casting

• int()
constructs an integer number from an integer literal, a float
literal (by rounding down to the previous whole number), or a
string literal (providing the string represents a whole number)

• Example:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

94
Casting

• float()
constructs a float number from an integer literal, a float
literal or a string literal (providing the string represents a
float or an integer)

• Example:
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2

95
Casting

• str()
constructs a string from a wide variety of data types, including strings, integer
literals and float literals

Example:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'

96
String

• String literals in python are surrounded by either


single quotation marks, or double quotation
marks 'hello' is the same as "hello".

• Strings can be output to screen using the print


function.

• For example:
print("hello").

97
String functions

b = "Hello, World!"
print(b[2:5]) # Prints llo
print(len(b)) # Prints 13
print(b.replace("H", "J")) # Prints Jello World
print("Enter your name:")
x = input() # Gets input from user
print("Hello, " + x)

98
Operators

Python divides the operators in the following groups:


Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators

99
Arithmetic
operators

x to the power y

5 / 2 will return 2.5 and 5 // 2 will return 2

100
Assignment
operators

Signed Shift Right


Shift Left

101
Comparison operators

102
Logical Operators

103
Identity
Operators

104
Membership
Operators
Membership operators are used to test if a sequence is presented in an
object:

105
Bitwise
Operators

Bitwise operators are used to compare (binary) numbers:

106
If ... Else

a = 200
b = 33
if b > a or b >= a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

print("A") if a > b else print("B")

107
While Loops

i = 1
while i < 6:
print(i)
i += 1
if(i == 3):
break
else:
continue

108
For Loops

fruits = ["apple", "banana", "cherry"] #list


for x in fruits:
if x == "banana":
continue
print(x)

#The range() function returns a sequence of numbers,


starting from 0 by default, and increments by 1 (by
default), and ends at a specified number.
for x in range(6): #From 0 to 5
print(x)

109
Python Data-Structure
There are four data structure in the Python:
• List is a collection which is changeable (Mutable).
Allows duplicate members.
• Tuple is a collection which is unchangeable (Immutable).
Allows duplicate members.
• Set is a collection which is unindexed and changeable
(Mutable).
No duplicate members.

• Dictionary is a collection which is changeable (Immutable).


No duplicate keys.
110
List []

Create a=List:
thislist ["John", "David", "Chris"]
print(thislist) #Prints ["John", "David", "Chris"]
print(thislist[1]) # Accessing an element and Printing David
thislist[1] = "Sam" # Replaces David with Sam
#Loop through list
for x in thislist:
print(x)
if "Chris" in thislist:
print("Yes, 'Chris' is in the list")

print(len(thislist)) #Prints 3

thislist.append("Smith")
thislist.insert(1, "Jack")
thislist.remove("John")

111
List Built in Functions

112
Tuple ()

thistuple = ("Sally", "Sama", "Sara")


print(thistuple)

thistuple[1] = "Salma"
# Error The values will remain the same:
print(thistuple)

thistuple[3] = "Serine" # This will raise an error, can not


add new item
print(thistuple)

113
Tuple Built in Functions

Python has two built-in methods that you can use on


tuples.

114
Sets {}
thisset = {"Sally", "Sama", "Sara"}
print(thisset)

print("Sally" in thisset) #Prints Sally if it exists

#Once a set is created, you cannot change its items, but you can
add new items.
#To add more than one item to a set use the update() method.
thisset.add("Salma")

115
Sets Built in Functions

116
Dictionary
Dictionaries are written with curly brackets, and they have keys and values.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)

x = thisdict["model"] #Accessing item with key model


x = thisdict.get("model") #Get the value of the "model" key
thisdict["year"] = 2018 #Change the "year" to 2018

#add new item


thisdict["color"] = "red"

#Print all key names in the dictionary


for x in thisdict:
print(x)
#Print all values in the dictionary
for x in thisdict:
print(thisdict[x])

#Loop through values


for x in thisdict.values():
print(x)

#Loop through both keys and values, by using the items()


for x, y in thisdict.items():
print(x, y)
117
Dictionary Built in Functions

118
Functions
Example of function without parameters:
def my_function():
print("Hello from a function")

my_function()

Example of function with parameters:


def my_function(fname):
print("Hello" + fname)

my_function("Emil")

119
Functions
If we call the function without parameter, it uses the default
value:
def my_function_1(country = "Norway"):
print("I am from " + country)

my_function_1("Sweden")

To let a function return a value, use the


return statement:
def my_function_2(x):
return 5 * x

print(my_function_2(3))

120
Yield
To understand what yield does, you must understand what
generators are. And before generators come iterables.
Iterables: When you create a list, you can read its items one
by one, and it’s called iteration.

mylist = [1, 2, 3]
for i in mylist:
print(i)
1
2
3
121
Yield
• Everything you can use “for… in…” is an iterable: lists, strings,…
and all the values are stored in memory and it’s not always what
you want when you have a lot of values.

mylist = [x*x for x in range(3)]


for i in mylist:
print(i)
0
1
4

122
Yield
• Generators are iterators, but you can only iterate over
them once.

• It’s because they do not store all the values in memory, they
generate the values on the fly.

• It is just the same except you used () instead of []. BUT, you can not
perform for i in mygenerator a second time since generators
can only be used once: they calculate 0, then forget about it and
calculate 1, and end calculating 4, one by one.
mygenerator = (x*x for x in range(3))
for i in mygenerator:
print(i)
0
1
4

123
Yield
Yield is a keyword that is used like return, except the function will return
a generator.

def createGenerator():
mylist = range(3)
for i in mylist:
yield i*i
mygenerator = createGenerator() # create a generator
for i in mygenerator:
print(i)

124
Classes/Objects

• Python is an object oriented programming language.


• Almost everything in Python is an object, with its properties
and methods.
• Example of Creating a Class
class MyClass:
x = 5
class EmptyClass:
pass

125
The __init__() Function
• All classes have a function called __init__(), which is always
executed when the class is being initiated.

• Use the __init__() function to assign values to object properties, or


other operations that are necessary to do when the object is being
created:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age
“self” parameter
def myfunc(self): is not passed
print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()
126
Self

• The self parameter is a reference to the


class itself, and is used to access variables
that belongs to the class.

• It does not have to be named self , you can


call it whatever you like, but it has to be the
first parameter of any function in the class.

127
Try - Except

try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")

128
Hands On
Write a program to compute:
f(n)=f(n-1)+100 when n>0
and f(0)=0
with a given n input by console (n>0).

Example:
If the following n is given as input to the program:
5
Then, the output of the program should be:
500

129
Solution

def func(n):
if n == 0:
return 0
else:
return func(n - 1) + 100

n = int(input())
print(func(n))

130
Hands On
Write a class named Circle constructed by a radius and
two methods which will compute the area and the perimeter
of a circle.

Example:
Circle Radius: 8
Circle Area: 200.96
Circle Perimeter: 50.24

131
Solution

class Circle():
def __init__(self, r):
self.radius = r

def area(self):
return (self.radius ** 2) * 3.14

def perimeter(self):
return 2 * self.radius * 3.14

NewCircle = Circle(8)
print(NewCircle.area())
print(NewCircle.perimeter())

132
Hands on

Write a Python program to find the list


in a list of lists whose sum of elements
is the highest.

Sample lists: [1,2,3], [4,5,6], [10,11,12],


[7,8,9]
Expected Output: [10, 11, 12]
133
Solution
Solution 1:
num = [[1,2,3], [4,5,6], [10,11,12], [7,8,9]]
print(max(num, key=sum))

Solution 2:
num = [[1,2,3], [4,5,6], [10,11,12],
[7,8,9]]
l = []
maxSum = 0
for x in num:
sum = 0
for n in x:
sum+=n
if(sum > maxSum):
maxSum=sum
l = x
print(l)

134

You might also like