Python 101: Learn the 5 Must-Know Concepts

Tech With Tim
20 May 202320:00

Summary

TLDRThis video script is an educational guide for Python developers, focusing on five critical concepts: mutable vs. immutable types, list comprehensions, argument types in functions, the '__name__ == "__main__"' idiom, and the Global Interpreter Lock (GIL). It aims to enhance understanding of production code, enabling developers to write efficient and effective Python code. The script includes practical examples and a sponsored segment on Nord Pass, a password management solution.

Takeaways

  • 💻 **Understanding Python Concepts**: The video emphasizes the importance of grasping five crucial Python concepts to avoid common mistakes and effectively read and write production code.
  • 🔐 **Nord Pass Sponsorship**: Nord Pass is introduced as a secure password and credential management solution, useful for storing and sharing sensitive information like passwords and bank details.
  • 🔄 **Mutable vs Immutable Types**: A key concept explained is the difference between mutable and immutable types in Python, where immutable types cannot be changed after creation, unlike mutable types.
  • 📚 **List Comprehension**: The video covers list comprehensions, a Python feature used to create lists in a concise way, and how they can simplify or complicate code depending on usage.
  • 📝 **Parameter Types in Functions**: It explains various types of parameters in Python functions, including positional, optional, and the use of *args and **kwargs for flexible argument passing.
  • 🔑 **Global Interpreter Lock (GIL)**: The script discusses the GIL in Python, which allows only one thread to execute at a time, despite the presence of multiple CPU cores, limiting the performance benefits of multi-threading.
  • 🛠️ **Function Side Effects**: The concept of side effects in functions is introduced, where functions modify the state of objects passed as arguments, which is crucial for understanding when working with mutable types.
  • 🔍 **Conditional List Comprehensions**: The video demonstrates how to use conditional statements within list comprehensions to create lists based on specific criteria, like filtering even numbers.
  • 📦 **Module Execution Control**: The use of `if __name__ == '__main__':` is explained to control code execution when a module is run directly versus when it's imported, preventing unintended code execution.
  • 🔀 **Threading and Performance**: The script touches on the limitations of threading in Python due to the GIL, explaining that multi-threading does not speed up execution in Python as it might in other languages.

Q & A

  • What is the main goal of the video for Python developers?

    -The main goal of the video is to ensure that Python developers understand key concepts when reading through production Python code, so they can reproduce and write their own code that other developers will understand and expect.

  • Why is it important to understand the difference between mutable and immutable types in Python?

    -Understanding the difference between mutable and immutable types is crucial because it affects how variables and objects behave when they are assigned and modified. Immutable types cannot be changed after creation, whereas mutable types can be modified, which can lead to unexpected behavior if not properly managed.

  • What is a side effect in the context of functions in Python?

    -A side effect in Python functions refers to a function modifying a parameter that affects the outside state, such as changing a mutable object passed as an argument. This can lead to changes in the original object, which might not be intended and can cause bugs or unexpected behavior.

  • How do list comprehensions in Python simplify code?

    -List comprehensions in Python simplify code by allowing the creation of lists in a concise way, using a single line of code that includes an expression followed by a for loop. This can make the code more readable and efficient compared to traditional for loop methods.

  • What are the different types of parameters that can be used in Python functions?

    -Python functions can use various parameter types, including positional parameters, keyword parameters, optional parameters with default values, *args for accepting a variable number of positional arguments, and **kwargs for accepting a variable number of keyword arguments.

  • Why is the '__name__' attribute important in Python scripts?

    -The '__name__' attribute is important in Python scripts because it determines if the script is being run directly or being imported as a module. It is commonly used to prevent certain code from executing when the module is imported, but only when the script is run directly.

  • What is the Global Interpreter Lock (GIL) in Python, and how does it affect multi-threading?

    -The Global Interpreter Lock (GIL) is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. This means that even with multiple threads, only one thread can execute at a time, which can limit the performance benefits of multi-threading in CPU-bound Python programs.

  • How can the 'if __name__ == '__main__' idiom be used in Python scripts?

    -The 'if __name__ == '__main__' idiom is used to allow a Python script to determine if it is being run directly or being imported as a module. Code inside this block will only execute when the script is run directly, not when it is imported.

  • What is the significance of the 'Nord Pass' sponsorship mentioned in the video?

    -The 'Nord Pass' sponsorship mentioned in the video signifies that the company provided financial support for the creation of the video content. The video includes a promotion for Nord Pass, highlighting its features and offering a discount code for viewers.

  • How can understanding Python concepts like mutable and immutable types help in writing production code?

    -Understanding Python concepts like mutable and immutable types helps in writing production code by preventing bugs and unexpected behavior that can arise from improper handling of object mutability. It ensures that code is more predictable, maintainable, and efficient.

Outlines

00:00

🐍 Introduction to Essential Python Concepts

The paragraph introduces the video's focus on five crucial Python concepts that are often misunderstood by programmers. It emphasizes the importance of understanding these concepts to read, reproduce, and write Python code effectively. The video is sponsored by Nord Pass, a password management solution. The speaker discusses the challenges of securely sharing sensitive information like passwords and how Nord Pass provides a solution. It offers features like autofill, data breach detection, and a password generator. Viewers are encouraged to use a promo code for a free trial. The first concept introduced is the difference between mutable and immutable types in Python, which is fundamental for beginners to grasp.

05:01

🔑 Mutable vs Immutable Types in Python

This section delves into the concept of mutable and immutable types in Python, using examples to illustrate the difference. Immutable types, such as strings, integers, floats, booleans, bytes, and tuples, cannot be changed after they are created. In contrast, mutable types like lists, sets, and dictionaries can be modified. The speaker provides a practical example where a tuple's inability to be changed (immutability) is demonstrated, followed by an example where a list's contents can be altered (mutability). The explanation clarifies how assignments work with these types, showing that changes to mutable objects reflect across all references to them due to the shared reference.

10:02

📝 Understanding List Comprehensions and Function Parameters

The speaker explains list comprehensions, a Python feature used to create lists in a concise way. Basic and nested list comprehensions are demonstrated, along with examples of including conditional statements within them. The paragraph then transitions into discussing various types of function parameters in Python, including positional, optional, and variable-length arguments. The use of asterisks (*) for arbitrary argument lists and double asterisks (**) for arbitrary keyword arguments is introduced, showing how to pass a list or dictionary as arguments to a function.

15:03

🔐 The '__name__' Special Variable and the Global Interpreter Lock

This part discusses the special variable `__name__` in Python, which is used to determine if a module is run as the main program or imported. The use of `if __name__ == '__main__':` is highlighted to prevent code from running when the module is imported. The final concept covered is the Global Interpreter Lock (GIL), a Python mechanism that allows only one thread to execute at a time, even in a multi-threaded environment. This limits the performance benefits of multi-threading in CPU-bound tasks. The speaker suggests further exploration of the GIL for a deeper understanding.

Mindmap

Keywords

💡Mutable vs Immutable Types

Mutable types in Python are objects that can be modified after their creation, such as lists, sets, and dictionaries. Immutable types, on the other hand, cannot be changed once created and include strings, integers, tuples, and frozensets. This concept is crucial for understanding how data is handled in Python, as it affects how variables and functions operate. In the script, the distinction is highlighted by showing that changes to mutable objects affect all references to that object, whereas changes to immutable objects do not.

💡List Comprehension

List comprehension in Python is a concise way to create lists based on existing lists or iterables. It consists of a single-line expression that can include for loops and if statements, allowing for more readable and efficient code. This concept is important for writing clean and efficient Python code, as it can replace multiple lines of for loop code with a single line. The script uses list comprehension to demonstrate how to create lists with specific conditions, such as even numbers.

💡Argument and Parameter Types

In Python, arguments are the values passed to a function when calling it, while parameters are the variables in the function definition that receive those values. The script explains different types of parameters, such as positional, keyword, optional, and variable-length arguments. Understanding these concepts is essential for mastering function usage and creation in Python, as they affect how data is passed and manipulated within functions.

💡Global Interpreter Lock (GIL)

The Global Interpreter Lock is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. This concept is central to understanding Python's threading model and performance implications. The script explains that, despite having multiple threads, the GIL means only one thread can execute at a time, which can be a limiting factor for CPU-bound applications.

💡Side Effects

In programming, side effects refer to changes in state that persist beyond the completion of a function call. Functions that have side effects modify external variables or have other impacts outside their scope. The script warns about side effects when dealing with mutable objects in functions, as these can lead to unexpected behavior if not carefully managed.

💡Nord Pass

Nord Pass is mentioned as a password and credential management solution in the script. It's highlighted as a tool for securely storing and sharing sensitive information like passwords and bank details. This sponsorship mention is a practical example of how software developers might manage credentials and is used to illustrate the security challenges faced when collaborating with others on projects.

💡Pull Requests

A pull request is a feature in version control systems like Git, allowing developers to submit changes to a project's repository. The script mentions writing code that can be included in pull requests, emphasizing the importance of writing clean, understandable code that other developers can review and accept into the main codebase.

💡Production Code

Production code refers to the final, polished code that is deployed in a live environment and used by end-users. The script's goal is to help viewers understand production Python code, indicating the necessity of writing code that is not only functional but also maintainable and understandable by other developers.

💡Autofill

Autofill is a feature that automatically completes form fields with previously saved information. In the context of the script, Nord Pass is said to have an autofill feature, which is beneficial for developers and users to efficiently manage and use their stored credentials without manually entering them each time.

💡Data Breach Detection

Data breach detection is a security feature that alerts users to potential compromises of their stored data, such as passwords. The script mentions this feature of Nord Pass, emphasizing the importance of security in development tools and the need for developers to be aware of and incorporate such features into their applications.

Highlights

Understanding five important Python concepts is crucial for developers.

Beginner and intermediate programmers often misunderstand production code.

The video aims to clarify concepts for reading and writing Python code effectively.

Nord Pass is introduced as a secure password and credential management solution.

Nord Pass helps in storing personal and business data securely and sharing access with team members.

Use the code 'techwithTim' for a three-month free trial of Nord Pass.

Mutable vs. immutable types are a fundamental concept where beginners often make mistakes.

Immutable types in Python include strings, ints, floats, booleans, bytes, and tuples.

Mutable types such as lists, sets, and dictionaries can be changed after creation.

Demonstration of tuple immutability and list mutability with code examples.

Functions can have side effects on mutable objects passed as arguments.

List comprehensions are a Python feature for creating lists concisely.

Examples of simple and nested list comprehensions are provided.

List comprehensions can include conditional statements to filter values.

Different argument types in Python functions include necessary, optional, and variable arguments.

Necessary parameters must be provided in the correct order when calling a function.

Optional parameters have default values and are not required when calling a function.

Asterisk (*) and double asterisk (**) syntax allows functions to accept variable numbers of arguments.

"__name__ == '__main__'" idiom is used to determine if a script is run directly or imported.

The Global Interpreter Lock (GIL) is a Python mechanism that limits execution to one thread at a time.

The GIL prevents performance benefits from multi-threading in CPU-bound Python programs.

Transcripts

play00:02

If you're interested in

play00:03

becoming a developer that writes

play00:04

any type of code in Python,

play00:06

then you need to understand these five

play00:08

very important Python concepts.

play00:10

These are what I see most beginner

play00:12

and intermediate python programmers

play00:13

making a ton of mistakes

play00:15

with and misunderstanding

play00:16

when they're reading through

play00:17

production code.

play00:18

The goal of this video is to make sure

play00:20

that when you're reading

play00:21

through production Python code,

play00:22

you understand what's happening.

play00:23

You know the concept

play00:25

and then you can reproduce that code

play00:26

and write your own pull requests

play00:28

and own features.

play00:29

Using Python code

play00:30

that other developers

play00:31

will understand and expect.

play00:33

So with that

play00:34

said, let's get into the video.

play00:35

After I share with you

play00:36

the first

play00:36

very important concept

play00:38

you need to understand,

play00:39

which is the sponsor of this video.

play00:41

Nord Pass.

play00:42

Nord Pass is the ultimate password

play00:44

and credential management solution

play00:46

that I've actually been looking for

play00:47

for a long time now.

play00:49

I don't know about you guys,

play00:50

but I have a lot of passwords,

play00:51

credit cards, bank

play00:52

details, private keys, etc.

play00:55

And oftentimes

play00:55

I need to share these with my coworkers,

play00:57

making it a constant struggle

play00:59

to not only keep

play01:00

this data secure and safe,

play01:01

but to allow them to access it quickly

play01:03

without having to message me

play01:04

all the time.

play01:05

Now, Nord Pass fixes

play01:06

this problem

play01:06

because it allows me to store

play01:08

both my personal

play01:08

or my business data

play01:10

in a single secure location

play01:12

and then give different access

play01:13

to members of my team.

play01:14

Now, if you don't believe me,

play01:15

you can check it out

play01:16

from the link in the description

play01:17

and use the code tech with Tim,

play01:19

which means you'll no longer

play01:20

have to be sending messages

play01:21

or receiving messages

play01:23

asking for passwords.

play01:24

Not to mention that Nord

play01:25

Pass has features like autofill,

play01:27

data breach detection and activity.

play01:29

Log a password generator and much more.

play01:32

Check out Nord

play01:33

Pass from the link in the description

play01:35

and use the Code tech with Tim

play01:36

for a three month free trial

play01:38

so you can take control over your data

play01:39

and your accounts.

play01:41

Thanks again to Nord Pass

play01:42

for sponsoring this video.

play01:44

So the first concept to go over here

play01:45

is mutable versus immutable types.

play01:48

Now, this is the concept

play01:49

that most beginner

play01:49

and intermediate programmers

play01:51

make mistakes with.

play01:52

Don't worry if you already understand it,

play01:53

there's a lot more complicated concepts,

play01:55

so stick around

play01:56

for the rest of the video.

play01:57

Regardless and immutable type

play01:59

is something that cannot change.

play02:01

A mutable type is something

play02:02

that can change.

play02:03

An example of

play02:04

these in Python is the following

play02:06

So immutable types

play02:07

are going to be our string,

play02:08

our ints, our floats, our boolean,

play02:12

our bytes type, and our topple type.

play02:16

All of these are immutable,

play02:17

meaning once you define this,

play02:19

you cannot change it.

play02:20

However, we have mutable types in Python,

play02:23

which are the list,

play02:24

the set and the dictionary,

play02:26

and pretty much any other type

play02:28

use from some third party

play02:29

library or module.

play02:31

These can change,

play02:32

which means once you define them,

play02:34

you can actually modify them.

play02:36

Let me give you a super quick example

play02:37

here of immutable versus mutable.

play02:39

Then we'll go into a more complex

play02:40

one using a function,

play02:42

which is where I see

play02:42

most beginners make a mistake.

play02:44

Okay,

play02:45

so let's say we have some number

play02:46

like x equals one

play02:47

and we say y is equal to x.

play02:49

And in fact,

play02:49

let's change this to a tuple,

play02:51

which remember is e

play02:52

mutable, meaning we cannot change it.

play02:55

Actually, to quickly show this to you.

play02:57

Let's try to do something

play02:57

like zero is equal to one where

play02:59

we're trying to change this tuple

play03:01

without reassigning

play03:03

something to this variable.

play03:05

So if I go here and run my code, notice

play03:07

I get an error

play03:07

and it says the tuple object

play03:09

does not support item assignment.

play03:10

Now, the reason it doesn't support

play03:12

that is because this is immutable.

play03:14

That means that once I define this tuple,

play03:16

I cannot change it.

play03:18

Now, if we go here

play03:19

and do something like Y equals X,

play03:20

and let's come and say x

play03:23

is equal to 1 to 3,

play03:26

I just want to show you

play03:27

if I print out both X and Y here

play03:29

that my change to x here

play03:31

after assigning X to Y did not affect Y.

play03:34

The reason for that

play03:35

is whenever you're using immutable types,

play03:37

when you do an assignment

play03:38

to another variable,

play03:39

so I do something like y equals x,

play03:41

it makes a copy.

play03:42

So an actual real copy

play03:44

of this immutable object,

play03:46

meaning that if I now go

play03:48

something like X is equal

play03:49

to one, two, three,

play03:50

that's not going to affect Y

play03:52

because I'm not modifying

play03:53

Y is storing,

play03:54

I'm just reassigning a new value to X.

play03:57

I know that seems trivial.

play03:58

The reason I'm illustrating this to you

play04:00

is because this works differently.

play04:01

When we change this to a list.

play04:04

So if I change this to a list now

play04:05

and then I come and do something like

play04:07

x zero is equal to 100,

play04:12

you might think that Y

play04:13

is not going to change.

play04:14

But when I run this, you see that

play04:16

both X and Y have the same value.

play04:18

Now the reason for

play04:19

that is when you're using mutable types

play04:21

and you do something like Y

play04:22

equals x, here

play04:23

you're signing a variable

play04:25

to another variable

play04:26

and this variable storing a mutable type.

play04:28

What happens is

play04:29

you actually store a reference

play04:31

or an alias

play04:32

to this same object,

play04:34

meaning that

play04:34

if I make a change to the object

play04:36

like I'm doing right here,

play04:37

it changes for both of these variables

play04:39

because they're actually

play04:40

storing the same object.

play04:42

In fact,

play04:42

they're storing a reference

play04:43

to the same object.

play04:44

So again,

play04:45

if you change the underlying object

play04:46

that it changes for both X

play04:48

and Y, that's the difference

play04:49

between immutable and mutable types.

play04:52

Now let me just paste

play04:53

in a quick example here

play04:54

that will illustrate this

play04:55

even a little bit further.

play04:56

You can see in this example

play04:57

we have a function that returns

play04:58

the largest numbers.

play04:59

It returns the end

play05:00

largest numbers, actually.

play05:01

What it does

play05:02

is it sorts

play05:03

the list of numbers that it accepts.

play05:05

So what I've done down here

play05:06

is I've created a list of numbers.

play05:08

I printed out

play05:08

what the value of the list was

play05:10

before I called the function.

play05:11

And then I printed out

play05:12

what the value was afterwards.

play05:14

Now take a guess

play05:14

if you want

play05:15

what you think the output is going to be,

play05:16

but I'll go ahead and run the code.

play05:18

And you can see here

play05:19

that we actually get the list

play05:20

before that's unsorted

play05:21

and then the list becomes

play05:23

sorted afterwards.

play05:24

Now, the reason

play05:25

this occurs is because what happens

play05:27

is when we call this function,

play05:28

we pass this nums

play05:29

list as the parameter numbers.

play05:32

Now, since we're passing

play05:33

a mutable object, a list is mutable

play05:35

when we do an numbers dot sort.

play05:37

What this does is actually sort

play05:39

the list in place.

play05:41

Now, numbers

play05:41

here is going to be storing

play05:43

a reference to this same list.

play05:46

So when I sort the numbers

play05:47

parameter here,

play05:48

since I had passed in my numbers array,

play05:51

it ends up sorting that numbers array

play05:53

that's down here.

play05:54

Seems a little bit strange,

play05:55

but the reason

play05:56

this is occurring is again,

play05:57

because we're using a mutable object.

play05:59

So the point here is

play06:00

that you need to understand

play06:01

when you're using mutable

play06:02

versus immutable objects,

play06:03

because you can have functions like this

play06:05

that can perform side effects

play06:07

on your mutable objects.

play06:08

This is referred to as a side effect

play06:10

because what happens is

play06:11

one of the parameters

play06:12

is being mutated or modified

play06:14

inside of the function.

play06:15

Sometimes you want that to be the case.

play06:17

Sometimes you don't

play06:18

want that to be the case.

play06:19

You need to be intentional

play06:20

when you're writing your code.

play06:21

So the next concept to understand

play06:22

here is list comprehension.

play06:24

Now, the reason you need to understand

play06:26

this is because it's used

play06:27

quite a bit in Python,

play06:28

and oftentimes

play06:29

you'll see people writing

play06:30

fairly complicated comprehension

play06:32

to simplify a line of code.

play06:34

Now this can kind of do the reverse.

play06:36

Sometimes it can actually make it

play06:37

more complicated.

play06:38

Regardless,

play06:39

you need to understand what they are

play06:41

so that you can actually understand them

play06:42

if you see them in some production code.

play06:45

So let's have a look at

play06:45

a list comprehension.

play06:47

So the most basic comprehension

play06:48

you can do here

play06:49

is something like X,

play06:50

or we'll go with AI for AI in range

play06:54

and then maybe something like ten.

play06:56

And in case you can't

play06:57

guess it here,

play06:58

what this is going to do

play06:58

is give me an array

play06:59

that contains the numbers

play07:00

zero through nine.

play07:01

So me open up my terminal and run this.

play07:04

And there you go.

play07:04

We get zero through nine.

play07:05

So this is a list comprehension

play07:07

where essentially you write

play07:08

a for loop inside of a list.

play07:10

What you do on the left hand

play07:11

side is

play07:12

you put the value

play07:13

that you want to populate the list with

play07:15

and then you have some kind of iterator.

play07:16

In this case,

play07:17

we have a for loop

play07:18

that's going to loop through

play07:19

and generate these different values.

play07:21

Now, this is a very simple list

play07:22

comprehension.

play07:23

You can make much more complicated ones.

play07:25

For example,

play07:26

we can have a list here instead.

play07:28

So now if I do a list,

play07:29

we have a bunch of empty lists

play07:30

inside of this list.

play07:32

But just like we have a list

play07:33

comprehension here,

play07:34

we can have one inside of this list.

play07:35

So I can do something like for.

play07:38

So actually, let's go with J for J

play07:41

in range five like that.

play07:44

And now we have a nested

play07:45

list comprehension.

play07:46

And if I run this code, you can see that

play07:48

now we get a bunch of lists

play07:49

that contain five different values

play07:51

inside of them ten times.

play07:54

Okay, so that's one thing you can do.

play07:55

Another thing that we can do here

play07:57

is the following.

play07:58

So let's go

play07:59

here and say I for I in range ten,

play08:02

and then we can put in if statement

play08:04

and we can say

play08:04

if I mod two is equal to zero.

play08:08

Now this means we're going

play08:09

to only put this value here

play08:10

if this condition evaluates to true.

play08:13

So in this case

play08:13

we're only going to put

play08:14

even values or zero inside of this list.

play08:17

So when I run this,

play08:18

you see that

play08:18

we get all of the even values up to,

play08:20

but not including ten.

play08:22

All right.

play08:22

So the next concept here

play08:23

is the different Python

play08:25

argument in parameter types.

play08:27

Now, there's quite a few.

play08:28

That's why

play08:28

I'm going through this concept.

play08:29

And a lot of times

play08:30

people have no idea what they are

play08:32

beyond the basic ones.

play08:33

So if we define a function

play08:35

here, like complicated function,

play08:36

we can have

play08:37

what's known

play08:37

as are necessary parameters

play08:39

or are positional parameters

play08:41

that are defined in order.

play08:43

So I can have something like x, y.

play08:44

Now these are required

play08:45

and they're are positional,

play08:47

meaning that if I want to pass values

play08:48

here, I have to do something

play08:50

like one two, right?

play08:51

I pass them in the order in which

play08:53

I want them to be kind of a sign.

play08:55

So X is one and Y is two.

play08:58

However, I can actually switch

play09:00

things up a little bit here

play09:01

and as I pass these arguments

play09:03

I can do something like y is equal to two

play09:06

and x is equal to one.

play09:08

And now if I go here and print this,

play09:10

so X and Y,

play09:13

you see that we get one and two.

play09:16

So this is valid.

play09:17

When you are calling a function,

play09:18

you can actually write out

play09:19

the name of the parameter,

play09:20

whether or not

play09:21

it's positional optional, etc.

play09:23

and then you can just assign it

play09:24

directly inside of here.

play09:25

This allows you to no longer

play09:27

pass this positionally.

play09:29

However,

play09:30

if I passed some positional arguments,

play09:32

so let's say I do something like one

play09:34

now I can do something like Z to Y

play09:37

is equal to three,

play09:39

so I can pass

play09:39

some of the arguments positionally

play09:41

and then some of them I can pass

play09:43

using the kind of keyword argument here

play09:45

or, you know, the named argument,

play09:47

whatever you want to refer to it as.

play09:49

By the way,

play09:49

inside of your function call,

play09:51

you refer to these as arguments.

play09:52

And up here in your function,

play09:53

you refer to these as parameters.

play09:55

So I just wanted to show you that

play09:56

I can pass some of these positionally.

play09:58

However, things get a little bit weird

play10:00

if I try to pass some positionally

play10:02

and some using the keyword.

play10:04

So in this case

play10:04

I have like Z equals to one

play10:06

and then Y equals three.

play10:07

If I try to run this notice,

play10:09

I get an error

play10:09

and it's a positional argument

play10:11

follows a keyword argument

play10:12

which you're not allowed to do.

play10:14

So if I want to use

play10:15

some positional arguments

play10:16

and the rest keyword arguments,

play10:18

that means that I need to start

play10:19

by defining my positional arguments.

play10:21

Then I can do the keyword arguments after

play10:23

hopefully that's clear,

play10:24

but that was the first thing to go over.

play10:26

Okay, next thing is optional parameters.

play10:29

So inside of your function

play10:30

you can mark one of your parameters

play10:31

as optional by putting an equal sign.

play10:34

If I do Z equals two, in this case, none.

play10:36

Now this is optional,

play10:38

meaning I'm not required to pass it

play10:39

when I call this function.

play10:41

So if I call with one and three here,

play10:44

you can see this is perfectly fine.

play10:45

However, if I got rid of the equals

play10:47

sign here

play10:47

so I made this no longer optional,

play10:50

then I get an erroneous.

play10:51

This is missing

play10:51

one required positional argument

play10:54

that's worth noting.

play10:55

Now, if I try to access Z here,

play10:57

you'll see that actually

play10:58

it's making an eagle something like ten.

play11:00

If I run this,

play11:01

it actually gets its default value.

play11:03

So when you make something

play11:04

optional, really what you're doing

play11:06

is providing a default value for it,

play11:08

which means if you don't pass that value

play11:09

and you call the function by default,

play11:11

it will be equal to that value.

play11:13

Okay.

play11:14

So that was actually the easy stuff.

play11:15

Now we move on

play11:16

to the more complicated ones.

play11:18

Now we have something

play11:19

referred to as Asterix arcs.

play11:21

Now what this allows us to do

play11:23

is actually accept

play11:23

any number of positional arguments

play11:26

so I can pass

play11:29

a bunch arguments like this.

play11:31

Okay.

play11:32

I can pass no additional arguments.

play11:34

I can pass one to whatever its any number

play11:36

after my positional arguments.

play11:38

So I print out X-Y-Z and then args here

play11:42

and I run the code.

play11:43

You see that this is perfectly valid.

play11:44

So when I do this asterix args again,

play11:47

this means okay, I'm

play11:47

going to accept any number

play11:49

of positional arguments at this point.

play11:51

So after my two positional arguments

play11:53

that I have here

play11:54

and then it's going to store

play11:55

all of them in a tuple, which is an e

play11:57

mutable type.

play11:59

Okay.

play11:59

If we just have star args here,

play12:01

then you see it works the exact same way.

play12:03

We accept any number

play12:04

of positional arguments, even zero.

play12:07

Right.

play12:07

So if I have none here,

play12:08

this works perfectly fine.

play12:10

Okay. That is star books.

play12:12

Now we also have star star quarks.

play12:15

So when you have star

play12:16

star quarks,

play12:17

this means we're going to accept

play12:18

any number of keyword arguments.

play12:20

So let me just print out quarks here

play12:23

and go and pass some keyword arguments.

play12:25

So the keyword arguments are like this.

play12:26

Something like X equals one s is equal to

play12:31

I don't know, hello B is equal to true

play12:34

whatever. Do a capital true here.

play12:37

So now if I run this,

play12:38

you see that

play12:38

we have no positional arguments,

play12:40

but we have these three keyword arguments

play12:42

and they are stored

play12:43

inside of a dictionary.

play12:44

So if I want to access

play12:45

any of these individual

play12:46

keyword arguments, I go quarks

play12:49

and then I reference whatever the key is.

play12:50

So I want to reference X here and notice

play12:53

I get one.

play12:54

Okay, This is useful

play12:55

when you want to make

play12:56

your functions dynamic

play12:57

and you don't know

play12:58

how many regular arguments or keyword

play12:59

arguments are going to be accepting.

play13:01

Now you can obviously pass both.

play13:02

So if I do something like one, two, three

play13:05

and then some keyword arguments

play13:06

here, you'll

play13:06

now see that

play13:07

we'll get both args

play13:08

and quarks having some values

play13:10

and then we can process those values

play13:12

whatever we see fit.

play13:14

Okay, great.

play13:14

Last thing to show you

play13:15

is how to use these

play13:17

with inside of your function.

play13:18

So let's swap this round out

play13:20

and let's say we have like a, B,

play13:22

and then C is equal to true, D

play13:25

is equal to false.

play13:27

Okay.

play13:28

Now if we go here,

play13:30

we can actually use the asterisks

play13:31

to kind of break apart

play13:33

a list and pass

play13:34

different positional arguments.

play13:36

So let's I have a list here

play13:37

and I have one, two, three inside of it.

play13:39

And these are actually

play13:40

the corresponding values for both A, B

play13:43

and not see just A and B,

play13:45

If that's the case,

play13:46

I can't just pass this list

play13:47

because if I do that, it's

play13:48

going to be the positional argument for a

play13:50

So what I can do

play13:50

is put an Asterix before it

play13:52

and this is actually going

play13:53

to kind of decompose

play13:54

or break this apart

play13:55

into two individual positional arguments.

play13:58

So if I go here and I print my A and my B

play14:01

and I run this notice,

play14:02

I get one, two works perfectly fine.

play14:05

Okay,

play14:05

Now we have the same thing we can do

play14:08

with our keyword arguments.

play14:10

So let's say I have a dictionary

play14:11

that contains my keyword arguments.

play14:12

Something like C is I don't know.

play14:15

Hello. Andy is cool.

play14:18

I can actually place a dictionary here

play14:20

and then put two Asterix before it.

play14:23

And what this will do is break

play14:24

this dictionary

play14:25

into its keyword arguments

play14:26

and pass that to the function.

play14:28

So now I can print C and D

play14:31

when I have a look.

play14:32

What does it say here? C is not defined.

play14:34

Sorry, we need to add a string here.

play14:37

Always forget

play14:38

that you need to do that in python.

play14:39

Let's clear

play14:40

and rerun and notice

play14:41

now that we get the values

play14:42

for our keyword arguments.

play14:44

So the next concept here is

play14:46

if underscore,

play14:46

underscore, name equals,

play14:48

underscore, underscore me.

play14:49

Now, this is simply telling you

play14:51

if you ran the current python file.

play14:54

The reason

play14:54

why it's important to understand

play14:56

that is because a lot of times

play14:57

you can have a bunch

play14:58

of different python modules

play14:59

and sometimes you run

play15:00

the module directly.

play15:01

Other times

play15:02

it might be imported

play15:03

by a different module.

play15:04

So let's have a look at this example.

play15:06

In this case, we have Startup Pi,

play15:07

we have some function

play15:09

and then we have this

play15:10

if underscore, underscore,

play15:11

name equals equals

play15:12

underscore, underscore, main.

play15:13

We're printing run.

play15:15

Then we have another file here.

play15:17

Inside of this file,

play15:18

we import the add function from it,

play15:20

this start module.

play15:22

Now, if I didn't have this line here,

play15:24

what would happen is

play15:25

when I import this module

play15:27

by default, Python would read the entire

play15:29

kind of block of code here,

play15:30

the entire file,

play15:31

and if I didn't have something

play15:33

inside of the if statement.

play15:33

So I just had say, print run here,

play15:35

then it would actually execute

play15:36

that line of code,

play15:37

which I might not want to happen

play15:39

unless I'm actually inside of that module

play15:42

or sorry, not inside of that module.

play15:43

But if I ran that module,

play15:45

it's better if I just show it to you.

play15:46

So if I go here

play15:47

and I run

play15:48

Python Startup Pi,

play15:49

you see that

play15:50

we get run printing out to the screen.

play15:52

However, if I run my other file so Python

play15:55

other filed out

play15:56

pi notice it doesn't print out run.

play15:58

However, if I remove this line here

play16:00

and we remove the indentation

play16:02

now it will print run.

play16:04

So the purpose again of having this

play16:06

line is to determine

play16:07

if you ran this file directly.

play16:09

A lot of times

play16:10

you have a file

play16:11

where it has a ton of utility functions

play16:12

that are going

play16:13

to be imported by other files

play16:15

and then you have something

play16:15

you might want to do

play16:16

when you're running a directly,

play16:17

like maybe initializing a game

play16:19

or starting some program

play16:20

or sending an API request,

play16:22

whatever it may be.

play16:23

But you don't want this event to occur.

play16:25

You don't want this code to run

play16:27

if it's being imported,

play16:28

only if it's being ran directly.

play16:30

So that's how you use this.

play16:31

It's pretty much all you need to know,

play16:33

hopefully now you know.

play16:34

So the next concept to go over here

play16:36

does not involve my computer.

play16:37

And this is the Google or

play16:38

the Global Interpreter lock.

play16:40

Now this is exclusive to Python

play16:43

and essentially what this says

play16:44

is that any thread

play16:45

that wants to be executing needs

play16:47

to acquire the interpreter lock.

play16:49

Now, what

play16:50

that kind of technically means for you

play16:52

is that you can only execute

play16:53

one thread at the same time,

play16:55

even if you have multiple

play16:57

CPU cores on your computer.

play16:59

Now to better illustrate this,

play17:00

because I'm sure it's a bit

play17:01

confusing on your computer,

play17:03

you have a CPU or in your computer

play17:04

you have a CPU.

play17:05

That CPU will typically

play17:07

have multiple cores

play17:08

to cores for cores, eight

play17:09

cores, whatever it may be.

play17:11

Now each one of these cores

play17:12

can execute one operation at a time.

play17:15

With hyperthreading and virtualization,

play17:17

you might be able to do a few more.

play17:19

I'm not going to talk about

play17:19

all the details there for simplicity.

play17:21

Let's say each

play17:22

CPU core can execute one operation.

play17:25

Well, this is great

play17:26

because that means your CPU

play17:27

can be working on

play17:27

multiple things at the same time.

play17:29

And if you have a complex application,

play17:31

it's possible that you want to be doing

play17:33

something like processing an image

play17:35

while allowing a user to type

play17:36

something in,

play17:37

while maybe sending a

play17:38

request to the network.

play17:39

There's a ton of different things

play17:40

you could be doing at the same time.

play17:42

And this is where

play17:43

Multi-Threading comes in a thread

play17:45

is essentially

play17:46

a component of your application

play17:48

that's being executed by the CPU.

play17:50

When you start getting into

play17:51

larger programs,

play17:52

you start designing

play17:53

multi-threaded applications

play17:54

where you have different

play17:55

pieces of your code

play17:56

separated into different threads

play17:58

such that they can execute

play18:00

at the same point in time.

play18:02

Now with Python, you can do this,

play18:03

you can have multiple threads.

play18:04

The issue becomes though, that you have

play18:06

this global interpreter lock.

play18:08

Now what that means is

play18:09

even if you have a bunch of CPU

play18:10

cores on your computer,

play18:12

only one of these threads

play18:13

can be executing at a time.

play18:15

That's

play18:16

because this thread needs to acquire

play18:18

something known as a lock

play18:19

on the interpreter.

play18:20

Now, I'm not going to discuss

play18:22

why this was implemented in Python,

play18:24

but what you need to know about this

play18:25

is that if you do

play18:26

actually have multiple threads,

play18:28

this is not going to give you

play18:29

a performance bonus in Python.

play18:31

It's not going to increase the speed

play18:32

at which you execute your code.

play18:34

So to give you a simple example

play18:35

here, let's say

play18:36

I want to sum the numbers from 1 to 100.

play18:39

Well,

play18:39

if I was doing this in a single thread,

play18:41

I'd have to

play18:41

some all of the numbers

play18:42

from 1 to 100 in a row.

play18:44

However, what could be more efficient

play18:46

is if I split this into four threads

play18:48

or six threads or eight threads,

play18:49

and I summed sections of the numbers.

play18:52

For example,

play18:52

if there was four of me,

play18:53

then I could sum the first 0 to 25, 25

play18:56

to 50, 50 or 75, 75 to 100.

play19:00

And then I could add

play19:01

all of those values together,

play19:02

and this would allow me

play19:03

to sum the numbers four times faster.

play19:05

And in a traditional programing language,

play19:07

you can do this,

play19:07

you can create four threads.

play19:09

They're going to be executed

play19:10

on four different CPU cores,

play19:11

and this will allow you to very quickly

play19:13

speed up

play19:14

your programs

play19:14

using these multiple threats in Python.

play19:17

You can't do that

play19:18

even though you have

play19:19

these multiple threads.

play19:20

Only one of them can execute at a time,

play19:22

which means it doesn't matter

play19:24

how you split these things up,

play19:25

it's going to take the exact

play19:26

same amount of time,

play19:27

approximately

play19:28

the exact same amount of time

play19:29

to execute this code.

play19:31

Now, I'm going to stop here.

play19:32

Hopefully you get the point

play19:33

that you can only have one

play19:34

thread in execution at a time.

play19:36

If you know that you pretty much know

play19:38

the Global Interpreter lock.

play19:39

If you want to learn

play19:40

more than I'll encourage you

play19:41

to read about it

play19:42

or let me know in the comments.

play19:43

If you want to see me,

play19:43

make an entire video on it.

play19:45

Regardless, I'm going to wrap it up here.

play19:47

I hope that you found this helpful

play19:49

and I look forward to seeing you

play19:50

in another YouTube video.

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Python CodingDeveloper TipsMutable vs ImmutableList ComprehensionFunction ParametersCode OptimizationPython TutorialGlobal Interpreter LockCode UnderstandingProgramming Concepts
¿Necesitas un resumen en inglés?