Giter VIP home page Giter VIP logo

python-basics-exercises's Introduction

In this code repository you find the solutions and sample implementations for the solutions and challenges posed in our Python Basics book. All solutions and sample files are ordered by chapter so you can quickly navigate to the code you're looking for.

In most cases, the solutions presented here represent just one way out of many that the exercises and challenges can be solved. If you find a better way to solve one of the exercises or challenges feel free to open an issue or pull request!

Get the Book

» Click here to learn more about the book and get your copy

Downloading the Files

With git

If you have git installed, the easiest way to access these files is to clone the repository to the directory of your choice:

$ git clone https://github.com/realpython/python-basics-exercises.git

Without git

Alternatively, you can download the entire repository as a .zip file from the repository's homepage using the green "Clone or download" button in the top right hand corner, or by clicking here.

Running the Solutions & Code Examples

To run the solution code and code examples, first make sure you have Python 3 installed on your machine. If you need help installing Python 3, check out our Python 3 Installation & Setup Guide.

Example: To run the 3-store-a-variable.py exercise, type python3 3-store-a-variable.py into your terminal. Here's how that looks, with output:

$ python3 ch03-first-python-program/3-store-a-variable.py
hello
hi

Note: Depending on your installation, you may need to type python3.9 or python39 to run the examples.

python-basics-exercises's People

Contributors

dbader avatar fheisler avatar martin-martin avatar michaeljpeake avatar mjhea0 avatar somacdivad avatar terrameijar avatar terwebs avatar topleft avatar ybrenning avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-basics-exercises's Issues

Page 379

typo in:
>>> daily_tempraturees = []

Total vote count or win 2 of 3 regions?

In reading the exercise, the winner has to win 2 of 3 regions. This code seems to count wins as total vote counts across all regions. So the exercise was to do an electoral college kind of win, but your solution is counting the popular vote. Am I misinterpreting your code? Thanks, love the book and the exercises.

Page 377

>>> file = file_path.open(mode="w", encoding="utf-8")
Now create a new CSV writer object by passing the file object file to csv.writer():
>>> writer = csv.writer()
You did not passed the file created earlier and running the code by just looking to the code provided, results in error:
TypeError: expected at least 1 argument, got 0

Page 314

Original Text
Don’t worry about how the datetime class works right now. The important part of this example is that having to constantly time datetime.datetime anytime you want to use the datetime class is redundant and tiring.

Suggested Edit
Don’t worry about how the datetime class works right now. The important part of this example is that having to constantly type datetime.datetime anytime you want to use the datetime class is redundant and tiring.


The word "type" was misspelled as "time."

Page 374

>>> file_path = Path.home() / "temperatures.txt"
but in page 375 says:
This creates a file called temperatures.csv in your home directory andopens it in append mode.
Obviously it creates temperature.txt not temperature.csv

Alternative Solution with List (quite similar to Dictionary)

Quite short with two nested for loops:

cat_count = 100
cats = [False for i in range(cat_count)]



for step in range(0, cat_count):
       for selected_cat_index in range(step, cat_count,step + 1 ):
           cats[selected_cat_index] = not cats[selected_cat_index]
   

for i in range(cat_count):
    if cats[i]:
        print(f"Cat {i+1}: {cats[i]}")

Translation to portuguese

Hello, I'm interested in translating some exercises to portuguese. A lot of people here in Brazil are interested in practicing with these exercises, so this could make it more accessible for those people.

Thanks

Can this be done more efficiently?

I notice you doing the same thing repeatedly for nouns, verbs, and adjectives. Would a for loop work here to loop through a range (3) for items in noun, verb, adjective? I can't figure out how to do it though.

2023-07 : examples are too old to run with latest editions of Python (3.11.4) and pip packages

As of July 2023, the example code is too old to run with the latest editions of Python (3.11.4) and packages (e.g., pypdf).

Symptoms include (but are not limited to) deprecation warnings almost immediately when trying the code such as 2-example-app-pdf-page-rotator that make it next to impossible to run the examples.

I will totally understand if there is no cost/benefit to you to upgrade the examples from four years ago (according to GitHub). I'll just not buy the products or services associated with Real Python and seek other ways to educate myself and save myself the investment in "dead code".

Possible Issue?

Hello, I'm going through the Python Basic book and going through the excersises and I've come across a possible code error in the PDF page 85, question 5. The solutions to exercises shows the solution, but when entered it doesn't show as True as the instructions says. I've already tried the solution on both Python 2 and Python 3 and no luck. Can someone, please advise?

Github: ch04-strings-and-string-methods/3-manipulate-strings-with-methods.py
Python Basic book PDF: Pg: 85, Question 5

sc1

sc2

sc3

Page 325

Original Text
However, deeply nested subpackages introduce long dotted module names. You can image how much typing it would take to import a module from a subpackage of a subpackage of a subpackage of a package.

Suggested Edit
However, deeply nested subpackages introduce long dotted module names. You can imagine how much typing it would take to import a module from a subpackage of a subpackage of a subpackage of a package.


The word "imagine" was misspelled as "image."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.