Python's print function includes abilities that are easily overlooked, even by long-time Pythonistas. Table of contentsUsing multiple arguments Unpacking an iterable into print Using print instead of the string join method Customizing the sep value Printing to a file Customizing the end value Using print is a file-writing utility tool Using multiple arguments Python's print function can accept more than one argument: >>> count=4>>> print("There are",count)There are 4 Python's f-strings pair v...| Python Morsels
You can detect your operating system in Python using either os.name, sys.platform, or platform.system().| www.pythonmorsels.com
Python exercises designed to help you level-up your skills. Practice Python every week!| www.pythonmorsels.com
Python Morsels strict_zip exercise: Like zip, but raises an error for iterables of different lengths| www.pythonmorsels.com
Python Morsels with_previous exercise: Looping helper to get each iterable item along with the previous item| www.pythonmorsels.com
Python Morsels my_builtins exercise: Re-implement some built-in functions to learn how they work| www.pythonmorsels.com
Tuples are technically just immutable lists, but by convention we tend to use tuples and lists for quite different purposes.| www.pythonmorsels.com
Python Morsels bullet points exercise: Utilities for parsing nested bullet points| www.pythonmorsels.com
Python's strings have methods for checking whether a string starts or ends with specific text and for removing prefixes and suffixes.| www.pythonmorsels.com
Python Morsels make_file exercise: Context manager to create a temporary file| www.pythonmorsels.com
Python Morsels cd exercise: Context manager for temporarily changing directories| www.pythonmorsels.com
Python Morsels iospy exercise: Utilities to merge file streams and spy on I/O| www.pythonmorsels.com
Python Morsels Timer exercise: Context manager to time blocks of code| www.pythonmorsels.com
To use a function in Python, write the function name followed by parentheses. If the function accepts arguments, pass the arguments inside the parentheses.| www.pythonmorsels.com
Unlike many programming languages, Python doesn't have "type coercion". Python typically doesn't implicitly convert one object to another type of object. Type conversions usually need to be explicit.| www.pythonmorsels.com
When you're working with named arguments (a.k.a. keyword arguments) it's the argument name that matters. When you're working with positional arguments, it's the position matters (but not the name).| www.pythonmorsels.com
If you're trying to make a friendly command-line interface in Python, instead of manually parsing sys.argv you should probably use Python's argparse module.| www.pythonmorsels.com
When working with text files in Python, it's considered a best practice to specify the character encoding that you're working with.| www.pythonmorsels.com
Want to turn a list of strings into a single string? You can use the string join method to join a list of strings together (concatenating them with a delimiter of your choice).| www.pythonmorsels.com
The string title method falls down when title-casing contractions. What's a good alternative to the title method? Or is there one?| www.pythonmorsels.com
Strings can be split by a substring separator. Usually the string split is called without any arguments, which splits on any whitespace.| www.pythonmorsels.com
Python's string formatting syntax is both powerful and complex. Let's break it down and then look at some cheat sheets.| www.pythonmorsels.com
Wondering how to concatenate a string in Python? Hold on! There are two ways to build up bigger strings out of smaller strings: string concatenation (with +) and string interpolation (with f-strings).| www.pythonmorsels.com
In Python, strings are used to represent text and bytes are used to represent binary data. If you end up with bytes that representing text, you can decode them to get a string instead.| www.pythonmorsels.com
Python Morsels get_vowel_names exercise: Find all the names starting with a vowel| www.pythonmorsels.com
An exercise-first introduction to Python in bite-sized chunks. Each section consists of many 4 minute videos, each followed by 30 minute exercises. By the end of this course you'll have a solid foundation of Python knowledge.| www.pythonmorsels.com
Python programmers typically check for empty lists by relying on truthiness.| www.pythonmorsels.com
Dunder methods power most operators in Python as well as some of the built-in functions. Dunder methods are a contract between the person who made a class and Python itself.| www.pythonmorsels.com
In Python we care about the behavior of an object more than the type of an object. We say, "if it looks like a duck and walks like a duck, it's a duck." This idea is called duck typing.| www.pythonmorsels.com
Functions in Python can be defined within another function.| www.pythonmorsels.com
It's best to avoid calling dunder methods. It's common to define dunder methods, but uncommon to call them directly.| www.pythonmorsels.com
Sets and dictionaries are powered by hashability. And hashable objects tend to be immutable.| www.pythonmorsels.com
Python's collections.Counter class is extremely handy, especially when paired with generator expressions.| www.pythonmorsels.com
Strings are used to store text-based data.| www.pythonmorsels.com
Sign up for Python Morsels to improve your Python skills in just 60 minutes every week.| www.pythonmorsels.com
Sign in to your Python Morsels account.| www.pythonmorsels.com
Sets are unordered collections of values that are great for removing duplicates, quick containment checks, and set operations. Table of contentsWhat are sets? Sets are like dictionaries without values Quick containment checks with sets Using set arithmetic Converting to a set for efficiency Checking for duplicates What are sets good for in Python? What are sets? Like lists, sets are collections of values: >>> fruits={"apples","strawberries","pears","apples"} But unlike lists, sets can't conta...| Python Morsels
In Python, default argument values are defined only one time (when a function is defined). Table of contentsFunctions can have default values A shared default value Default values are only evaluated once Mutable default arguments can be trouble Shared argument values are the real problem Avoiding shared argument issues by copying Avoiding mutable default values entirely Be careful with Python's default argument values Functions can have default values Function arguments in Python can have def...| Python Morsels
You can check whether iterables contain the same elements in Python with equality checks, type conversions, sets, Counter, or looping helpers. Table of contentsSimple equality checks Comparing different types of iterables Checking equality between large iterables Checking for near-equality Ignoring order when comparing iterables Comparing iterables isn't just about equality Simple equality checks If we have two lists and we wanted to know whether the items in these two lists are the same, we ...| Python Morsels
Python allows us to represent newlines in strings using the \n "escape sequence" and Python uses line ending normalization when reading and writing with files.| www.pythonmorsels.com
Adopt a more Pythonic coding style in 60 minutes of practice each week. Python Morsels includes exercises and screencasts by a professional Python trainer.| www.pythonmorsels.com
An explanation of all of Python's 100+ dunder methods and 50+ dunder attributes, including a summary of each one.| www.pythonmorsels.com
Instead of using hard-coded indices to get tuple elements, use tuple unpacking to give descriptive names to each item. Important items should have a name instead of a number.| www.pythonmorsels.com
The range function can be used for counting upward, countdown downward, or performing an operation a number of times.| www.pythonmorsels.com
Python Morsels grep exercise: A utility like the Linux/Unix grep command| www.pythonmorsels.com
Need to loop over two (or more) iterables at the same time? Don't use range. Don't use enumerate. Use the built-in zip function. As you loop over zip you'll get the n-th item from each iterable.| www.pythonmorsels.com
Sequences are iterables that have a length. Sequences are ordered collections (they maintain the order of their contents). The most common sequences built-in to Python are string, tuple, and list.| www.pythonmorsels.com
Python Morsels observe exercise: Refactor to reduce repetition| www.pythonmorsels.com
Context managers power Python's with blocks. They sandwich a code block between enter code and exit code. They're most often used for reusing common cleanup/teardown functionality.| www.pythonmorsels.com
Python Morsels Point exercise: Class representing a 3-dimensional point| www.pythonmorsels.com
Python Morsels head exercise: Get the first N items from any iterable| www.pythonmorsels.com
Python Morsels transpose exercise: Transpose a given list-of-lists| www.pythonmorsels.com
Need to represent multiple lines of text in Python? Use Python's multi-line string syntax!| www.pythonmorsels.com
Have a long line of code? If you don't have brackets or braces on your line yet, you can add parentheses wherever you'd like and put line breaks within them. We call this "implicit line continuation".| www.pythonmorsels.com
Is your Python program printing out a traceback? You have an uncaught exception! You can catch that exception with a try-except block.| www.pythonmorsels.com
Python crashed with the error TypeError: can only concatenate str (not "int") to str. Essentially Python's saying you've used + between a string and a number and it's unhappy about it. Let's talk about how to fix this issue and how to avoid it more generally.| www.pythonmorsels.com
If you need to make a very simple command-line interface and it doesn't need to be friendly, you can read sys.argv to manually process the arguments coming into your program.| www.pythonmorsels.com
Python's strings have dozens of methods, but some are much more useful than others. Let's discuss the dozen-ish must-know string methods and why the other methods aren't so essential.| www.pythonmorsels.com
Lists are used to store and manipulate an ordered collection of things.| www.pythonmorsels.com
In Python, slicing looks like indexing with colons (:). You can slice a list (or any sequence) to get the first few items, the last few items, or all items in reverse.| www.pythonmorsels.com
In Python, truthiness is asking the question, what happens if I convert an object to a boolean. Every Python object is truthy by default. Truthiness in Python is about non-emptiness and non-zeroness.| www.pythonmorsels.com
Want to customize what "equality" means for your class instances in Python? Implement a __eq__ method! Make sure to return NotImplemented as appropriate though.| www.pythonmorsels.com
Python's built-in enumerate function is the preferred way to loop while counting upward at the same time. You'll almost always see tuple unpacking used whenever enumerate is used.| www.pythonmorsels.com
Any reversible iterable can be reversed using the built-in reversed function whereas Python's slicing syntax only works on sequences.| www.pythonmorsels.com
Python's built-in functions list includes 71 functions now! Which built-in functions are worth knowing about? And which functions should you learn later?| www.pythonmorsels.com
Unlike traditional C-style for loops, Python's for loops don't have indexes. It's considered a best practice to avoid reaching for indexes unless you really need them.| www.pythonmorsels.com
Iterators are lazy iterables which power all iteration in Python. Iterators are the generic form of a generator-like object.| www.pythonmorsels.com
Python's dictionaries act as lookup tables which map keys to their values.| www.pythonmorsels.com
To work with a text file in Python, you can use the built-in open function, which gives you back a file object. Reading from and writing to text files is an important skill.| www.pythonmorsels.com
Classes are for coupling state (attributes) and functionality (methods). Calling a class returns an instance of that class. Class and "type" are synonyms in Python.| www.pythonmorsels.com
Within a function in Python, you can read from global variables and read from or write to local variables. Within each function, each variable name is either local or global (but not both).| www.pythonmorsels.com
When you import a module in Python, you'll get access to a module object with attributes representing each of the variables in that module. Python comes bundled with a bunch of modules.| www.pythonmorsels.com
Python's list comprehensions are special-purpose tools for taking an old iterable, looping over it, and making a new list out of it.| www.pythonmorsels.com
Python's "walrus operator" is used for assignment expressions. Assignment expressions are a way of embedding an assignment statement inside another line of code.| www.pythonmorsels.com
The Python REPL is another name for Python's interactive Python interpreter, which accepts Python statements and immediately evaluates them.| www.pythonmorsels.com
When exceptions go unhandled, Python prints a traceback. Tracebacks are read from the bottom upward. The last line describes what happened and lines above describe where it happened.| www.pythonmorsels.com
Python's variables are not buckets that contain objects; they're pointers. Assignment statements don't copy: they point a variable to a value (and multiple variables can "point" to the same value).| www.pythonmorsels.com
Every command-line tool included with Python. These can be run with python -m module_name.| www.pythonmorsels.com
The time complexity of common operations on Python's many data structures.| www.pythonmorsels.com
Python's None value is used to represent nothingness. None is the default function return value.| www.pythonmorsels.com
Equality checks whether two objects represent the same value. Identity checks whether two variables point to the same object.| www.pythonmorsels.com
List comprehensions make lists; generator expressions make generators. Generators are lazy single-use iterables which generate values as you loop over them.| www.pythonmorsels.com
An iterable is anything you're able to iterate over (iter-able). Iterables can be looped over and anything you can loop over is an iterable. Not every iterable is indexable or has a length.| www.pythonmorsels.com
Python's built-in next function will consume the next item from a given iterator.| www.pythonmorsels.com
Python Morsels Privacy Policy| www.pythonmorsels.com
You can set the value of a variable in Python by using the equals sign (=) to make an assignment statement.| www.pythonmorsels.com
When should you use the built-in list function in Python? And when shouldn't you?| www.pythonmorsels.com
You don't need third-party libraries to read CSV file in Python! Python's csv module includes helper functions for reading CSV files, tab-delimited files, and other delimited data files.| www.pythonmorsels.com
While it is possible to make singleton objects in Python, the classic singleton design pattern doesn't always make a lot of sense.| www.pythonmorsels.com
You can use Python's if, elif, and else blocks to run code only when specific conditions are met.| www.pythonmorsels.com
Python's "invalid syntax" error message comes up often, especially when you're first learning Python. What usually causes this error and how can you fix it?| www.pythonmorsels.com
When your code depends other Python libraries to run, you'll want to reach for pip and venv to install those third-party Python packages.| www.pythonmorsels.com
Definitions for colloquial Python terminology (effectively an unofficial version of the Python glossary).| www.pythonmorsels.com
Unlike many programming languages, variables in Python are not buckets which "contain" objects. In Python, variables are pointers that "point" to objects.| www.pythonmorsels.com