Skip to content

You’ll begin by exploring the basics of Python programming and why Python is such a powerful tool for data analysis. You’ll learn about Jupyter Notebooks, an interactive environment for coding and data work. You’ll investigate how to use variables and data types to store and organize your data; and, you’ll begin practicing important coding skills.

Learning Objectives

  • Explain how to convert data types in Python
  • Define fundamental data types such as integer, float, and string
  • Interpret the naming conventions and restrictions for variables in Python
  • Describe the purpose and procedure of assigning variables
  • Define fundamental concepts of object-oriented programming such as object, class, method, and attribute
  • Recognize the uses and benefits of Jupyter Notebook for data work
  • Identify the basic features and capabilities of the Python programming language
  • Explain how to import modules in Python scripts using import
  • Introduce Jupyter notebook as a python environment
  • Perform arithmetic operations
  • Use built-in functions and keywords to explore data
  • Understand why Python is relevant to data science
  • Understand packages and interpreter options for Python
  • Identify the concept of algorithms in python
  • Identify the value of Python competency for data careers
  • Define Python and what syntax means

Get started with the course


Video: Introduction to Course 2

  • Python is a powerful tool for data professionals.
  • This course will provide a strong foundation in Python fundamentals.
  • The course will cover a variety of topics, including variables, data types, functions, conditional statements, loops, strings, data structures, and more.
  • The course will conclude with an end-of-course project that can be added to a professional portfolio.
  • Learning Python will take data analysis skills to the next level and make candidates more competitive in the job market.

Hey there! Welcome to the next stage of your learning journey. Congratulations on
completing your first course. You’ve learned how data professionals contribute to the success
of an organization and the main tools and techniques they use on the job. Now, you’ll learn how to use one of the most powerful tools available to data professionals: the Python programming language. Computer programming refers to the process of giving instructions to a computer to perform an action or set of actions. You can use different
programming languages to write these instructions. You might choose a specific language based on the project you’re working on, or the problem you want to solve. The Python programming language is super useful for working with data. Data professionals use Python to analyze data in faster, more efficient, and more powerful ways because it optimizes every
phase of the data workflow, from exploring, cleaning,
and visualizing data, to building machine learning models. This course will give
you a strong foundation in Python fundamentals, and prepare you for
more advanced data work in your future career. If this is your first experience with the Python programming
language… welcome! This course does not assume you have any prior knowledge of Python. We’ll begin from the beginning, and work through each
concept step by step. Take it one step at a time, and go at your own pace. As you develop your Python skills, you’ll apply what you learn to gain valuable practice
working with data. And if you have experience with Python, that’s great too! I’ll help you apply your
knowledge in a new way, and demonstrate how to use Python for data analytics specifically. Let me introduce myself. My name’s Adrian. I work as a Customer
Engineer at Google Cloud. This means that I work with our customers to understand the technologies they have at their disposal to solve data analytics needs. I first learned Python when I wanted to create
an electronic journal. I got tired of buying a new
physical one every year. I learned how to password protect it, and to this day, it’s still
one of my proudest moments of using Python. Throughout your career
as a data professional, you’ll have the opportunity
to continually learn and grow. To me, that’s one of the
coolest aspects of the job. And learning Python is one
of the most rewarding parts of that growth process. I’m still learning new ways
to use Python all the time, both at work and for fun. Now, let’s review what you will learn. We’ll start with a general
introduction to Python, and discuss why it’s such a
popular programming language among data professionals. You’ll learn fundamental coding concepts, such as variables and data types, and how they help store
and organize your data. You’ll also get a chance to start writing your own Python code. Next, you’ll explore functions, or reusable chunks of code that let you perform specific tasks. Functions help you work with
data quickly and efficiently. You’ll also learn about
conditional statements, which tell the computer
how to make decisions based on your instructions. Then, you’ll discover the power of loops, which repeat a portion of code until a process is complete. You’ll also learn how
to work with strings, which are sequences of characters, such as letters and punctuation marks. After that, you’ll explore
data structures in Python, which are methods of storing and organizing data in a computer. You’ll review the most useful structures for data professionals, such as lists, sets,
dictionaries, and dataframes. Finally, you’ll apply your Python skills in an end-of-course project that you can add to your
professional portfolio. The end-of-course project
features a unique dataset based on a workplace scenario. In future job interviews, you can share your project as a demonstration of your skills, and impress potential employers. Learning Python will take
your data analysis skills to the next level. It’ll also be a great
addition to your resume. Knowing how to use Python is a key credential
for data professionals, and will give you a big
boost as a job candidate. I’m here to help you
every step of the way. And remember, you set the pace. Feel free to watch the videos as many times as you like, and review topics that are new to you. By the end of this course, you’ll know how to use Python to explore and analyze data. Let’s get started.

Video: Welcome to module 1

This course will teach you how to use Python for data analysis. You will learn about the main features and capabilities of Python, as well as the basics of Python programming. You will also learn about Jupyter notebooks, object-oriented programming, variables, and fundamental data types.

Hi there! I can’t wait to explore Python with you! During our journey together, you’ll learn how to use the Python programming language to power your data analysis. This course will give you a foundation of essential Python skills that you can continue to build on throughout this
program… and your career. We’ll begin with an overview of Python’s main features
and capabilities, and discuss the basics
of Python programming. You’ll discover how Python can help you work with your data quickly and efficiently. Then, we’ll consider Jupyter notebooks, an interactive environment for coding and data work. You’ll learn about the useful functionality of Jupyter notebooks, and how to write Python code in the notebook environment. Python is an object-oriented programming language based on objects that contain data and useful code. Next, you’ll explore the benefits of object-oriented programming for data professionals, and become familiar
with its basic concepts. After that, you’ll discover variables, one of the building blocks
of Python programming. You’ll learn how variables help you store and label your data, and how to assign specific
values to variables. We’ll also review the conventions, or widely accepted rules, for naming variables. Learning these conventions will help you write code that is clear, precise, and consistent. Finally, we’ll explore
fundamental data types such as integers, floats, and strings. You’ll learn how to convert and combine data types
to organize your data. When you’re ready, join me in the next video!

Video: Adrian: My path to a data career

  • Adrian is a Customer Engineer at Google Cloud who helps customers solve data analytics needs.
  • Adrian came from a non-traditional background in nursing and was able to transfer skills such as critical thinking, problem solving, and assessment to his data analytics career.
  • Adrian also found that his soft skills or interpersonal skills were critical in his data analytics career as he works with others in a collaborative space.
  • Adrian got into data analytics through his interest in programming and digital humanities.
  • Adrian believes that data analytics is an entry-level field and that anyone can get started with the right foundation.

My name’s Adrian. I work as a Customer
Engineer at Google Cloud. This means that I work with our customers to understand the technologies
they have at their disposal to solve data analytics needs. I come from a non-traditional background started out in nursing,
spent several years there before I realized that
while dealing with patients and helping patients was
definitely fulfilling, it wasn’t exactly what I wanted to do with the rest of my life. There were a number of
transferable skills that I learned from my previous career as a nurse. First being critical thinking,
second problem solving, and third assessment. Within advanced data analytics, critical thinking and
problem solving becomes key when trying to debug. Furthermore, when looking
for answers online, you need to be able to
assess what information you’re being given and understand how to apply
it to fix your problem. Another skill that I was able
to bring over from nursing is soft skills or interpersonal skills, which are critical in
advanced data analytics as you try to work with others
in a collaborative space. I got into this career
because I had gained skills related to programming
just throughout my life. I didn’t really understand
how I could apply it or the fact that I could actually apply it. When I got an undergraduate
degree in English and history and it was after realizing
what the job opportunities were for humanities that I had to think again about what my next step would be. That’s where technology
came into play and I found that I could actually use
technology and integrate that with what I was doing from
a humanities point of view with English and history and
get into digital humanities. That led me to data analytics
through the process of or the concept of knowledge management. We’re in a space where every company is gonna become a data company. Whether you’re working in medicine, whether you’re working in
retail, whatever it might be, data, and the ability to manipulate and leverage it is going to be critical. My favorite thing about data analytics is it can be incredibly entry level. Once you understand those basics, you can get started on your own. You don’t need to have a
formalized university degree, you don’t need to have years
and years of experience. Getting started is
something that can be done as long as you put in the work to get the elementary foundation down.

Video: Introduction to Python

  • Programming languages are the words and symbols that we use to write instructions for computers to follow.
  • Computers are just billions of transistors, or switches, and they understand things only in binary terms.
  • Programming languages are designed to simplify and generalize programming instructions.
  • High-level languages are easier to learn because they are designed with simpler rules and structures.
  • Python is a high-level language that is versatile, easy to learn, and powerful.
  • Python is open source and has a large community of users who are willing to help and provide support.
  • Coding is simple and complex at the same time. Each line of code represents a small, simple idea, but together those lines of code can express very complex logic.
  • Coding can be frustrating at times, but also a lot of fun and very rewarding.
  • Experimenting is part of the process, and practicing will help you quickly improve your coding skills.

Introduction to Python

Python is a high-level, general-purpose programming language. It is known for its clear syntax and its readability. Python is also a very versatile language, and it can be used for a wide variety of tasks, including:

  • Web development
  • Data science
  • Machine learning
  • Scripting
  • Automation

In this tutorial, we will cover the basics of Python programming. We will learn about variables, data types, operators, control flow, and functions.

Variables

A variable is a name that refers to a value. In Python, variables are created using the assignment operator (=). For example, the following code creates a variable called age and assigns it the value of 25:

Python

age = 25

Data types

Python has a number of different data types, including:

  • Integers (e.g., 1, 2, 3)
  • Floats (e.g., 1.5, 2.25, 3.14)
  • Strings (e.g., “Hello, world!”, “Python is awesome!”, “I love coding!”)
  • Booleans (True, False)

The data type of a variable can be determined using the type() function. For example, the following code determines the data type of the age variable:

Python

type(age)

Operators

Operators are symbols that are used to perform operations on values. Python has a number of different operators, including:

  • Arithmetic operators (+, -, *, /, %)
  • Comparison operators (<, >, <=, >=, ==, !=)
  • Logical operators (and, or, not)

For example, the following code uses the addition operator (+) to add the values of two variables:

Python

x = 1
y = 2
z = x + y

Control flow

Control flow statements are used to control the flow of execution of a program. Python has a number of different control flow statements, including:

  • If statements
  • Else statements
  • Elif statements
  • For loops
  • While loops

For example, the following code uses an if statement to determine if a number is even or odd:

Python

number = 10

if number % 2 == 0:
  print("Even")
else:
  print("Odd")

Functions

Functions are blocks of code that can be reused multiple times. Functions are defined using the def keyword. For example, the following code defines a function called add() that takes two arguments and returns the sum of the arguments:

Python

def add(x, y):
  return x + y

result = add(1, 2)
print(result)

Conclusion

This tutorial has covered the basics of Python programming. We have learned about variables, data types, operators, control flow, and functions. With this knowledge, you can start writing your own Python programs.

Additional resources

  • Python official documentation: https://docs.python.org/
  • Python tutorial: https://www.learnpython.org/

Python is a powerful coding language that has become one of the preferred tools of data professionals
worldwide, and for good reason. In this video, we’ll
explore what Python is and why it has become so popular. But first, let’s discuss
some basic elements of programming languages in general. Programming languages originated with the development of
electronic computers. They were, and still are, the words and symbols that we use to write instructions
for computers to follow. Communicating with a computer ultimately relies on computer hardware. A transistor is the most
fundamental component of the computer because it controls the flow of electricity through a circuit. A transistor can exist in two states: on or off, like a switch. When a transistor is on,
electricity passes through it. When it’s off, it blocks the electricity. This duality defines
how computers operate. If you chain enough transistors together, each either on or off, you
can create complex logic. So how does this relate
to computer programming? Well, because computers are just billions of transistors, or switches, they understand things
only in binary terms. You may have encountered
this concept before: binary. Binary is represented as ones and zeros. These numbers are just an easier way to refer to the on and off
sequences of transistors when a computer receives
instructions from a program. Computers are powerful, but they still need to
be given instructions, and they can only understand instructions that are given in binary. The engineers who first designed computers encountered this and discovered a problem: computers are great at
understanding binary, humans are not. This problem is what gave rise to the first programming languages. The very first programming languages were difficult to use,
required lots of training, and often only worked
on the specific machine that each was designed for. These types of languages are
known as low-level languages. Over time, new coding languages developed to simplify and generalize
programming instructions. The programming languages
became easier to learn because they were designed with
simpler rules and structures, known as syntax. Modern programming languages use syntax that’s much more familiar to humans. These languages are known
as high-level languages. And, this brings us back to Python. Python is a high-level language that’s versatile and easy to learn. Simply put, Python is friendly. In fact, some people might think that the name itself is scary, but the creator of Python didn’t name it after a giant snake. He named it after a British
comedy troupe: Monty Python, because he wanted it to
be easy and approachable. In addition to Python being
versatile and easy to learn, it’s also powerful. This combination of qualities
has made it a a favorite, not only of data professionals, but of scientists and web developers too. Part of what makes it so
powerful is that it’s open source, and developers have created
many libraries and tools to make many jobs requiring Python easier. A library is a reusable
collection of code. For instance, you could
hand code a function that takes two numbers, adds ’em together and returns the sum. But what if now you want to
add three numbers? Or four? You can write a more complex function that lets you input any
combination of numbers and it will return the sum. But summing is a super common task, so you can save yourself a lot of time by just using a math library
that contains a sum function. There are thousands of Python libraries that contain code for tasks
as simple as summing numbers and as complex as
building a neural network for an artificial
intelligence application. You’ll learn more about libraries soon, and you’ll learn about neural nets and AI and how they fit into the
world of data analytics in a later course. This certificate program focuses
on advanced data analytics, so you’ll learn to use Python as its most often applied
in data analysis work. You’ll also learn about
NumPy, pandas, statsmodels, matplotlib, seaborn,
scikit-learn, and more. These are code libraries
that are used every day by data professionals on the job. You’ll explore these in detail later. The ease of learning,
ease of use, versatility, and power of Python make it one of the most-used coding languages today. Because it’s so widely used, it has a large and
active community of users who are willing to help
and provide support… which makes it a great coding language to discover and explore. As you move through this course and the entire certificate program, always remember that coding
is simple… and complex. In other words, each line of code represents a small, simple idea, but together those lines of code can express very complex logic. Coding can be frustrating at times, but also a lot of fun and very rewarding. You will practice coding
a lot in this course, so you can get better and better. Lastly, don’t be afraid to make a mess. Experimenting is part of the process, and practicing will help you quickly improve your coding skills.

Practice Quiz: Test your knowledge: Get started with the course

Fill in the blank: Computer _ refers to the process of giving instructions to a computer to perform an action or set of actions.

In Python, what is a library?

Python is a low-level programming language.

The power of Python


Lab: Annotated follow-along guide: Hello, Python!

Reading

Video: Discover more about Python

In this video, you learned the fundamentals of Python. Python is a high-level programming language that uses human-friendly syntax. It is simple enough that you can learn some of its basic concepts just by example.

Here are the key takeaways from the video:

  • You can print to the console using the print() function.
  • You can perform computations using Python operators, such as +, /, and *.
  • You can assign variables using the = sign.
  • You can evaluate statements using Python operators, such as ==, <, and >.
  • You can use conditional logic to perform actions based on certain conditions.
  • You can use loops to perform the same action to each element of something.
  • You can define functions to reuse code.
  • Python has a library of built-in functions that perform common tasks.

Python is a powerful language that can be used to create a wide variety of applications. It is a great language for beginners to learn, and it is also used by professional programmers.

Discover More About Python in Cybersecurity

Python is a general-purpose programming language that is widely used in cybersecurity. It is a popular choice for many cybersecurity tasks because it is easy to learn, powerful, and versatile.

Here are some of the things you can do with Python in cybersecurity:

  • Network security: Python can be used to write scripts to automate tasks such as port scanning, vulnerability scanning, and intrusion detection.
  • Malware analysis: Python can be used to write scripts to analyze malware samples and identify their capabilities.
  • Security forensics: Python can be used to write scripts to collect and analyze evidence from compromised systems.
  • Web security: Python can be used to write scripts to test web applications for vulnerabilities and to exploit vulnerabilities.
  • Cryptography: Python can be used to implement cryptographic algorithms for encryption, decryption, and digital signatures.

If you are interested in learning more about Python for cybersecurity, here are some resources to get you started:

  • Online tutorials: There are many online tutorials available that can teach you the basics of Python programming. Some good tutorials to start with include the tutorials on the Python website and the tutorials on the Real Python website.
  • Books: There are also a number of books available on Python for cybersecurity. Some good books to start with include the book “Python for Cybersecurity” by Michael T. Swart and the book “Python for Security Professionals” by Michael Gregg and Chad Seaman.
  • Courses: There are also a number of courses available on Python for cybersecurity. Some good courses to start with include the course “Python for Cybersecurity” on Coursera and the course “Python for Security Professionals” on Udemy.

Once you have learned the basics of Python programming, you can start to explore more advanced topics, such as network security, malware analysis, security forensics, web security, and cryptography. There are many resources available online and in libraries that can help you learn these topics.

Here are some examples of how Python is used in cybersecurity:

  • Nmap: Nmap is a popular network scanning tool that is written in Python. It can be used to scan networks for open ports, services, and operating systems.
  • Metasploit: Metasploit is a popular penetration testing framework that is written in Python. It can be used to exploit vulnerabilities on systems and to perform other security testing tasks.
  • Wireshark: Wireshark is a popular network traffic analyzer that is written in Python. It can be used to capture and analyze network traffic.
  • Maltego: Maltego is a popular intelligence gathering tool that is written in Python. It can be used to gather information about people, organizations, and infrastructure.

Python is a powerful and versatile language that can be used for a wide variety of tasks in cybersecurity. If you are interested in a career in cybersecurity, I highly recommend learning Python.

Previously, you learned that Python is a high-level programming language. This means that Python uses
more human-friendly syntax, and more closely resembles
a spoken language. In fact, Python is simple enough that you can learn some of its basic concepts just by example. In this video, I’ll demonstrate some of
the fundamentals of Python. I’ll introduce you to some new terms, but we won’t take into account formal definitions and processes. We’ll consider these things in more detail later. For now, just take a
moment to get familiar with the code and how it works. The first thing we’ll do
is print to the console. If we tell the computer
to print “Hello, world!”… it will output “Hello, world!” for us. In fact, the print function will output whatever we enter in its parentheses. Of course, Python is also capable of performing computations. If we divide the sum of
five and four by three, we get three. In Python, we can also assign variables. You can think of a variable like a container that you can name. A container’s contents
are known as its value. For example, here we
create two new variables. The first variable is called “country,” and we’re assigning to
it the value of “Brazil.” The second variable is called “age.” We’ll assign it the value of “30.” Now we can refer to the
values – “Brazil” and “30” – by their variable names. When I give the “country” variable to the print function,
it returns “Brazil.” Similarly, when I give the “age” variable to the print function, it returns “30.” Python can also be used
to evaluate statements. For example, I can
check if 10 to the 3rd is equal to 1,000. It is, so the computer returns “True.” Notice that so far I’ve used the “plus” sign for addition, the forward slash for division, and two asterisks to indicate an exponent. Like in mathematics, these signs are known as operators. A lot of them are straightforward. However, some might not be so simple. Take this last case for example. I used two equals signs to check the equivalence of two values. When used properly, the computer will return
either True or False. What happens when I use one equals sign? I get a syntax error. You might have noticed
from earlier examples that the single equals sign is reserved for assigning variables. Just like in spoken languages, Python has rules that
govern its construction. You’ll learn many of these
rules in this course, but I won’t discuss them in detail now. You won’t easily learn
a new spoken language by studying a big book of its rules. You’re more likely to learn it by speaking it, hearing it,
reading it, and writing it. It’s also a lot more fun that way. It’s the same with Python. So, for now, don’t worry
about memorizing any rules. First, let’s observe Python in action. As you might expect, if I make a false equivalency, like 10 x 3 = 40, the computer will return “False.” I can even use a variable
I defined previously, age, in a new statement. 10 x 3 does equal “age.” You’ll recall we assigned “age” a value of 30, so the computer returns “True.” Python also lets me perform actions based on conditional logic. Here, I tell the computer that if the value of my “age” variable is greater than or equal to 18, then it should print the word “adult.” Otherwise, it should
print the word “minor.” The value stored in the
“age” variable was 30, so the computer returns “adult.” Another common task in Python is looping. Looping performs the same action to each element of something. Here’s a simple loop. For each number in this
list of 1 through 5, we print the number. The computer outputs 1,
2, 3, 4, 5. Here’s another example. This time I’ll create a list containing the numbers 3, 6, and 9 and assign it to a
variable named “my list.” Now I’ll loop over “my list” and for each number in
the list the computer will print that number divided by 3. And there you have it! It outputs 1, 2, and 3. Now let’s return to the
conditional statement we wrote. If “age” is greater than or equal to 18, print “adult,” otherwise print “minor.” What if we want to repeat that same action for many different age values? We can write a function! A function is a chunk of code that can be reused to
perform the same task. We’ll define this function
and call it “is adult” and it will accept an
argument called “age.” By the way, an argument is information that you give a function
in its parentheses. Now, in the body of the function we’ll use the same code we used for the conditional statement. Nothing happens when we run this code, but check out what happens when we call the function and
give it an argument of 14… The computer returns the word “minor.” Now we can perform this same evaluation as many times as we want, simply by using the “is adult”
function we just created. And remember how I told you that libraries are part of what make Python such a useful and powerful language? Python has its own library of built-in functions
that perform common tasks. For example, here’s a list of numbers. 20, 25, 10, and 5. We’ll assign this list to a
variable named “new list.” We can use the built-in Python
function called “sorted” and enter “new list” as its argument. It returns our list with its values sorted from least to greatest. These are just a few simple examples of what Python can do. These simple processes can be stacked and layered and combined to create algorithms and programs that could
possibly change the world. Python’s power is bounded only by the limits of your imagination. Now that we’ve explored what Python is and what it can do, I’m excited for you to
learn more about it!

Reading: Python versus other programming languages

Reading

Video: Jupyter Notebooks

Jupyter Notebook is a popular open-source web application for creating and sharing documents containing live code, mathematical formulas, visualizations, and text. It is a preferred platform among data professionals because it offers a number of advantages over terminal-based text editors, including:

  • Modularity: Jupyter Notebook allows you to modularize your code into cells, which makes it easy to organize, move, and add or delete code.
  • Flexibility: Jupyter Notebook supports a variety of programming languages, including Python, R, and Julia. It also supports Markdown, which allows you to add formatted text, such as titles, bullets, tables, and mathematical formulas, to your documents.
  • Collaboration: Jupyter Notebook makes it easy to collaborate on data projects with others. You can share your notebooks with others and they can view and edit them in their own browsers.
  • Visualization: Jupyter Notebook provides a variety of tools for creating and displaying visualizations, such as charts, graphs, and images.

Overall, Jupyter Notebook is a powerful and flexible tool for data analytics. It is a good choice for beginners and experienced data professionals alike.

Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. It is a popular tool for data science and machine learning, but it can also be used for other tasks such as teaching, writing, and prototyping.

To get started with Jupyter Notebook, you can either install it on your local machine or use a cloud-based service such as Google Colab or Kaggle Notebooks.

Once you have Jupyter Notebook installed, you can create a new notebook by clicking the “New” button and selecting “Notebook”.

A new notebook will open in your browser. You can start by adding a title to the notebook and then adding code cells.

To add a code cell, click the “+” button in the toolbar and select “Code”.

You can then type your code into the code cell and press “Shift” + “Enter” to run it.

Jupyter Notebook will evaluate the code and display the output below the code cell.

You can also add Markdown cells to your notebook. Markdown is a lightweight markup language that allows you to format text using simple characters.

To add a Markdown cell, click the “+” button in the toolbar and select “Markdown”.

You can then type your Markdown text into the Markdown cell.

Jupyter Notebook will render the Markdown text below the Markdown cell.

You can also add visualizations to your notebook. To do this, you can use the plotting libraries that are available in the programming language that you are using.

For example, if you are using Python, you can use the Matplotlib library to create charts and graphs.

Once you have created a notebook, you can save it or export it to a variety of formats, such as HTML, PDF, and Markdown.

Here are some tips for using Jupyter Notebook:

  • Use code cells to organize your code into logical chunks.
  • Use Markdown cells to add text, equations, and images to your notebook.
  • Use visualizations to present your data and results in a clear and concise way.
  • Save your notebook regularly.
  • Export your notebook to a variety of formats so that you can share it with others or use it in other applications.

Jupyter Notebook is a powerful tool for data science and machine learning. It is also a good choice for teaching, writing, and prototyping.

Here are some examples of how to use Jupyter Notebook:

  • To analyze a dataset, you can use Jupyter Notebook to import the dataset, perform data cleaning and preprocessing, and create visualizations to explore the data.
  • To train a machine learning model, you can use Jupyter Notebook to load the training data, split the data into training and test sets, train the model, and evaluate the model’s performance.
  • To create a presentation, you can use Jupyter Notebook to add slides with text, equations, visualizations, and code.
  • To write a tutorial, you can use Jupyter Notebook to add text, equations, visualizations, and code to explain a concept or task.

Jupyter Notebook is a versatile tool that can be used for a variety of tasks. It is a good choice for beginners and experienced users alike.

Recently, you’ve been
investigating the power of Python. There are many environments
programmers use when exploring all of
Python’s capabilities, and one of the most popular
is Jupyter Notebook. You’ve already encountered
Jupyter Notebook. The examples that I
presented to you previously were done in one. We’ll use this platform to write code and perform analysis
throughout the course. We’ll also provide you with information about how to set up Jupyter Notebook on your own computer, but that’s optional and not necessary to complete the Advanced Data Analytics program. Jupyter Notebook is an
open-source web application for creating and sharing
documents containing live code, mathematical formulas,
visualizations, and text. Using a Jupyter Notebook
lets you collaborate on data projects and integrate code. Plus, it puts all of your
output in one document, which is very useful, especially when first
learning about programming. To illustrate why, take this example. In the computing world, most code is written in an
environment similar to this. This is a terminal-based text editor. Notice that it’s like a single
page that’s infinitely long. If I perform an operation
or write a line of code, it executes immediately when
I move to the next line, and I can only move forward. I can’t return to an earlier line of code, insert my cursor there,
and change or run it. A terminal-based text editor
is a very useful environment for many, many situations,
but it’s not always the best or easiest to work with for
data analytics projects. Now, compare that to our
notebook environment from before. Here, I can more easily
modularize my code into cells to organize them in sections. Cells are the modular
code input/output fields into which Jupiter
Notebooks are partitioned. I can move code around, add it, and delete it with the click of a mouse or the press of a button. And it’s great for
visualizations and presentations. I can add comments, annotations, and explanations using markdown syntax. Markdown lets you write formatted text in a coding environment
or plain-text editor. For example, I can add titles and bullets, tables, and mathematical formulas. These are just a few of the many features of Jupyter Notebooks that
make it a preferred platform among data professionals. As you move through this course, you’ll create projects
in Jupyter Notebooks to showcase your skills
as a data professional.

Reading: How to use Jupyter Notebooks

Video: Object-oriented programming

Python is a popular programming language for data analysis because it is approachable and powerful. Object-oriented programming (OOP) is a programming paradigm that organizes code around objects, which can contain both data and useful code that manipulates that data.

Classes are the most important concept in OOP. A class is an object’s data type that bundles data and functionality together. Methods are functions that belong to a class and typically perform an action or operation. Attributes are values associated with an object or class which are referenced by name using dot notation.

OOP is an ideal structure for data analysis because it allows you to package data together with ways to manipulate it and learn about it. This makes code more organized, accessible, and reusable.

Some examples of OOP in Python:

  • The string class has methods like swapcase(), replace(), and split().
  • The dataframe class has attributes like shape and columns.

Benefits of OOP for data analysis:

  • Organized code: OOP helps to organize code into logical units, making it easier to read and maintain.
  • Accessible code: OOP makes code more accessible by grouping related data and functionality together.
  • Reusable code: OOP promotes code reuse by allowing you to create classes and objects that can be reused in different parts of your program.

Overall, OOP is a powerful tool for data analysis because it allows you to write more organized, accessible, and reusable code.

Python Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm that organizes code around objects, which can contain both data and useful code that manipulates that data. OOP is a powerful tool for data analysis because it allows you to package data together with ways to manipulate it and learn about it.

Classes and Objects

A class is a blueprint for creating objects. It defines the data and functionality that an object will have. An object is an instance of a class. It is a concrete realization of the class’s blueprint.

For example, you could create a class called Dog. This class would define the data and functionality that a dog object would have, such as its name, breed, age, and the ability to bark. You could then create an object of the Dog class, such as fido = Dog().

Python

class Dog:
  def __init__(self, name, breed, age):
    self.name = name
    self.breed = breed
    self.age = age

  def bark(self):
    print("Woof!")

fido = Dog("Fido", "Golden Retriever", 2)

Attributes and Methods

Attributes are values associated with an object or class. Methods are functions that belong to a class and typically perform an action or operation on the object’s data.

For example, the fido object has the following attributes:

Python

fido.name
fido.breed
fido.age

The fido object also has the bark() method, which it can call to bark.

Python

fido.bark()

Inheritance

Inheritance is a powerful feature of OOP that allows you to create new classes based on existing classes. This allows you to reuse code and create more complex and sophisticated classes.

For example, you could create a new class called Poodle that inherits from the Dog class. The Poodle class would inherit all of the data and functionality of the Dog class, but it could also define its own unique data and functionality.

Benefits of OOP for Data Analysis

OOP offers a number of benefits for data analysis, including:

  • Organized code: OOP helps to organize code into logical units, making it easier to read and maintain.
  • Accessible code: OOP makes code more accessible by grouping related data and functionality together.
  • Reusable code: OOP promotes code reuse by allowing you to create classes and objects that can be reused in different parts of your program.
  • Modularity: OOP makes code more modular, making it easier to add new features or fix bugs.
  • Scalability: OOP makes code more scalable, making it easier to develop and maintain large and complex applications.

Examples of OOP in Python

The following are some examples of OOP in Python:

  • The string class has methods like swapcase(), replace(), and split().
  • The dataframe class has attributes like shape and columns.
  • The scikit-learn library provides a number of machine learning algorithms that are implemented using OOP principles.

Conclusion

OOP is a powerful tool for data analysis because it allows you to write more organized, accessible, and reusable code. If you are serious about data analysis, it is important to learn the basics of OOP.

Image:

The diagram above shows a simple example of OOP in Python. The Dog class is the parent class, and the Poodle class is the child class. The Poodle class inherits all of the data and functionality of the Dog class, but it also defines its own unique data and functionality.

There are four fundamental concepts in object-oriented programming, including objects. What are the other three concepts? Select all that apply.

Attributes, Methods, Classes

The four fundamental concepts in object-oriented programming include objects, classes, attributes, and methods.

You’re well on your way now! In this video, you’ll
learn more about Python and what makes it both approachable and powerful as a coding language. You’re going to learn about object-oriented programming. Specifically, we’re
going to discuss classes, methods, and attributes. What follows is a brief introduction to object-oriented programming. A more detailed discussion is beyond the scope of this course. You’ll have the opportunity to further explore
object-oriented programming in your future career
as a data professional. Object-oriented programming is a programming system that
is based around objects, which can contain both data and useful code that
manipulates that data. An object is an instance of a class. Think of it like a fundamental building block of Python. Lists, functions, strings…
these are all objects. The main idea behind
object-oriented programming is to have both data and the methods that manipulate it within objects, allowing for more organized, accessible, and reusable code. The most important concept in object-oriented
programming is the class. A class is an object’s data type that bundles data and
functionality together. In other words, the reason why it’s useful for an object to have a type – or belong to a class – is because it allows us to build a bunch of useful tools that can be packed directly into the object itself. This will make a lot more
sense with an example. When we put the words “hocus pocus” inside quotations and assign that to a variable called “magic,” this variable becomes an
instance of the string class. Because it belongs to the string class, it behaves in a certain way and has a lot of built-in functionality reserved for strings. We can swap the case of the characters by typing “magic dot swapcase” with empty parentheses after it. We can replace some characters with new characters by
typing “magic dot replace” and entering the characters
we want to replace and what we want to replace them with. And we can split the string into a list of two
strings using “dot split” and an empty pair of parentheses. These actions are known as methods. A method is a function that belongs to a class and typically performs an action or operation. They use parentheses. In our examples, each method acted on the value of our variable. It changed it in some way. That’s what I mean when I say that methods typically perform an action or operation. By the way, you don’t have
to memorize these methods. There aren’t many people
who know all of them. Most coding environments have ways to access a list of methods
available to a given class. In Jupyter Notebook, we type a dot and then hit the tab key. Notice that we’re attaching the method to its class instance using a dot. That is called dot notation, and it’s how we access the methods and attributes that belong
to an instance of a class. There are many different
classes in Python. You’ve encountered some of these already. The core classes of Python are: Integers, floats, strings, booleans, lists, dictionaries, tuples, sets, frozensets, functions, ranges, and none, which is a data type that
returns an empty value. There are also many additional
custom-defined classes that come with libraries and packages, and you can even make your own. Okay, the last thing we’re going to discuss is attributes. Attributes are values associated with an object or class which are referenced by
name using dot notation. They don’t use parentheses. Attributes are especially important for custom-built classes and more complex data
structures, like dataframes. You’ll learn more about these later, but here’s an example. Suppose we have a
dataframe called “planets” that contains a row for each planet and columns that represent planet name, its radius, and the
number of moons it has. One attribute of this
dataframe would be its shape. This dataframe is eight
rows by three columns. Another attribute of the
dataframe class is columns. Calling this attribute
on the dataframe object returns an index object containing the column names of the dataframe. Attributes allow you to access characteristics of a class, but they don’t do anything
to it or change it. Perhaps you’re beginning to appreciate how object-oriented programming is an ideal structure for data analysis. By packaging data together with ways to manipulate it and learn about it, objects are the
fundamental building blocks of Python and part of what makes it such a powerful tool
for data professionals.

Reading: More about object-oriented programming

Reading

Video: Hamza: How Python helped my data science career

Hamza is an applied machine learning engineer. He builds, maintains, and deploys large-scale models at work. He uses Python because it is a versatile and user-friendly language. He believes that the most important thing when learning is to be consistent, even if it is difficult at first.

Key points:

  • Python is a central theme of Hamza’s job as an applied machine learning engineer.
  • Hamza chose Python because it is versatile, user-friendly, and well-documented.
  • Python has helped Hamza become a better data scientist and machine learning engineer by helping him to understand a diverse set of mathematical applications in machine learning.
  • Hamza believes that the unique strength of Python is that it is a multifaceted tool that can be used for data manipulation, data cleaning, model building, model production, API development, and monitoring system development.
  • Hamza emphasizes the importance of being consistent when learning, even if it is difficult at first.

Overall message:

Python is a powerful tool for machine learning engineers and data scientists. It is versatile, user-friendly, and well-documented. Hamza encourages learners to be consistent, even when the learning curve is difficult.

[MUSIC] My name is Hamza and
I’m an applied machine learning engineer. I love building models,
I love building large-scale systems and that is exactly what I do at work. It’s a form of art for me, that you get to create something
from scratch that does not exist and you put it into production and it is
used by about a hundred million users. The scope of my work is to build,
maintain, and production allies large-scale models. So, Python is pretty much
the central theme of my job. Python is a programming language which
helps you manipulate data, which helps you build models and you can use it to create
production level models and softwares. There are a lot of other programming
languages which will do the same things that you’re looking for. For me personally, I use Python because
there’s a lot of documentation, there’s a lot of help. And there is a lot of learning from
past failures from different programming languages which has been incorporated
to help Python be user-friendly and be used by everyone around and
easy to adapt. You never get hired as a good chef
because you have good knives, you get hired as a good chef because
you have good cooking skills. This is the same thing. Python has helped me become
a better data scientist, better machine learning engineer. And it has helped me understand
diverse set of mathematical applications in machine learning
which previously I was not aware of. I think the unique strength about Python
is that it’s just some multifaceted tool. It’s not just one thing, it’s not for just
for data manipulation or data cleaning, you can do transformations, data cleaning, you can build models,
you can put them into production, you can make an API out of it, you can
build monitoring systems on top of that. Those are the strengths of Python, that makes you sort of master of all
in when it comes to data science. The one most important thing that when
you do an online course you’re studying anything is that learning is not linear,
there’s a very steep curve, and then eventually you just get to the,
aha, moment. The thing is that in the first two weeks
when you’re doing a certain course, you might be, okay, this is not making
sense and it’s not working for me and it’s not doing what I’m looking for,
so just stick to it and be consistent. The learning curve is always
deep in these things but then you eventually get to a point where
you say, I know all of these things and I can put all the knowledge that I have
together to build something great.

Practice Quiz: Test your knowledge: The power of Python

Fill in the blank: Jupyter Notebook is an open source _ for creating and sharing documents containing live code, mathematical formulas, visualizations, and text.

In object-oriented programming, objects can contain both data and useful code that manipulates that data.

What are examples of classes in Python? Select all that apply.

Use Python syntax


Video: Variables and data types

Variables in Python are like nouns in language. They point to values, but they’re not the values themselves. Variables can store values of any data type, such as strings, integers, floats, lists, dictionaries, and more.

To assign a new variable, it’s helpful to answer these questions: What’s the variable’s name? What’s the variable’s type? And what’s the variable’s starting value? These questions help you create variable names that remain meaningful and easy to reference again later.

Once you’ve assigned a variable, you can use it in expressions. An expression is a combination of numbers, symbols, or other variables that produce a result when evaluated.

For example, you could use the max() function to find the maximum age of the players on a basketball team by passing the list of ages to the function as an argument.

You can also convert variables to different data types. For example, you could convert the maximum age variable from an integer to a string by using the str() function.

It’s important to note that variables are dynamic, which means that they can point to objects of any data type. Also, there are no default types for most new variables, so you need to assign them before calling them.

Finally, the order that you run your cells in matters when coding in Jupyter Notebook.

Variables and Data Types in Python

Variables

A variable is a named storage location for a value. Variables can be used to store any type of data, such as numbers, strings, or lists. To create a variable in Python, you simply assign a value to it. For example:

Python

my_variable = 10

This creates a variable called my_variable and assigns it the value 10.

You can then use the variable name to access the value stored in the variable. For example:

Python

print(my_variable)

This will print the value 10 to the console.

Data Types

Python has a variety of data types, each of which can store different types of data. The most common data types are:

  • Integers: Integers are whole numbers, such as 1, 10, and -100.
  • Floats: Floats are decimal numbers, such as 3.14 and -1.5.
  • Strings: Strings are sequences of characters, such as “Hello, world!” and “This is a string.”
  • Lists: Lists are collections of items, such as [1, 2, 3] and [“one”, “two”, “three”].
  • Dictionaries: Dictionaries are collections of key-value pairs, such as {“name”: “Alice”, “age”: 25}.

Assigning Data Types to Variables

When you assign a value to a variable, Python will automatically infer the data type of the value. However, you can also explicitly specify the data type of a variable by using a type hint. For example:

Python

my_integer: int = 10
my_float: float = 3.14
my_string: str = "Hello, world!"
my_list: list = [1, 2, 3]
my_dictionary: dict = {"name": "Alice", "age": 25}

This is good practice, as it can help to make your code more readable and maintainable.

Using Variables in Expressions

Variables can be used in expressions to perform calculations. For example:

Python

sum = my_integer + my_float
print(sum)

This will print the value 13.14 to the console.

Conclusion

Variables are a powerful tool for storing and manipulating data in Python. By understanding variables and data types, you can write more efficient and effective code.

Here are some additional tips for using variables in Python:

  • Choose meaningful names for your variables. This will make your code more readable and easier to understand.
  • Use type hints to explicitly specify the data type of your variables. This can help to catch errors early and make your code more robust.
  • Avoid using global variables unless necessary. Global variables can make your code more difficult to debug and maintain.
  • Use local variables whenever possible. Local variables are scoped to the function in which they are declared, which makes your code more modular and reusable.

I hope this tutorial has been helpful. Please let me know if you have any questions.

In this video, we’re
going to focus on variables. Variables give meaning to code. Think about nouns in language; nouns are used to identify people, places, or things in a sentence. Variables in Python are like nouns. Variables point to values. They’re not the values themselves. If you have the expression
X = 3, X is the variable, and its stored value is 3. The 3 exists in a specific location in the computer’s memory. The X points to that location. Another way to think of a
variable is like it’s a container with a label on it. A container is a separate thing
from whatever it contains, but if I ask you to pass me the ketchup, you’ll know which bottle to pass me even if you can’t see
the ketchup inside it, because the bottle is labeled. Variables can store
values of any data type. A data type is an attribute that describes a piece of
data based on its values, its programming language, or
the operations it can perform. In Python, this includes
strings, integers, floats, lists, dictionaries, and more. You’ve already encountered
some of these in this course. We’ll explore these data
types throughout the program. When assigning a new
variable, it’s helpful to answer these questions before you code: What’s the variable’s name? What’s the variable’s type? And what’s the variable’s starting value? These questions help you
create variable names that remain meaningful and
easy to reference again later. Naming variables is important because these names are
reminders for yourself and others about what you are
storing in the variable. Recalling the data type
also helps you understand what the data can and can’t do. Next, consider how you
will assign expressions, which can help make
your code more concise. An assignment describes the process of storing a value inside a variable. And an expression is a
combination of numbers, symbols, or other variables that produce
a result when evaluated. Now, let’s examine that in Python. We’ll translate a variable
algorithm into Python code. Let’s start with a list of the ages of the starting five players on a professional basketball team. We’ll assign the list to a
variable called “age list.” Notice that we didn’t
call it, for example, X, because X doesn’t tell us anything about the value it contains, and if we encountered an X later, we might not remember
that it’s a list of ages. One nice thing about Python is that the computer
interprets the data type for us automatically when
we assign a new variable. This is called dynamic typing. Dynamic typing means variables can point to objects of any data type. Also, there are no default
types for most new variables. So we need to assign, or
initialize, before calling them. Let’s return to our example. We’ll find the maximum age
of the basketball players by using Python’s built-in “max” function and passing our age list
to it as an argument. We’ll assign the result to a
new variable called “max age.” When we call the variable, the computer returns a value of 34. This is an integer. So the “max age” variable contains a value whose data type is “integer.” Now suppose we want the “max age” variable to contain a string value. We can convert it to string
by using the string function, represented by S-T-R, and reassigning the result
back to the “max age” variable. Now our variable has
a data type of string, which is indicated by the
quotation marks in the output. You’ll learn more about strings later. For now, just remember that those quotation marks
are unique to strings. We can also overwrite the contents of the variable entirely, if we want. For instance, we can store
in it the text string “ninety-nine.” Now when we call the variable, the computer returns our new string. There are a couple of
important things to note here. First, notice that when
we converted “max age” from an integer to a string, we reassigned it back to itself. If we hadn’t done this, and we had simply used the string function on the “max age” variable, the computer would have returned a string, but the contents of the
variable would not have changed. Generally, when you want
to modify the contents of a variable, you have to reassign it. The second important thing to
be aware of is that the order that you run your cells in matters. For example, if I rerun the cell where I first assigned
the “max age” variable, and then call this variable in a new cell, you’ll notice that its
value has reverted back to the integer 34. It’s no longer the string, “ninety-nine.” In these examples, the value contained in the “max age” variable changed every time we reassigned it. That’s why it’s dynamic. Variables are convenient
because you can refer to them instead of the values they contain. So if we define a new variable
that contains the minimum age in our list of ages, we can
subtract the two variables to find the age difference of the oldest and youngest player. There is so much we can do with variables in expressions in Python. The program asks a question, and the variable helps
us capture the answer, based on input from a specific source. Just remember that if you
want to modify the contents of a variable, you usually
need to reassign it. It’s also important to consider that the order you run
the cells in matters when coding in Jupyter Notebook. Soon, we’ll learn about
variable-naming conventions and restrictions. I’ll meet you there.

Video: Create precise variable names

Python has naming conventions and restrictions for variables to maintain consistency and usefulness.

Naming conventions:

  • Avoid using keywords or existing function names.

Naming restrictions:

  • Variables can only include letters, numbers, and underscores.
  • Variables must start with a letter or underscore.
  • Python is case-sensitive.
  • Variable names cannot include parentheses.

Here are some examples of effective and ineffective variable names:

  • Effective: any_a_variable, any_a_variable_2
  • Ineffective: 1_is_a_number, Apples_&_oranges

Parentheses in Python follow the mathematical order of operations. If you don’t use parentheses, Python will follow the standard mathematical order of operations.

Naming conventions and restrictions for variables help data analytics professionals create meaningful code.

How to create precise variable names in Python

Why are precise variable names important?

Precise variable names are important for a number of reasons. First, they make your code more readable and understandable. When you read your code later, or when someone else reads your code, precise variable names will help you to quickly understand what each variable represents.

Second, precise variable names can help to prevent errors. If your variable names are vague, you are more likely to make mistakes when using them in your code. For example, if you have two variables named x and y, it can be easy to accidentally use one variable instead of the other. However, if you name your variables more precisely, such as customer_id and product_price, you are less likely to make this type of mistake.

How to create precise variable names

Here are some tips for creating precise variable names in Python:

  • Use descriptive names. Choose names that accurately describe the contents of the variable. For example, instead of naming a variable x, name it customer_name or product_quantity.
  • Avoid using abbreviations. Abbreviations can be confusing and difficult to understand. For example, instead of naming a variable cust_name, name it customer_name.
  • Avoid using generic names. Generic names, such as data or value, do not provide any information about the contents of the variable. Instead, use more specific names, such as customer_data or product_value.
  • Be consistent. Use the same naming convention for all of your variables. This will make your code more readable and easier to maintain.

Examples of precise variable names

Here are some examples of precise variable names in Python:

Python

customer_name = "Alice"
product_price = 10.99
order_total = 20.99

These variable names are descriptive, specific, and consistent. They accurately describe the contents of the variables, and they are easy to understand.

Examples of imprecise variable names

Here are some examples of imprecise variable names in Python:

Python

x = "Alice"
y = 10.99
z = 20.99

These variable names are not descriptive, specific, or consistent. They do not provide any information about the contents of the variables, and they are difficult to understand.

Conclusion

Creating precise variable names is an important skill for any Python programmer. By following the tips above, you can create variable names that will make your code more readable, understandable, and error-resistant.

Python has certain spelling
or grammatical rules to follow just like any other language. In programming, we call these
rules naming conventions and naming restrictions. If you completed the Google
Data Analytics Certificate, you might remember that naming conventions are consistent guidelines
that describe the content, creation date, and version
of a file in its name. Naming restrictions are rules built into the syntax
of the language itself that must be followed. In Python, there are some
important naming conventions to keep in mind. One of these is to avoid keywords. A keyword is a special
word that is reserved for a specific purpose and that can only be
used for that purpose. You’ve already encountered some keywords, such as “for,” “in,” “if,” and “else.” There are others too, which you’ll learn about soon. Keywords should never be
used when naming variables. Thankfully, you don’t have to worry about accidentally using a keyword. First of all, keywords will
appear in a special color in most coding environments. So a good rule to follow is that if you’re naming a variable and the name changes color, don’t use that name. And if you’re thinking, “But I like colorful variable names,” and you try to assign a
value to a keyword anyway, the computer will say “Not
so fast!” and throw an error. Python will let you
make spelling mistakes, but it won’t let you use a
keyword for a variable name. By the way, “throw an
error” is a common phrase that every coder knows only too well. It just means that the
computer returns an error and your code doesn’t successfully run. Also, some names are reserved
for existing functions, for example, “print” and “S-T-R.” So you should also avoid using
names of existing functions. To sum up, a big thing to keep in mind regarding variable naming conventions is that you don’t want to use reserved keywords or functions. Precision is essential when programming. This is why there are naming
restrictions for variables. For instance, variables
can only include letters, numbers, and underscores. This means you can’t use spaces, tabs, or special characters, such as the dollar sign or ampersand. Another rule to keep in mind is that, while variable names can contain numbers, they must start with a
letter or underscore. Also, Python is case-sensitive, which means capitalization matters. Lastly, variable names
cannot include parentheses. This is because parentheses
have other uses in Python. Let’s review some examples of effective and
ineffective variable names. “any_a_variable” is a valid variable name. “any_a_variable_2” is also good. However, “1_is_a_number” would be invalid because variable names must start with a letter or underscore. “Apples_&_oranges” is also invalid because it uses the special
character ampersand. You do have some flexibility
when naming your variables, since these are references you create; these conventions and restrictions just help make them consistent and useful. Okay, now let’s go back to parentheses to understand more about how
these function in Python. When doing calculations, the rules for parentheses follow the mathematical
order of operations. For instance, if we input 2 times 3 plus 4 in parentheses, Python will read 3 plus 4 first because it’s following
the order of operations. This is equal to 14. But, 2 times 3 in parentheses, plus 4 equals 10. That’s because the
operation within parenthesis will always be completed first. By the way, if we don’t
use any parentheses, Python will follow the standard mathematical
order of operations. Naming conventions and
restrictions for variables help maintain consistency and usefulness as you continue to use Python
for a variety of activities. A key part of working with Python as a data analytics professional is being able to effectively
name your variables to create meaningful code. Soon, we’ll explore
conversions and data types. Bye for now.

Fill in the blank: Naming _____ are rules built into the syntax of the language itself that must be followed.

restrictions

Naming restrictions are rules built into the syntax of the language itself that must be followed. 

Reading: Explore Python syntax

Reading

Video: Data types and conversions

  • Variables point to values that are stored in computer memory.
  • Data types can be strings, integers, and floats.
  • Strings are sequences of characters and punctuation.
  • Integers are whole numbers without fractions.
  • Floats are numbers with decimals.
  • Computers don’t know how to work with different data types.
  • Mixing different data types can throw an error.
  • You can use the type() function to identify data types.
  • Implicit conversion automatically converts one data type to another without user involvement.
  • Explicit conversion is where users convert the data type of an object to a required data type.
  • To explicitly convert a number to a string, use the str() function.
  • Debugging is figuring out what’s wrong when your code won’t work.
  • It’s common to search online for answers to programming errors.

Data types and conversions in Python

Data types

A data type in Python is a classification of data that tells the interpreter how to store and manipulate it. Python has a number of built-in data types, including:

  • Strings: Strings are sequences of characters, such as “Hello, world!” and “This is a string.”
  • Integers: Integers are whole numbers, such as 1, 10, and -100.
  • Floats: Floats are decimal numbers, such as 3.14 and -1.5.
  • Booleans: Booleans can be either True or False.
  • None: None is a special value that represents the absence of a value.

Conversions

Sometimes you may need to convert data from one type to another. For example, you may need to convert a string to an integer in order to perform a calculation. Python provides a number of built-in functions for converting data types.

Here are some examples of data type conversions:

Python

# Convert a string to an integer
string_to_integer = int("10")

# Convert an integer to a float
integer_to_float = float(10)

# Convert a float to a string
float_to_string = str(3.14)

# Convert a Boolean to a string
boolean_to_string = str(True)

Implicit conversions

Python also supports implicit conversions, which means that the interpreter will automatically convert one data type to another when necessary. For example, if you add two numbers, Python will automatically convert the numbers to integers or floats, depending on their types.

Here are some examples of implicit conversions:

Python

# Add two integers
integer_sum = 1 + 2

# Add an integer and a float
float_sum = 1 + 3.14

# Multiply a string by an integer
string_multiplication = "Hello, world!" * 2

Explicit conversions

Explicit conversions are conversions that are performed by the programmer. Python provides a number of built-in functions for performing explicit conversions.

Here are some examples of explicit conversions:

Python

# Convert a string to an integer using the int() function
string_to_integer = int("10")

# Convert an integer to a float using the float() function
integer_to_float = float(10)

# Convert a float to a string using the str() function
float_to_string = str(3.14)

# Convert a Boolean to a string using the str() function
boolean_to_string = str(True)

When to use implicit and explicit conversions

Implicit conversions are generally more convenient, but they can sometimes lead to errors. For example, if you add two strings, Python will automatically convert the strings to integers. However, if the strings contain non-numeric characters, Python will throw an error.

Explicit conversions are more reliable, but they can be more verbose. For example, to convert a string to an integer, you must use the int() function.

Generally, it is best to use explicit conversions when you are unsure of the data types of the values you are working with.

Conclusion

Data types and conversions are an important part of Python programming. By understanding data types and conversions, you can write code that is more efficient and less error-prone.

Fill in the blank: The _____ data type is a sequence of characters and punctuation that contains textual information.

string

The string data type is a sequence of characters and punctuation that contains textual information.

Previously, you learned about variables and how to name them. You learned that variables point to values that are stored in the computer’s memory. In other words, the variables are like containers, and the values they
store are their contents. Now, you’re going to learn more about the values that your
variables can contain. As you’ve learned, texts written between quotes
in Python is called a string. Programs need to manipulate data, which can come in a lot of
different forms or types. These data types include
strings, integers, and floats. First, a string is a sequence
of characters and punctuation that contains textual information. Strings are instantiated with single or double quotation marks or the string function. This is an immutable data type, which means the values can
never be altered or updated. An integer is a data
type used to represent whole numbers without fractions. And float data types represent numbers that contain decimals. Most computers understand when you tell them to add two integers, or add two strings. But generally speaking, computers don’t know how to
work with different data types. If you try to mix different data types, it can sometimes throw an error. As always, the computer tells
us the cause of the error. It’s like a little clue to help you improve
your programming skills. Read the errors carefully, understand what they’re
trying to tell you, and use that knowledge to fix the mistake. In this example, the last line of the error message says, “We’ve encountered something
called a TypeError.” Seven is being read as an integer and eight is being read as a string because of the quotation marks. No wonder we got an error: You can’t add seven to a word. As a data professional, you’ll often need to aggregate a lot of data of different types. This will require
converting the various types so they can be combined successfully. There’s an effective
method for doing this. But, first, it’s important to
know what you’re working with. Python offers a helpful way to identify data types
with the type function. You can use the type function to have the computer
tell you the data type. For instance, the type
function here tells us that A belongs to the S-T-R class, which is short for string. The number two belongs to
the I-N-T class, or integer. And 2.5 belongs to the float class. As a reminder, a class
is an object’s data type. The class bundles data and
functionality together. Now let’s find out how to combine these different data types. There are two ways to
convert data in Python. First is implicit conversion. Implicit conversion automatically converts one data type to another
without user involvement. Here’s an example. An arithmetic operations involving
both integers and floats, the interpreter works in the background and converts integers to floats. You don’t have to specify
anything in your code to do this. However, if you want to convert
numerical values to strings, you will need to do an
explicit conversion. Explicit conversion is where users convert the data type of an object
to a required data type. We use the predefined
functions I-N-T, float, and S-T-R. This is sometimes called typecasting, because the user casts
or changes the data type. Let’s use the S-T-R function inside the string we want to
have interpreted as output. And now the output of this
calculation will be stored as a string. Great work. You’ll continue exploring data types and seeking solutions to
errors throughout this program. Debugging, or figuring out what’s wrong when your code won’t work,
is a really useful skill for any data professional. Also, as a final tip: All of us in the profession, even experienced data
professionals and code developers, search for answers online
when we encounter an error. This is a common strategy
and a huge time saver. Always look to the data community for answers and inspiration.

Lab: Activity: Use Python syntax

Lab: Exemplar: Use Python Syntax

Practice Quiz: Test your knowledge: Using Python syntax

Variables can only store values of numeric data types.

What are best practices when assigning a new variable? Select all that apply.

Fill in the blank: An _____ is a combination of numbers, symbols, or other variables that produce a result when evaluated.

Which data type represents numbers that contain decimals?

Review: Hello, Python!


Video: Wrap-up

This section of the Python course covered the following topics:

  • Introduction to Python programming language and its benefits for data professionals
  • Jupyter notebooks and how to write Python code in the notebook environment
  • Object-oriented programming concepts and their benefits for data professionals
  • Working with variables, including naming conventions and standard data types
  • Converting and combining data types to organize data

The next section of the course will include a graded assessment. To prepare, students should review the reading list of new terms and revisit videos, readings, and other resources that cover key concepts.

We’ve come to the end of the first section of the Python course. You’ve developed a lot of
new Python skills already. Well done! Along the way, you’ve discovered that Python is a powerful
tool for data professionals, and learned how Python can help you work with your data quickly and efficiently. We began with a general introduction to the Python programming language and explored how data
professionals use Python to power their data analysis. Then we discussed Jupyter notebooks. You learned about the main
features of Jupyter notebooks, and how to write Python code
in the notebook environment. Next, you explored the benefits of object-oriented programming
for data professionals, and learned about its basic concepts. After that, we focused on
working with variables. You learned how to assign
specific values to variables, and effectively store and label your data. We also reviewed the standard naming
conventions for variables. You learned useful guidelines for making your code clear,
precise, and consistent. Finally, we explored
different data types in Python, such as integers, floats, and strings. You learned how to convert
and combine data types to organize your data. Coming up, you have a graded assessment. To prepare, review the reading that lists all the new terms you’ve learned. And feel free to revisit videos, readings, and other resources
that cover key concepts. Congratulations on your progress so far, and I’ll meet you again soon.

Reading: Glossary terms from Module 1

Terms and definitions from Course 2, Module 1

Quiz: Module 1 challenge

A data team researches which programming language they can use  to perform their coding work most effectively. They want a language that is easy to learn and has a supportive user community. Ultimately, they select Python. What are some other benefits of using Python for coding? Select all that apply.

Fill in the blank: The open-source web application _____ enables data professionals to create documents containing live code, mathematical formulas, visualizations, and text.

Fill in the blank: In Python, an _____ is a value associated with an object or class which is referenced by name using dot notation.

Fill in the blank: _____ is the process of storing a value in a variable.

A data professional names a variable in Python. To ensure they follow proper naming conventions, what may the variable name contain? Select all that apply.

Which of the following is an effective variable name? Mark all that apply.

In Python, which data type represents a sequence of characters and punctuation that contains textual information?

A data professional performs a calculation involving integers and floats. Python automatically converts the integers to floats. This is an example of what type of conversion?