Загрузка...

Python keyword "as"with examples. #programming #pythonprogramming #python #coding

## Slide 1

The Python Keyword [5]
as
Introduces
Sort of "Aliases"
Into a Python Program.
Three Main Uses:

- `import as` Statement,
- `with as` Statement.
- `try except as` Statement.
## Slide 2

Can You Answer the Following Questions?

^^^ import keyword as kw
^^^ keyword.iskeyword('as')
???
^^^ import os
^^^ os.system('echo "hi" | ex.txt')
^^^ with open('ex.txt', 'r') as f:
... f.read()
???

^^^ with (
... open('f1.txt', 'r') as f,
... open('f2.txt', 'r') as f,
... ):
... f.read()
???
## Slide 3

In the `import` Statement,
`as` is Used to "Rename"
An Imported Module.

There are Two Main Reasons
Why You Would Consider Using `as`:

- To Avoid Name Conflict,
- To Use a Name
* That is Easier to Use, or
* That is More Conventional.

For example,

^^^ import numpy as np
^^^ np.__version__

Note:
The Name `numpy` is Not Accessible
In the Importing Module.
## Slide 4

Another Kind of Use of `as` is
With the `with` Statement,
and a `except` Clause of `try` Statement.
A `with` Clause, for Example, Can Include
One or More Expression
That Returns
A Context Manager Value.
This Returned Value Can be
Accessed via the `as` Aliasing.

For example,

^^^ with (
... open('f1.txt', 'r') as f1,
... open('f2.txt', 'w') as f2,
... ):
... f2.write(f1.read())
## Slide 5

Can You Now Answer the Questions?

^^^ import keyword as kw
^^^ keyword.iskeyword('as')
Traceback (most recent call last):
File "stdin", line 1, in module
NameError: name 'keyword' is not defined

^^^ import os
^^^ os.system('echo "hi" | ex.txt')
0

^^^ with open('ex.txt', 'r') as f:
... f.read()
'hi\n'

^^^ with (
... open('f1.txt', 'r') as f,
... open('f2.txt', 'r') as f,
... ):
... f.read()

# Prints out the content of f2.txt.
## Slide 6

Our Next
Python Keyword [6] is
assert.

We've Sorted the Keywords in a Particular Way.
The Order of the Presentation of the Keywords
In Our Videos Has No Special Significance.
Please Like and Subscribe @codingjoy!

Видео Python keyword "as"with examples. #programming #pythonprogramming #python #coding канала Coding Joy
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять