100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Beginning Python_From Novice to Professional $4.99   Add to cart

Presentation

Beginning Python_From Novice to Professional

 4 views  0 purchase
  • Course
  • Institution

"Gain a fundamental understanding of Python’s syntax and features with this revised introductory and practical reference. Covering a wide array of Python–related programming topics, including addressing language internals, database integration, network programming, and web services, you’ll be...

[Show more]

Preview 4 out of 559  pages

  • August 9, 2024
  • 559
  • 2018/2019
  • Presentation
  • Unknown
avatar-seller
,1. Instant Hacking: The Basics
Magnus Lie Hetland1 and Fabio Nelli2
(1)
Trondheim, Norway
(2)
ROMA, Roma, Italy

It’s time to start hacking.1 In this chapter, you learn how to take control of your computer by
speaking a language it understands: Python. Nothing here is particularly difficult, so if you
know the basic principles of how your computer works, you should be able to follow the
examples and try them out yourself. I’ll go through the basics, starting with the excruciatingly
simple, but because Python is such a powerful language, you’ll soon be able to do pretty
advanced things.

To begin, you need to install Python, or verify that you already have it installed. If you’re
running macOS, Windows, or Linux/UNIX, open a terminal (the Terminal app on a Mac, or
Command on Windows), type in python, and press Enter. You should get a welcome
message describing the current version and operating system in which it runs. Furthermore,
some commands are suggested that could be useful, such as help, with which you can obtain
information on other commands. Finally, you’ll see a prompt consisting of the three
characters >>> as follows:
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

If everything went correctly as described, then it means you have just opened an interactive
interpreter session and the system is ready to accept any line of code in Python and execute
it after pressing the Enter key.

But before we start working with it, you can immediately check the currently
installed version on your system. If it is too outdated, you should update it to the latest
version released. A quicker way to know the Python version, without opening an interactive
interpreter session, is to write the following command in the terminal:
python --version

The details of the installation process will of course vary with your OS and preferred
installation mechanism, but the most straightforward approach is to
visit https://www.python.org/downloads/, where all versions of Python are listed,
including the latest release, each with a download link. For Windows and Mac, you’ll
download an installer that you can run to actually install Python. For Linux/UNIX, Python is
generally installed by default, but if this is not the case, it is always possible to install it via
APT or other advanced package tools (depending on the distributions).

,Note

The starting point for working with Python is to open an interactive interpreter session from
the terminal and start executing commands line by line. There are, however, many other
ways to develop and execute code in Python, some simpler and others more complex, which
make use of additional applications or web interfaces. We will look at some of these
throughout the book, but in the meantime I recommend taking a look at Appendix C, which
shows an overview of these methods, with a detailed description of their use.

The Interactive Interpreter
When you start up Python, you get a prompt similar to the following:
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The exact appearance of the interpreter and its error messages will depend on which
version you are using. This might not seem very interesting, but believe me, it is. This is your
gateway to hackerdom—your first step in taking control of your computer. In more
pragmatic terms, it’s an interactive Python interpreter. Just to see if it’s working, try the
following:
>>> print("Hello, world!")
When you press the Enter key, the following output appears:
Hello, world!
>>>

If you are familiar with other computer languages, you may be used to terminating every line
with a semicolon. There is no need to do so in Python. A line is a line, more or less. You can
add a semicolon if you like, but it won’t have any effect (unless more code follows on the
same line), and it is not a common thing to do.

So what happened here? The >>> thingy is the prompt. You can write something in this space,
like print("Hello, world!"). If you press Enter, the Python interpreter prints out the
string “Hello, world!” and you get a new prompt below that.

What if you write something completely different? Try it:
>>> The Spanish Inquisition
SyntaxError: invalid syntax
>>>

Obviously, the interpreter didn’t understand that.2 (If you are running an interpreter other
than IDLE, such as the command-line version for Linux, the error message will be slightly
different.) The interpreter also indicates what’s wrong: it will emphasize the
word Spanish by giving it a red background (or, in the command-line version, by using a
caret, ^).

, If you feel like it, play around with the interpreter some more. For some guidance, try
entering the command help() at the prompt and pressing Enter. You can press F1 for help
about IDLE. Otherwise, let’s press on. After all, the interpreter isn’t much fun when you don’t
know what to tell it.

Algo . . . What?
Before we start programming in earnest, I’ll try to give you an idea of what computer
programming is. Simply put, it’s telling a computer what to do. Computers can do a lot of
things, but they aren’t very good at thinking for themselves. They really need to be spoon-
fed the details. You need to feed the computer an algorithm in some language it
understands. Algorithm is just a fancy word for a procedure or recipe—a detailed
description of how to do something. Consider the following:
SPAM with SPAM, SPAM, Eggs, and SPAM: First, take some SPAM.
Then add some SPAM, SPAM, and eggs.
If a particularly spicy SPAM is desired, add some SPAM.
Cook until done -- Check every 10 minutes.

Not the fanciest of recipes, but its structure can be quite illuminating. It consists of a series
of instructions to be followed in order. Some of the instructions may be done directly (“take
some SPAM”), while some require some deliberation (“If a particularly spicy SPAM is
desired”), and others must be repeated several times (“Check every 10 minutes.”)

Recipes and algorithms consist of ingredients (objects, things) and instructions (statements).
In this example, SPAM and eggs are the ingredients, while the instructions consist of adding
SPAM, cooking for a given length of time, and so on. Let’s start with some reasonably simple
Python ingredients and see what you can do with them.

Numbers and Expressions
The interactive Python interpreter can be used as a powerful calculator. Try the following:
>>> 2 + 2
This should give you the answer 4. That wasn’t too hard. Well, what about this:
>>> 53672 + 235253
288925
Still not impressed? Admittedly, this is pretty standard stuff. (I’ll assume that you’ve used
a calculator enough to know the difference between 1 + 2 * 3 and (1 + 2) * 3.) All the
usual arithmetic operators work as expected. Division produces decimal numbers,
called floats (or floating-point numbers).
>>>
0.5
>>>
1.0
If you’d rather discard the fractional part and do integer division, you can use a double
slash.

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 RobertCuong. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

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

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

79978 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
$4.99
  • (0)
  Add to cart