100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Summary 'Automate The Boring Stuff with Python' for Information & Data Management $6.97
Add to cart

Summary

Summary 'Automate The Boring Stuff with Python' for Information & Data Management

 12 views  0 purchase
  • Course
  • Institution
  • Book

English summary of all the required chapters of 'How to automate the boring stuff with Python' by Al Sweigart, ISBN: 97815 . Includes chapter 1-5 of the book and Jake Vanderplas: The Basics of NumPy Arrays. For the course Information & Data Management of the BSc Business Administration at the UvA

Preview 3 out of 21  pages

  • No
  • 1-5
  • February 7, 2023
  • 21
  • 2019/2020
  • Summary
avatar-seller
Python

Chapter 1: Python Basics
The interactive shell, also called the REPL (Read-Evaluate-Print Loop), lets you run (or execute)
Python instructions one at a time and instantly shows you the results.

Expressions consist of values (such as 2) and operators (such as +), and they can always evaluate
(reduce) down to a single value. Therefore, you can use expressions anywhere in Python code that
you could also use a value. An example of an expression is 2+2, which can be evaluated down to the
value 4. A single value with no operators is also considered an expression, though it evaluates only to
itself. E.g., the value 4 is also considered an expression.
Table 1 shows all math operators in Python:




The order of operations (precedence) of Python math operators is similar to that of mathematics:
The ** operator is evaluated first, followed by the *, /, //, and % operators from left to right, and
finally the + and – are evaluated from left to right. Parentheses can be used to override this
precedence. Whitespace in between the operators and values does not matter for Python, although
a single space is contention. The programmer must enter the expression, but Python will keep
evaluating parts until it becomes a single value.

A data type is a category for values, where every value belongs to one data type. Table 2 shows the
most common data types. Note that 1 is an integer (int), while 1.0 is a floating-point number (floats)
because it incorporates a decimal point. Strings are text values surrounded by single quote (‘)
characters, also known as strs (pronounce: stirs). A string with no characters in it (‘’) is called an
empty/blank string. If you see the error message SyntaxError: EOL while scanning string literal, you
probably forgot the final single quote character at the end of the string.

,The meaning of an operator depends on the data types of the values next to it. E.g., + is the addition
operator when it operates on 2 integers or floating-point values, but when used on 2 string values it
joins the strings as the string concatenation operator:
>>> ‘Alice’ + ‘Bob’
OUTPUT: ‘AliceBob’

However, if you try to use the + operator on a string and an integer value, Python will an error:
>>> ‘Alice’ + 42
Traceback (most recent call last):
File “<pyshell#0>”, line 1, in <module>
‘Alice’ + 42
TypeError: can only concatenate str (not “int”) to str
This means that Python thought you were trying to concatenate an integer to the string ‘Alice’. Your
code will have to explicitly convert the integer to a string because Python cannot do this
automatically.

The * operator multiplies 2 integer or floating-point values. When used on one string value and one
integer value, it becomes the string replication operator, evaluating the expression down to a single
string value that repeats the original string a number of times equal to the integer value.
>>> ‘Alice’ * 5
‘AliceAliceAliceAliceAlice’
The * operator can only be used with 2 numeric values or one string and one integer value. Python
cannot multiply words with words or words with a floating-point value.

If you want to use the result of an evaluated expression later in your program, you can save it inside
a variable: a box in the computer’s memory where you can store a single value. You can store values
in variables with an assignment statement consisting of a variable name, an equal sign (the
assignment operator), and the value to be stored. E.g. spam = 40 is a variable named spam with
the integer value 40 stored in it.
A variable is initialized (created) the first time a value is stored in it. After that, you can use it in
expressions with other variables and values.
>>> spam = 40
>>> spam
40
>>> eggs = 2
>>> spam + eggs
42
When a variable is assigned a new value, the old value is forgotten. This is called overwriting the
variable.
>>> spam = ‘Hello’
>>> spam
‘Hello’
>>> spam = ‘Goodbye’
>>> spam
‘Goodbye’

A good variable name describes the data it contains. It may not contain spaces, it cannot begin with a
number, and it can only use letters, numbers, and the underscore (_) character. No other special
characters are allowed. Variable names are case-sensitive, meaning that spam and Spam will be
different variables. Variables are usually started with a lowercase letter. Instead of underscores, you
can also use camelcase, meaning that every new word starts with a capitalized letter. E.g.,

, current_balance becomes currentBalance.




The interactive shell is used to run Python instructions one at a time, but to write entire programs,
the file editor is used. In the file editor, instructions will not be run when you press enter. Instead,
you can save a file with many instructions and then run the program. In addition, it does not have the
>>> prompt. You can start the program in the file editor so that it will start running in the interactive
shell. When there are no more lines of code to execute, the Python program terminates (stops
running/exits).

An example of a program, including explanation per line:
# This program says hello and asks for my name.
Every line following a hash mark (#) is called a comment and will be ignored by Python. It can be used
as a note to yourself. The # can also be used to temporarily remove a line of code while testing a
program. This is called commenting out and can be useful if you want to find out why your program
does not work.
print(‘Hello, world!’)
print(‘What is your name?’) # ask for their name
The function print() indicates that Python has to print out the text in the string, e.g. Hello, world!
When Python executes this line, it is calling the print() function and the string value is being passed
to the function. A value that is passed to a function call is an argument. You need to use parentheses
after a function name to identify it as the name of a function.
myName = input()
The input() function waits for to send some text. This function call evaluates to a string equal to the
user’s text, and the line of code assigns the myName variable to this string value.
print(‘It is good to meet you,’ + myName)
This expression will evaluate to a single string value that can be passed to print().
print(‘The length of your name is:’)
print(len(myName))
The len() function evaluates to the integer value of the number of characters in a string value. It is
then passed to print() to be displayed on the screen, which only allows you to pass it either integer
values or string values. Therefore you will get an error if you combine string values with an integer,
e.g. if you try:

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller bergceline1. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $6.97. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

50064 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$6.97
  • (0)
Add to cart
Added