100% tevredenheidsgarantie Direct beschikbaar na betaling Zowel online als in PDF Je zit nergens aan vast
logo-home
Summary 'Automate The Boring Stuff with Python' for Information & Data Management €6,49
In winkelwagen

Samenvatting

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

 12 keer bekeken  0 keer verkocht

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

Voorbeeld 3 van de 21  pagina's

  • Nee
  • 1-5
  • 7 februari 2023
  • 21
  • 2019/2020
  • Samenvatting
book image

Titel boek:

Auteur(s):

  • Uitgave:
  • ISBN:
  • Druk:
Alle documenten voor dit vak (17)
avatar-seller
bergceline1
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:

Voordelen van het kopen van samenvattingen bij Stuvia op een rij:

Verzekerd van kwaliteit door reviews

Verzekerd van kwaliteit door reviews

Stuvia-klanten hebben meer dan 700.000 samenvattingen beoordeeld. Zo weet je zeker dat je de beste documenten koopt!

Snel en makkelijk kopen

Snel en makkelijk kopen

Je betaalt supersnel en eenmalig met iDeal, creditcard of Stuvia-tegoed voor de samenvatting. Zonder lidmaatschap.

Focus op de essentie

Focus op de essentie

Samenvattingen worden geschreven voor en door anderen. Daarom zijn de samenvattingen altijd betrouwbaar en actueel. Zo kom je snel tot de kern!

Veelgestelde vragen

Wat krijg ik als ik dit document koop?

Je krijgt een PDF, die direct beschikbaar is na je aankoop. Het gekochte document is altijd, overal en oneindig toegankelijk via je profiel.

Tevredenheidsgarantie: hoe werkt dat?

Onze tevredenheidsgarantie zorgt ervoor dat je altijd een studiedocument vindt dat goed bij je past. Je vult een formulier in en onze klantenservice regelt de rest.

Van wie koop ik deze samenvatting?

Stuvia is een marktplaats, je koop dit document dus niet van ons, maar van verkoper bergceline1. Stuvia faciliteert de betaling aan de verkoper.

Zit ik meteen vast aan een abonnement?

Nee, je koopt alleen deze samenvatting voor €6,49. Je zit daarna nergens aan vast.

Is Stuvia te vertrouwen?

4,6 sterren op Google & Trustpilot (+1000 reviews)

Afgelopen 30 dagen zijn er 55628 samenvattingen verkocht

Opgericht in 2010, al 14 jaar dé plek om samenvattingen te kopen

Start met verkopen
€6,49
  • (0)
In winkelwagen
Toegevoegd