Work with Python long enough, and eventually you will encounter *args and **kwargs. These strange terms show up as parameters in function definitions. What do they do? Let's review a simple function: In [1]: def myfunc(a,b): return sum((a,b))*.05 myfunc(40,60) Out[1]: 5.0 This function returns 5% of the sum of a and b. In this example, a … Continue reading *args and **kwargs
Category: Python Tutorial
Nested Statements and Scope
Now that we have gone over writing our own functions, it's important to understand how Python deals with the variable names you assign. When you create a variable name in Python the name is stored in a name-space. Variable names also have a scope, the scope determines the visibility of that variable name to other … Continue reading Nested Statements and Scope
Lambda Expressions, Map, and Filter
Now its time to quickly learn about two built in functions, filter and map. Once we learn about how these operate, we can learn about the lambda expression, which will come in handy when you begin to develop your skills further! map function The map function allows you to "map" a function to an iterable … Continue reading Lambda Expressions, Map, and Filter
Functions
Introduction to Functions This tutorial will consist of explaining what a function is in Python and how to create one. Functions will be one of our main building blocks when we construct larger and larger amounts of code to solve problems. So what is a function? Formally, a function is a useful device that groups … Continue reading Functions
Methods
We've already seen a few example of methods when learning about Object and Data Structure Types in Python. Methods are essentially functions built into objects. Later on in the course we will learn about how to create our own objects and methods using Object Oriented Programming (OOP) and classes. Methods perform specific actions on an … Continue reading Methods
Objects and Data Structures Assessment Test
Test your knowledge. Answer the following questions Write a brief description of all the following Object Types and Data Structures we've learned about: Numbers: Strings: Lists: Tuples: Dictionaries: Numbers Write an equation that uses multiplication, division, an exponent, addition, and subtraction that is equal to 100.25. Hint: This is just to test your memory of … Continue reading Objects and Data Structures Assessment Test
Files (reading and writing) in Python
Python uses file objects to interact with external files on your computer. These file objects can be any sort of file you have on your computer, whether it be an audio file, a text file, emails, Excel documents, etc. Note: You will probably need to install certain libraries or modules to interact with those various … Continue reading Files (reading and writing) in Python
Set and Booleans in Python
There are two other object types in Python that we should quickly cover: Sets and Booleans. Sets Sets are an unordered collection of unique elements. We can construct them by using the set() function. Let's go ahead and make a set to see how it works In [1]: x = set() In [2]: # We add to … Continue reading Set and Booleans in Python
Tuples
In Python tuples are very similar to lists, however, unlike lists they are immutable meaning they can not be changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or dates on a calendar. In this section, we will get a brief overview of the following: 1.) … Continue reading Tuples
Dictionaries in Python
We've been learning about sequences in Python but now we're going to switch gears and learn about mappings in Python. If you're familiar with other languages you can think of these Dictionaries as hash tables. This section will serve as a brief introduction to dictionaries and consist of: 1.) Constructing a Dictionary 2.) Accessing objects … Continue reading Dictionaries in Python
