As we embark on our next adventure in the Google IT Automation Certification journey, we tackle a fundamental skill in Python programming – performing mathematical operations. From saying your first “Hello, World!” to getting input from users and using Python as a pocket calculator, this section is a stepping stone into the seemingly complex world of Python. But fret not, with this Python Crash Course, we’ll make it as approachable and straightforward as possible. So, buckle up, as we dive into Python’s number crunching capabilities and uncover how they play a crucial role in everyday coding.
In our previous lesson, “Crash Course on Python Week 1 Section 2: What is Python?”, we explored the challenging yet rewarding landscape of coding, highlighting the importance of perseverance, collaboration, and seeking knowledge. We discussed breaking down complex projects into manageable tasks, gaining fresh perspectives by taking breaks, and seeking guidance when faced with obstacles. We emphasized the value of community in the developer and IT world, where sharing solutions and ideas is the norm. As we delve deeper into this subject, we’ll equip ourselves with strategies to become successful programmers, whether by enhancing our Python skills or pursuing the Google IT Automation Certification. Let’s forge ahead, knowing that coding is not a solitary journey but one best traveled with the support of others.
>>> print("Hello World!") Hello World!
When we execute this code on our local machine or a web interpreter, the words “hello world” appear on the screen within the terminal we are working in. This is the result of the “print” function, which is predefined in Python and also known as a keyword. This function allows us to write specific content on the screen, like the statement “Hello world.” The print function is an essential part of the Python language, and whenever we utilize keywords or functions inherent to the language, we employ the syntax to instruct the computer on what to do.
Functions are like little blocks of code that do specific tasks. They perform a unit of work, and we’ll explore them in detail later on. Keywords are special words that we use to give instructions. They form the foundation of a programming language and have specific rules for their usage. Once we understand how they work, we can use them to construct more complex expressions that get the computer to do what we want it to do.
Some common examples of keywords:
Values: True, False, None
Conditions: if, elif, else
Logical operators: and, or, not
Loops: for, in, while, break, continue
Functions: def, return
Additionally, notice how “Hello, World!” is written between double quotation marks. This is because in programming, wrapping text in quotation marks indicates that it is a string, which means it’s text that will be manipulated by our script. Anything outside of the quotation marks is considered part of the code.
In Python, you can write the hello world example in just one line. In C, it takes three lines, and in other languages, it can be even more.
Learning to write Hello World gives you a first impression of how functions are used and what a program in that language looks like. It’s simple, but now we’ll into concepts that can be slightly more difficult for completely new programmers.
Getting Information from the User
In order for a program to be truly useful, it needs to gather information from the user. This data allows the program to take actions that are specifically relevant to the user’s needs, rather than just generic actions like printing “Hello World!”. There are various ways to provide data to a computer, depending on the platform or application you’re using.
For instance, on a website, you can input data by typing text into fields or clicking on links. If you’re using a mobile app, you might interact by tapping buttons or choosing preferences from a dropdown menu. In a command line program, you can provide additional data by passing strings as parameters or by having the program prompt you for input. Each platform, program, or app processes data differently, whether it’s from a file, external sources, or processed in the background.
Let’s take the example of processing a list of people’s favorite movies, and removing duplicate titles from that list. The data put into the program is a list of movies, which could be given through a file that lists the movies. This is a limited view of how data can be provided to your program, but it’s a good starting point to understand the basic idea.
Let’s take a look at the basic idea of saving information, then having the program pull that information in when a function, or in this case the keyword “print”, calls for that data.
>>> movie = "Independence Day" >>> print("Top movie: " + movie) Top movie: Independence Day
Python as a Calculator
To demonstrate Python’s ability to calculate mathematical equations, we can start with some simple examples.
>>> print(6+6) 12 >>> print(10*8) 80 >>> print(1/10) 0.1
When we encounter repeating or periodic numbers, we use a longer format to represent them. Let’s take an example of dividing 1 by 3 in math theory. After the decimal point, the digit 3 repeats forever. However, it’s challenging to display an infinite repetition. So, we use a representation with many decimal places to approximate the repeating pattern.
>>> print(1/3) 0.3333333333333333
But how do we work with more complex equations that require the implementation of an order of operations, such as PEMDAS (Parentheses -> Exponents -> Multiplication-Division-> Addition-Subtraction)? Well, we’d use similar formatting as if we were writing the equation out ourselves, as Python will follow that same order of operations. Keeping in mind what operators look like in Python:
x + y Addition + operator returns the sum of x plus y
x – y Subtraction – operator returns the difference of x minus y
x * y Multiplication * operator returns the product of x times y
x / y Division / operator returns the quotient of x divided by y
x**e Exponent ** operator returns the result of raising x to the power of e
x**2 Square expression returns x squared
x**3 Cube expression returns x cubed
x**(1/2) Square root (½) or (0.5) fractional exponent operator returns the square root of x
x // y Floor division operator returns the integer part of the integer division of x by y
x % y Modulo operator returns the remainder part of the integer division of x by y
There are more operators included in Python, but we’ll get into those later.
>>> print(((3000/2)-40)*2) 2920.0
When you experiment with the math capabilities of Python, you learn how it can be used in certain situations. In IT jobs, there are plenty of tasks that require you to perform math calculations. For example, you might need to count how many times a specific word appears in a text or calculate the average time it takes for automated tasks to complete.
Whatever calculation you need to do, writing a script can help you do it faster and with greater accuracy. Python also offers advanced numeric functionalities that are widely used for data analysis, statistics, machine learning, and other scientific applications. So, by exploring Python’s math capabilities, you’ll be well-equipped for various computational challenges.
The section quiz covers the definitions of the basic concepts covered in the videos and study guide. Read more on:
Python’s mathematical capabilities are an integral part of the language, and they extend far beyond simple arithmetic. As we’ve seen, they can be instrumental in solving complex problems in the IT realm, from text analysis to automation tasks. Exploring these functions paves the way for more advanced computations, equipping you with the tools necessary for tackling data analysis, machine learning, and more. As we continue our Python Crash Course in the Google IT Automation Certification, remember each concept builds upon the last. So, keep practicing, keep exploring, and most importantly, keep coding!