• Problems for writing in python. Learning Python from scratch (my story). What is an iterator

    I am periodically asked about test tasks on Python topics. I decided to summarize the questions and write them in one place. I don't use these questions and tasks in interviews, but I do use them in training.

    Data types, basic constructs

    1. How to get a list of all attributes of an object
    2. How to get a list of all public attributes of an object
    3. How to get a list of methods on an object
    4. What "magic" variable stores the contents of help?
    5. There are two tuples, get the third as a concatenation of the first two
    6. There are two tuples, get the third one as the union of the unique elements of the first two tuples
    7. Why is it that if a list is changed in a loop, then for x in lst[:] is used, which means [:] ?
    8. There are two lists of the same length, one contains keys, the other contains values. Make a dictionary.
    9. There are two lists of different lengths, one contains keys, the other contains values. Make a dictionary. For keys that have no values, use None as the value. Ignore values ​​for which there are no keys.
    10. There is a dictionary. Invert it. Those. pairs key: value swap - value: key.
    11. There is a string in Unicode, get an 8-bit string in utf-8 and cp1251 encoding
    12. There is a string in cp1251 encoding, get the Unicode string

    Functions

      Write a function that can be passed arguments either in a list/tuple or one at a time. The function sums all arguments.

      >>> f(1, 2, 3) 6 >>> f() 6 >>> f((3, 5, 6)) 14 >>> f(3, (5, 6)) 14

      Write a factory function that will return an addition function with an argument.

      >>> add5 = addition(5) # addition function returns the addition function with 5 >>> add5(3) # returns 3 + 5 = 8 8 >>> add5(8) # returns 8 + 5 = 13 13 >>> add8 = addition(8) >>> add8(2) # will return 2 + 8 = 10 10 >>> add8(4) # will return 4 + 8 = 12 12

      Write options with the usual “internal” and anonymous lambda functions.

      Write a factory similar to step 2, but returning a list of such functions

      >>> additionals = addition_range(0, 5) # list of addition functions from 0 to 5 inclusive

      those. similar

      Write an analogue of map:

      • the first argument is either a function or a list of functions
      • the second argument is a list of arguments that will be passed to the functions
      • it is assumed that these functions are functions of one argument
      >>> mymap(, ) [(1, 2, 3), (2, 3, 4), (3, 4, 5)]

      in this case, the “expanded” entry will be: [(add0(1), add0(2), add0(3)), (add1(1), add1(2), add1(3)), (add2(1), add2(2), add2(3))]

    Iterators

      Write a generator function cycle that would return a cyclic iterator.

      >>> i = iter() >>> c = cycle(i) >>> c.next() 1 >>> c.next() 2 >>> c.next() 3 >>> c.next () 1

      Write a chain generator function that sequentially iterates the passed objects (an arbitrary number)

      >>> i1 = iter() >>> i2 = iter() >>> c = chain(i1, i2) >>> c.next() 1 >>> c.next() 2 >>> c. next() 3 >>> c.next() 4 >>> c.next() 5 >>> c.next() Traceback (most recent call last): ... StopIteration

    Write doctests for functions and iterators

    Modules

    1. We have an imported module foo, how can we find out the physical path of the file from where it is imported?
    2. From the foo module you import the feedparser module. Version X of feedparser is in the system-wide site-packages directory, version Y is next to the foo module. The PYTHONPATH environment variable is defined, and feedparser is also there, version Z. Which version will be used?
    3. How can I see the list of directories where Python looks for modules?
    4. You have a module foo, inside it the module bar is imported. Next to the foo module there are files bar.py and bar/__init__.py Which module will be used.
    5. What does the construction __name__ == "__main__" mean and what is it used for?

    Classes

      Write a base Observable class that would allow descendants to:

      1. when passing **kwargs, enter the corresponding values ​​as attributes
      2. make sure all public attributes are displayed when printing
      >>> class X(Observable): ... pass >>> x = X(foo=1, bar=5, _bazz=12, name="Amok", props=("One", "two")) >>> print x X(bar=5, foo=1, name="Amok", props=("One", "two")) >>> x.foo 1 >>> x.name "Amok" > >> x._bazz 12
    1. Write a class that, by all appearances, would be a dictionary, but would allow keys to be accessed as attributes.

      >>> x = DictAttr([("one", 1), ("two", 2), ("three", 3)]) >>> x ( "one": 1, "three": 3, "two": 2) >>> x["three"] 3 >>> x.get("one") 1 >>> x.get("five", "missing") "missing" >>> x .one 1 >>> x.five Traceback (most recent call last): ... AttributeError

      Point 2 with a complication: write the parent class XDictAttr so that the successor’s key is dynamically determined by the presence of the get_ method .

      >>> class X(XDictAttr): ... def get_foo(self): ... return 5 ... def get_bar(self): ... return 12 >>> x = X(("one": 1 , "two": 2, "three": 3)) >>> x X: ( "one": 1, "three": 3, "two": 2) >>> x["one"] 1 > >> x.three 3 >>> x.bar 12 >>> x["foo"] 5 >>> x.get("foo", "missing") 5 >>> x.get("bzz", "missing") "missing"

      Write a class that registers its instances and provides an iterator interface over them

      >>> x = Reg() >>> x >>> y = Reg() >>> y >>> z = Reg() >>> for i in Reg: ... print i

    Write unit tests, based on the tests above, but not limited to them.

    Metaclasses and descriptors

    1. What are they used for, what arguments do they receive, what should they return: methods __new__ and __init__ classes
    2. What arguments does __new__ and __init__ receive for a metaclass?

      Implement descriptors that capture the attribute type

      >>> class Image(object): ... height = Property(0) ... width = Property(0) ... path = Property("/tmp/") ... size = Property(0) > >> img = Image() >>> img.height = 340 >>> img.height 340 >>> img.path = "/tmp/x00.jpeg" >>> img.path "/tmp/x00.jpeg " >>>

      Implement a base class (using a metaclass) that captures the attribute type

      >>> class Image(Object): ... height = 0 ... width = 0 ... path = "/tmp" ... size = 0 >>> img = Image() >>> img.height = 340 >>> img.height 340 >>> img.path = "/tmp/x00.jpeg" >>> img.path "/tmp/x00.jpeg" >>> img.path = 320 Traceback (most recent call last): ... TypeError

      Implement a base class (using a metaclass) and descriptors that would create an SQL schema (ANSI SQL) for the model based on the class:

      >>> class Image(Table): ... height = Integer() ... width = Integer() ... path = Str(128) >>> print Image.sql() CREATE TABLE image (height integer, width integer, path varchar(128))

      There is no need to implement NULL, PRIMARY KEY, FOREIGN KEY, CONSTRAINTS. It is enough to implement two types: Integer and Str.

    More popular than ever, it is used wherever possible, from backend servers to the development of frontend games, and so on. Python is rightfully considered a general-purpose language and is quickly becoming a must-have tool in the arsenal of any self-respecting programmer.

    But Python isn't popular just because it's popular. It's easy to learn, reads like pseudo-code, and is very dynamic. However, learning a new language can be quite a challenge; finding the right place to study and people from whom you can learn new information will solve half the problem. Our guide will help you with this. This will be your foolproof plan that will make learning Python easier and make learning fun and interesting.

    Task 1: Start with the Basics

    As of this writing, there are two main versions of Python: Python 2.7 and Python 3.2. Which version you choose to study is not so important, since the differences will be minimal, especially for a beginner. But you should know that Python 2 supports more third-party libraries, while Python 3 mainly focuses on developers working on improving the language. The choice is yours, if the code from any tutorial does not work as intended, make sure you are using the appropriate version of Python from that tutorial.

    Wikibooks has always been a reliable source for learning something new and Python is no exception. Here you will find a great series of tutorials to help you become more familiar with Python. There won't be a lot of tech jargon and you can start writing code pretty quickly, which will definitely be a rewarding experience. Therefore, I recommend this site as a good place to start your Python journey.

    You won't find a better source of information than the official documentation on python.org. However, if you want to start right away from here, this may not be a good idea.

    The content here is more technical than wikibooks, which in turn will be useful later as you progress in learning the language. For a beginner, the content may seem complicated and this will prove to be an obstacle in learning this simple and beautiful language.

    For beginners, the most noticeable difference between Python 2 and Python 3 will likely be the ability in Python 2 to use print without parentheses. Parentheses are required in Python 3, that's all.

    Task 2: tutorials and screencasts

    The NewBoston playlist is always great and you can learn a lot of languages. "Bucky" is an excellent instructor, he is interesting to listen to as he has found a balance between being funny and informative, what he is trying to convey is always easy to understand. I recommend checking out any of his playlists - especially the Python playlist. You don't need to have any programming knowledge, after watching his video tutorials you will have a solid understanding of the language.

    Nettuts+ Python from scratch

    A good introduction to Python is Giles Lavelle's course. As with TheNewBoston's series, Lavelle assumes you have no programming experience.

    If you want to see real-world applications or want to understand Python web development, this series is perfect.

    In this screencast, you will create a dynamic website from scratch using the Django Python framework.

    Python screencast from ShowMeDo

    StackOverflow isn't just known for newbies, bugs, and problems.

    ShowMeDo has a huge catalog of Python-related videos. While the site may not seem like it has the best user interface, it has a huge range of useful videos, ranging from information for beginners to advanced techniques for using Python. You should definitely check out this site.

    Create a Python bot that can play web games

    In this tutorial you will get acquainted with specific material, I do not recommend it to absolute beginners. However, I think it's worth mentioning. In this tutorial, Chris Kiehl will show you how to create a very interesting Python bot that will play a simple game for you. The tutorial will show you the power of Python; it can be useful for solving everyday tasks that can be used to manage your computer.

    Task 3: Free eBooks

    A good book is a great help when learning anything new, and Python has an amazing and friendly community, resulting in a large selection of free eBooks. Below you will see a small list of the best books. You can download a free electronic version of each of them or buy a printed edition (or donate) if you want to support the author, I'm sure they will appreciate it.

    Learn Python the Hard Way

    Despite the book's title, "The Not Easy Approach to Learning Python," learning Python is still a breeze—as it should be! In this book, Zed A. Shaw gives you a complete and detailed guide, with problems and examples to test your programming skills. The book is written in an informal language, but it describes many details in detail, so you will not have any difficulties reading it and the result will not be long in coming.

    Think Python: How to Think Like a Computer Scientist

    You won't find a better source of information than the official python.org documentation.

    As the subtitle says, "Thinking Like a Programmer," you'll find a lot of theoretical material here. Beginners might get frustrated and find the book difficult, but trust me, it's worth reading as you'll find information about algorithm theory and high-level concepts.

    Invent With Python

    If you are more interested in the practical part, creating your own game will be a truly rewarding experience! In this book, Al Sweigart assumes that you are not comfortable with Python and helps you create a game. Although this book is about game development, it is also quite suitable for complete beginners. Later in this article, I mentioned a similar book, but it requires a strong command of Python. If you feel that you know the language well, then another book will be a more informative source for you.

    The Django Book

    If you want to learn web development in Python, you will most likely use the Django framework. You may not be familiar with the Django framework, but you should have a good understanding of Python to read this book. The information in it is indispensable for any beginning web developer.

    Books on Python

    If all of the above books are not enough or you are interested in a specific topic, follow this link. The guys from python.org have created an extensive list of books, sorting them by complexity and topic.

    Task 4: Get to know StackOverflow

    Thousands of developers have experienced the same problems that you will have to face head-on. StackOverflow is a great source of information where any developer can find a solution to their problem. When you encounter another bug and you have no idea how to fix it, look for the answer on StackOverflow. Most likely, there is already information there on how other people solve a similar problem.

    But StackOverflow isn't just known for newbies, bugs, and problems; This site is full of very smart people willing to help - learn from them!

    Most of the tricks and tips you will find here are not found in any of the tutorials and will be very useful for advanced or intermediate Python users.

    Task 5: Project Euler


    Project Euler (pronounced 'Oil-er', remember this so you don't lose face in the future) is one of my favorite websites. Once you create an account, you can participate and solve approximately 400 challenges on this website Each task is 50 percent mathematics, 50 percent programming, in my opinion, the most correct approach to studying each of these subjects.

    The tasks begin with simple ones to test your level of language knowledge, after which the complexity will increase, and eventually there will be problems that even an experienced programmer cannot solve. In the end, you will have to find the most efficient algorithm - in case you don't have time, wait several hours to calculate the correct answer.

    Nothing will make you a better programmer faster than finding the most efficient solution to Project Euler problems.

    When you manage to get a solution to another problem, a forum page will open for you, where people discuss solutions and ideas with each other regarding this problem. Most of these solutions will be in Python. This is the very key that will help you significantly improve your programming abilities. If someone has found a faster solution, don’t be afraid to take the time to analyze it, so you can understand what points need to be reworked. Over time, you will begin to understand all the tricks and your Python language skills will improve, the result will not be long in coming.

    In addition to this, there are several informative blogs where you can find solutions to Python problems on Project Euler. If you get stuck on a problem, it's okay if you look at the solution of other developers, the most important thing is to learn something new from them. Here are two of my favorite blogs:

    Task 6: Create a game

    Apart from creating your own game, there are only a few things that can be fun.

    Apart from creating your own game, there are only a few things that can be fun. It can be quite a steep learning curve, but it's definitely worth it. PyGame is the most famous library for developing games in Python, and you won't have a hard time finding free tutorials about it. Below are the best of them.

    Official PyGame documentation

    As with Python tutorials, the PyGame developers have provided documentation - an introduction. The material is full of technical terminology, in case you want to start developing games right away. However, as always, the developer documentation is the best source of information; that's why I recommend this site.

    Invent With Python (With PyGame)

    A free e-book from AI Sweigart introduces the reader to the PyGame library, even if you don't know anything about this library, after reading it you will be able to make a couple of games. Simple games will be an excellent foundation for creating your own project, if you have the desire. Sweigart provides detailed comments on his code to help with the learning process.

    This is another playlist from TheNewBoston. An excellent introduction to PyGame. Again, you may not be familiar with this library; by reading the book you will begin to understand PyGame, but unlike InventWithPython you will not make a full-fledged game.

    Task 7: Explore Popular Libraries and Tools

    Python is a general-purpose language with which you can do almost anything; We have an endless range of libraries and tools at our disposal. Below is a list of the most popular.

    PyPy

    If you want to collect information from HTML pages... BeautifulSoup will do everything you need and save a huge amount of time.

    When doing things that require a lot of CPU resources and you feel like Python is consuming those resources, PyPy will come to the rescue. PyPy is an alternative compiler for Python that can speed up computation.

    NumPy + SciPy

    These libraries are usually used together (SciPy depends on NumPy). If you have to do work related to mathematical calculations or scientific research, these libraries will serve as an excellent assistant. NumPy and SciPy expand the mathematical functions and capabilities of Python, as a result of which they will significantly speed up the solution of tasks.

    BeautifulSoup

    BeautifulSoup is absolutely amazing. Parsing information from HTML pages can be quite tedious and frustrating. BeautifulSoup will do everything for you and save you a huge amount of time. I highly recommend this library, it's fun to work with.

    Python Image Library

    The Image Processing Library (PIL) is perfect for any image processing task. If there is a need to interact with the image in any way, PIL will most likely help accomplish this task.

    Django

    As I mentioned in this article, if you are interested in web development, your choice is the Django framework. This is the most popular Python framework and there are a huge number of training resources on it.

    Challenge 8: Contribute to open source projects

    With a decent understanding of the language, being able to read and navigate other people's code is an essential skill and will also prove to be a great way to learn.

    This is why open source projects are so popular. Github and Bitbucket websites are where you should start. Don't worry, if people criticize your code, you shouldn't contribute to those projects immediately. You can always work on a separate branch of this project, figure out how it works and do whatever you want with it. If you suddenly find things that need to be improved, great! Do this and submit your improvements. That's what open source projects are for.

    Conclusion

    I hope I was able to provide a reliable base for learning Python for you. Just in case I forgot to mention any resources, please let me know in the comments below to help other users!

    • Goal 1- help with links, materials, for those who are planning to study programming and take Python as their first language. Show that it is not as difficult as it seems.
    • Goal 2- collect links to useful and interesting materials on this topic in the comments.

    0. Will I succeed?

    From the very beginning I doubted that I would be able to do anything more than Hello World. It seemed to me that programming was extremely difficult and extremely magical. In addition, there is work, hobbies, family, which will distract from full study.

    I shouldn't have been afraid and I don't recommend it to you. Programming will probably never become my main profession, but it is a great way to be creative. This is chess and Civilization in one bottle.

    Everything is simpler than it seems and much more interesting.

    1. Literature

    Mark Lutz “Python Programming”- it is recommended to read it on many forums and courses. It seemed to me too detailed and loaded for a beginner. Read a lot, program a little. It is much more useful to read it after mastering Python at least.

    Mark Summerfield “Python 3 Programming”- dynamically, with excellent examples and tasks. Without unnecessary in-depth, which only complicates everything at the beginning. I recommend starting with this book; it will help you quickly understand without intimidating you with difficulties.

    All other books turned out to be less useful and informative. In general, it is difficult to just pick up and buy good literature on this topic in a store or in a digital version.

    2. What to read on the Internet

    http://pythonworld.ru/ - talks about the basics of the language in simple and understandable language, often used as a cheat sheet.

    After another two months, I was able to create my first Django application. But the main thing is that now I have enough knowledge for independent development and learning. The hardest thing is to get to this point.

    Another month later, I joined two projects on GitHub and am participating in them. Of course, I still solve simple problems, but in return I receive advice and training.

    Tags: python training, programming training

    Every Python learner needs to write sticky code. We present to your attention several tasks to implement (not too simple (except for the first) and not too complex).

    There is also a repository for these tasks with tests and my solutions (to test yourself).

    To run tests for your function, the easiest way is to add the code from the tests folder to the end of the function file.

    And now, actually, the tasks:

    Simple arithmetic operations (1)

    Write a function arithmetic that takes 3 arguments: the first 2 are numbers, the third is the operation that should be performed on them. If the third argument is + , add them; if - , then subtract; * - multiply; / - divide (first by second). Otherwise, return the string "Unknown operation".

    Leap year (2)

    Write a function is_year_leap that takes 1 argument - the year, and returns True if the year is a leap year, and False otherwise.

    Square (3)

    Write a function square that takes 1 argument - the side of the square, and returns 3 values ​​(using ): the perimeter of the square, the area of ​​the square and the diagonal of the square.

    Seasons (4)

    Write a function season that takes 1 argument - the month number (from 1 to 12), and returns the season to which this month belongs (winter, spring, summer or autumn).

    Bank deposit (5)

    The user makes a deposit of a ruble for a period of years at 10% per annum (every year the size of his deposit increases by 10%. This money is added to the deposit amount, and there will also be interest on it next year).

    Write a function bank that takes arguments a and years and returns the amount that will be in the user's account.

    Prime numbers (6)

    Write a function is_prime that takes 1 argument - a number from 0 to 1000, and returns True if it is prime, and False otherwise.

    Correct date (7)

    Write a date function that takes 3 arguments - day, month and year. Return True if such a date is in our calendar, and False otherwise.

    XOR encryption (8)

    Write a function XOR_cipher that takes 2 arguments: the string to be encrypted and the encryption key, which returns the string encrypted by XORing the characters of the keyed string. Also write a function XOR_uncipher, which, using an encrypted string and a key, restores the original string.

    Learning to program starts with small steps. It is clear that first you need to study the syntax and concept of the chosen language. But no less important is honing your skills in creating algorithms and solving programming problems. This article lists resources that contain all kinds of tasks and ideas for your own projects.

    Here you can find a list of over 100 project ideas that you can implement. The list is designed for Python, but the ideas can be implemented in other languages. It contains both not very serious projects and good exercises for practice. The list is highly recommended for beginners to read and follow.

    And here you will find a list of 49 game clones that you can write. The choice depends on the level of difficulty.

    Want to create something new? Check out the Idea Machine and The Internet Wishlist, where people post suggestions for programs they'd like to see.

    If you want practical exercises, then go through this list of sites/resources:

    General exercises:

    • Project Euler contains more than 500 mathematical problems (number theory, number systems, etc.) that need to be solved using programming (in any language).
    • Code Abbey stores over 200 programming problems. 125 of them are awarded a certificate for their solutions, which motivates many students.
    • Rosalind is another site similar to Project Euler, which provides over 200 bioinformatics problems to choose from.
    • Codingbat.com provides Java and Python exercises for both beginners and advanced programmers.
    • codegolf.stackexchange.com is a site where programming puzzles are published and discussed.
    • Ruby Quiz is a series of puzzles that ask you to write short programs of varying complexity. The original solutions are described in Ruby, but they can be implemented in any language.
    • - a selection of logic and programming problems, each with a detailed analysis of the solution.

    Problems from programming olympiads:

    • UVa Online Judge is a collection of hundreds of problems from programming competitions, with an online system for checking solutions.
    • TopCoder contains complex algorithm problems collected over the years from various competitions. It also holds programming competitions several times a month.
    • CodeForces is similar to TopCoder, but contains more competition challenges and a few special features, including "virtual competitions".
    • Timus - same as UVA. Contains problems from the latest competitions (world and regional level).
    • SPOJ is similar to UVA, with a huge number of programming languages ​​to choose from.
    • USACO contains several algorithm problems with guidance on how to solve them.

    For specific languages:

    • For Prolog, Lisp and similar languages ​​visit