Learn Python programming by example code. #learnpython #learnprogramming #pythoncoding #programming

preview_player
Показать описание
## Slide 1

Python
Code Quiz [#01]
Can You Guess
What the Following
Program Does?

#Python #Beginner #Sundry

## Slide 2

Here's the "Mystery Code":

def myst(a,b):
a,b = b,a
return a,b

What Does
The Function `myst` Do?

Here's an Example Usage:

^^^ myst(1,2)
???

What Would be the Output?

You Can Pause the Video at This Point and Think about the Code,
if You Like.

## Slide 3

Let's Go through the Code:

def myst(a,b):

The Python Keyword `def` is Used
To Define a [Function.]{.coral-text}

a,b = b,a

This Assignment Statement (`=`) Assigns
The Values on the Right Hand Side
To the Variables on the Left Hand Side,
In the Corresponding Order.

return a,b

The `myst` Function Then Returns
Them As a [Tuple.]{.coral-text}

## Slide 4

If We Run the Example Code,
In the Python Interpreter,
In the Interactive Mode,

^^^ myst(1,2)

It Will Output the Following:

(2, 1)

The Parentheses around `2,1`
Indicates That
The Output is
A Single Value, a [Tuple,]{.coral-text}
With Two Elements, `2` and `1`.

## Slide 5

The `myst` Function
Could Have Been Written
"More Elegantly"
As Follows:

def swap(a,b):
return b,a

This `swap` Function
Does Exactly the Same Thing
As the `myst` Function.
We'll Leave it
As an Exercise to the Readers.

## Slide 6

Practice Quiz:
What's the Point of Learning
Without Practice? :grin:

Define a Function `reverse_three`
That Takes Three Arguments
And Returns Them
In the Reverse Order.
For Example,

def reverse_three(a,b,c):
pass

Please Like and Subscribe @codingjoy!
Рекомендации по теме
Комментарии
Автор

Please lemme know if u have any questions. 👌

codingjoy