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

Data science with Python

This document introduces Python as a programming language and its application in data science. It explains how to install Python and introduces basic concepts such as variables, comments, and printing. It also contrasts structured programming vs object-oriented programming and presents code examples. The goal is to prepare the reader to use Python libraries in data science by developing basic programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Data science with Python

This document introduces Python as a programming language and its application in data science. It explains how to install Python and introduces basic concepts such as variables, comments, and printing. It also contrasts structured programming vs object-oriented programming and presents code examples. The goal is to prepare the reader to use Python libraries in data science by developing basic programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit 1

DATA SCIENCE WITH PYTHON:


COLLECTION, STORAGE AND PROCESS

Source: adobestock/84383512

Author:
Amaury Giovanni Méndez Aguirre
INDEX

Introduction................................................................................. 3

Data science with Python: collection, storage, and process.

What is Python? 5

Installation of a Python interpreter 5

Starting to program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Commenting on a code...

Variables

Creation of variables..................................... 10

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Structured programming vs Object-oriented programming........ 12

Inheritance

Conclusiones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Bibliographya . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Introduction

INTRODUCTION

Python is a language that has become popular in


the last few years for their simplicity and ease in both
its use as in its learning. For this reason, you are going to
find the relevant and fundamental aspects
of language and its programming form based on
the object-oriented methodology, preparing you for the
path to use the specialized libraries for this
language for data science. You are going to learn
how to develop programs in Python, how to program
In object-oriented programming, what are comparisons?
and conditions in programming, loops or cycles and the
most commonly used data types such as lists and dictionaries
dictionaries. Throughout this course, you will encounter
code examples that you will be able to develop for
train your programming skills, so
let's get started.
Data science with
Python collection,
storage and process
What is Python?

As a programming language, Python has a very simple structure or syntax.


compared to other programming languages like C++ or Java. This does not
it reduces power when performing complex tasks in several scenarios with less
lines of code and in less time, as in the case of Data Science, where it is
preferred alongside the R language precisely for this reason (García, 2018)

From a more technical point of view, Python is a programming language inter-


borrowed, object-oriented, and high-level. It was created by Guido van Rossum in 1991,
with the idea of being an easy, intuitive, and open-source language. It is one of the languages
what has grown more, due to its compatibility with most operating systems
features and the ability to integrate with other programming languages. Applications
like Dropbox, Reddit, and Instagram are written in Python (Python for Data Science,
2022)

Structured programming simply presents


a sequence of instructions where its potential
it is evidenced in code blocks that can be reused Function:
In programming, a function is
to be defined indefinitely, called functions. For the a block of code that can re-
object-oriented programming (OOP), this is sim- to be used without the need to return
to define its content.
implement a feature of your potential, since
these 'objects' can be created and be independent Jupyter Lab:
from your original code block. This difference will be It is a development environment with in-
web interface for programming in
key throughout the module and will show the potential of Python. The installation process
program in Python. it can be seen in the following video:
https://youtu.be/tzxcKmFj24A

Installation of a Python interpreter

To start programming in Python, we are going to choose


to install a development environment known as Jupyter Lab. Once installed,
we will be able to run our first program (it is recommended to type the instruction
complete and not to use the copy and paste tool):

5
Hello World!

sum multiplication

print(2 + 3) print(3 * 4)
>> 5 12

division integer division

print(10 / 3) print(10 // 3)
3.3333333333335 >> 3

remainder of the division

print(20 % 6)
>> 2

make a text uppercase

DATA SCIENCE PROGRAMMING


PROGRAMMING FOR DATA SCIENCE

combine texts

programming in Python
Python programming

separate words in a text

["Ciencia","de","Datos"]
Data Science

Figure 1

Source: own

6
Figure 2

Source: own

The function print() takes a text argument that is enclosed in quotes.


doubles and is a necessary condition for Python to interpret it as text. This function
it is already pre-loaded when starting any Python interpreter and is not necessary
import it, as we will see later with other functions.

Starting to program

To start programming, you must understand that a programming language is...


CIA can perform mathematical operations, but it can also perform other types of
operations or instructions with various types of data that are not only numeric.
Let's look at some examples and exercises that you can develop within Jupyter.
Lab: Observe these examples that, just as arithmetic symbols can be used
To perform mathematical operations, linguistic symbols can also be used.
like the quotation marks, the comma, and the period, and in the latter its use becomes inherent to OOP,
like when using the methods .upper() and .split() of the string class, but this is a topic that
We will address it later.

Do you dare to perform operations like (3 + 4 * 7 * (5 - 2) / (10 ** 2))?

Comment a code

In Python, just like in other programming languages, you can perform small
or great comments to explain lines of code, leave important information or
any other use that the programmer wishes to leave, and for this, it can be done either a
single line comment or multi-line comments.

7
To comment a single line, simply use the hash symbol #, and to comment
various lines usually use single or double quotes, three times at the beginning
and three times at the end. Let's see how:

Single line comment


“””
Comments
of several lines
as required or desired by the programmer

“””

Variables

A variable is simply a space in memory that will be used temporarily.


to store some type of data. Let's see an example:

edad = 17

print( age )

>> 17

Note that the equal sign (=) has been used to perform this
Variable:
temporary storage of the number 17 in a created variable It is simply a
with the name age. The number 17 turns out to be a type of memory space
what will be used time-
since in this case it is an integer. In this example, for generally for the soul
show the content of the variable, the print function has been used to have dinner of some kind
date.
) and as a result, the stored number is displayed.

To inquire in Python about which type of data has been stored


Born in the variable, we can use a function called type() that fulfills this
purpose:

type(age)
>> int

8
Something very important to highlight is that variables can store results.
of different arithmetic operations or objects as shown below:

sum = 5 + 7

print( sum )

12

nombre = “Ciencia de Datos”.upper( )

print( name )

DATA SCIENCE

Figure 3

Source: own

9
Creation of variables

Variables can be created under certain rules:

It can start with a letter, (a - z, A - Z) or with an underscore, example:

ancho = 15

Altura = 20

_radio = 3.5

It can contain other letters, numbers, and the underscore later, for example:

primer_nombre = “Giovanni”

_horas_dia2 = 4

Variable names are case sensitive, which means that the variable
height will be different from the variable Height or from the variable HEIGHT

Because Python uses certain words to execute code instructions,


not all words can be used to create variables, and this is known as
reserved words.

Visit page

Find the detailed list at the following link

https://www.w3schools.com/python/python_ref_keywords.asp

10
Exercises

Solve these mathematical operations with Python

1. y = x2+4x+10 where x = 5

2. y = 3x2+5x-50 donde x = -15

3. y = 2x3-4x+10 -6 * 7 donde x = 35

4. y = ((10 + 30) ** 4) - (600 / 200)

5. y = xm- 7k+10 donde x = 5, m = 3, k= 8

A possible code for exercise 1 would be the following:

x=5

y = x**2 + 4*x + 10

print("y =", y)

y = 55

Now continue with the other exercises.

11
Structured programming vs Object-oriented programming

Consider the following lines of code as an example of structured programming.


sequential or sequential mode, where the code will execute line by line

edad = 20

if age < 19:

the value of age is less than 19

end of the program

end of the program

Figure 4

Source: own

In this example, a feature has been defined in the form of a variable with the word
age, and it has been assigned a numerical value of integer type 20. The result of the program
it will be the message 'end of the program' since the imposed condition of a lower value
Number 19 is not fulfilled, but we will study this in more depth later.
now consider that a structured or sequential program simply takes instructions
actions hierarchically and executes them one by one.

12
In the following example, analyze a problem where father and son each have a
car and it is desired to describe its characteristics as shown below:

auto_padre_color = “rojo”

auto_hijo_modelo = 2020

print (father_auto_model)

>> NameError: name ‘auto_padre_modelo’ is not defined

Figure 5

Source: own

13
In this example, Python will show us an error in which it tells us that the variable
auto_padre_modelo has not been defined, and it's right!, since although the variable has
It has been defined for the child car, but not for the parent car, and vice versa with the character.
Characteristic of color. Can you imagine how many variables we would have to create to describe
the characteristics of each car, not just for this example, but for example, for each
new car that enters our program? And if at some point a definition were made of a
new feature for cars, like a new navigation system, how many
What new variables would we have to create? Well, this is where object-oriented programming
objects make sense as they save us from creating new variables through the
ability to create new objects from a template known as
of Class. In fact, by creating variables where we define names, we are creating new
your text-type objects (str) and they are inheriting all their attributes and methods.
Let's look at an example:

nombres = “Saray Vanesa”

apellidos = “Lozano Castañeda”

print(nombres.upper(), apellidos.upper())

Saray Vanesa Lozano Castañeda

Figure 6

Source: own

Note that when creating the variables names and surnames, both have the ability to
use the .upper() method that transforms the text into uppercase

14
In code, a 'Class' is created as follows (it is very important that when you type-
Take the following example, spaces should be respected, as it is a characteristic of Python
called indentations

class Car: #we define the name of the Car class

We create the class attributes by default

they carry the reserved word self

def __init__(self, model, color):

self.model = model

self.color = color

def see_model(self):

print(self.model)

def check_color(self):

print(self.color)

We create the objects from the Car class

padre = Auto (2018, “blanco”)

hijo = Auto (2022, “rojo”)

son.show_model()

>> 2022

15
Figure 7

Source: own

Note that at this moment, the instruction hijo.ver_modelo() results in


the print of the number 2022, since it is a method defined in the Car class.
Likewise, if the instruction padre.ver_modelo() were executed now, the result must-
it could be the number 2018. In this code, we see that a class called Auto has been created with three
__init__(self) is a default method, a method that serves to
initially build the objects of the class and pass attributes from their creation, such as
It is said that the father's car will be a 2018 model and white in color. The method
show_model(self) which aims to display as a message the contained value
in the model attribute, and the method over_color(self). In the creation of classes and methods,
the reserved word 'self' is mandatory for Python to develop the methods correctly
completely. As a suggestion, think of the methods as actions that must be taken by the
object and hence the names to display information can be words like
show or see or print.

16
Inheritance

In programming, you can create object classes that can inherit both the attributes
but like the methods so you don't have to redefine any of these. In the
In the next example, you will see the creation of a machine class and from this, the creation
of other classes

Figure 8

Source: own

17
In this example, some are highlighted The variables meet conditions for
things: to be declared or created and can count-
ner any type of existing data in the
1. The reserved word pass is used lenguaje Python. Estas reglas son:
to leave part of the code undefined,
as it happens in the __init__ method a.They can start with a letter (a-z)
(self). (A-Z)

2.When inheriting, the name of the cla- b.They can start with an underscore.
from which it is going to be inherited, like ejemplo:_nombre
it happens in the case of the classclass
Computer (Machine) where it is- c.They are case sensitive, which indicates that
we are programming the classclass apple is different from Manzanao
Computer, all the attributes and APPLE and all its possible com-
methods that the class has class Ma- combinations of uppercase and lowercase
the class Computer has- the
read the method turn on of Machine
and that's why when creating the object pc_portatil, d. The words re- cannot be used
this can make use of that method reserved in Python as variables
through the instruction pc_porta-
turn_on()

Recommended reading

Conclusions

Python is an object-oriented language


terms, which should be understood that upon
programar, se debe pensar en que cada
instruction probably has some
type of special behavior that can To conclude, I invite you to carry out the
to be reused, such as the action next complementary reading:
of turning on or off a machine
like a car, or like a stove, a What's new in Python?
fridge, a television, among others, or opening
and closing a door or window. These
actions are known as methods, and
the characteristics they may have, such as
the material, size or color, are known
as attributes.

18
BIBLIOGRAPHY

García, J. (2018). Ciencia De Datos. Técnicas Analíticas Y Aprendizaje


Statistician. A Practical Approach - Jesús García.pdf
published. Retrievedfrom https://idoc.pub/documents/idocpub-6ng22yvvd2lv.

Python Tutorial. W3schools.com. (2022). Retrieved 31 August 2022, from


https://www.w3schools.com/python/default.asp.

You might also like