Write Python Code Properly!

Tech With Tim
24 Aug 202125:46

Summary

TLDRThis video tutorial focuses on the essential aspects of Python coding according to PEP 8, the Python style guide. It covers key conventions such as line length, indentation, naming conventions for variables, constants, modules, functions, classes, and exceptions. The presenter also discusses auto-formatting tools like Black, spacing rules, quotation mark usage, parameter naming, import statements, string operations, and best practices for comments and exception handling. The aim is to improve code readability and consistency, even though adherence to PEP 8 is not mandatory for code correctness.

Takeaways

  • πŸ“˜ The video discusses the importance of adhering to PEP 8, the Python style guide, for writing consistent and readable Python code.
  • πŸ›  It's not incorrect to deviate from PEP 8, but doing so can lead to inconsistency, especially in large organizations.
  • πŸ”— A link to PEP 8 is provided in the video description, and it's authored by Guido van Rossum, Python's creator.
  • βš™οΈ The video recommends using an auto-formatter like 'black' to automatically adhere to PEP 8 guidelines, saving time and effort.
  • πŸ“ PEP 8 suggests a maximum line length of 79 characters, with the 80th character being where the line should wrap.
  • πŸ”‘ For indentation, PEP 8 recommends using four spaces, avoiding tabs, to prevent potential errors and maintain consistency.
  • πŸ”‘ When naming variables, functions, and methods, snake_case should be used, while classes and exceptions should follow PascalCase.
  • πŸ”  Constants should be in all capitals with underscores separating words, and are typically placed at the top of a Python file.
  • πŸ“‘ The video emphasizes the importance of consistent styling, such as the use of quotation marks, over strict adherence to every rule.
  • 🚫 It advises against using wildcard imports (e.g., 'from module import *') as it does not clearly define what is being used from the module.
  • ➑️ For string operations, using methods like `startswith` or `endswith` is preferred over slicing for checking prefixes or suffixes.

Q & A

  • What is the main focus of the YouTube video?

    -The main focus of the YouTube video is to show the proper way to write Python code by following the Python conventions and the most important parts of the PEP 8 style guide.

  • Who wrote the PEP 8 style guide?

    -PEP 8 was written by Guido van Rossum, the inventor and creator of Python.

  • Why is it important to follow PEP 8 guidelines in a large organization?

    -Following PEP 8 guidelines in a large organization ensures code consistency, which can prevent some people from being upset about inconsistencies in how Python code is written.

  • What is one of the main rules in PEP 8 regarding line length?

    -One of the main rules in PEP 8 is that any line in Python code cannot exceed 80 characters.

  • What tool does the video recommend for auto-formatting Python code in VS Code?

    -The video recommends using 'black', a popular formatter for Python, for auto-formatting code in VS Code.

  • How should variables be named according to PEP 8?

    -Variables should be named using snake_case, which means separating words with underscores and using all lowercase letters.

  • What is the naming convention for constants in Python?

    -Constants should be named using all uppercase letters and snake_case.

  • How should functions be named according to PEP 8?

    -Functions should be named using snake_case, similar to variables.

  • What is the correct way to name classes in Python?

    -Classes should be named using PascalCase, which means capitalizing the first letter of each word without underscores.

  • What should be the first parameter name in instance methods and class methods?

    -The first parameter in instance methods should be named 'self', and in class methods, it should be named 'cls'.

  • How should top-level functions and classes be spaced in a Python file?

    -Top-level functions and classes should be separated by two blank lines.

  • What is the PEP 8 guideline for spacing around operators?

    -PEP 8 recommends having spaces around operators, with the same number of spaces on each side of the operator.

  • How should default values for parameters in functions be formatted?

    -Default values for parameters should have no spaces around the equal sign.

  • What is the recommended way to import modules in Python?

    -Modules should be imported at the top of the file, each on a separate line, and using 'import *' is discouraged.

  • How should strings be enclosed in Python, according to PEP 8?

    -Strings can be enclosed in single or double quotation marks, but consistency should be maintained throughout the code.

  • What is the preferred method for checking if a variable is 'None'?

    -The preferred method is using 'is None' rather than '== None'.

  • What is the correct way to check if a string starts or ends with a specific substring?

    -The correct way is to use the '.startswith()' and '.endswith()' methods instead of string slicing.

Outlines

00:00

πŸ“ Introduction to Python's PEP 8 Style Guide

The script begins with an introduction to a Python coding tutorial, focusing on adhering to the PEP 8 style guide, authored by Python's creator, Guido van Rossum. The guide, while extensive and covering many aspects of Python coding conventions, will only be partially covered in the video. The presenter emphasizes that while adherence to PEP 8 is not mandatory for correct Python code, it is important for consistency, especially in large organizations. The video is sponsored by Harbor DB, a hybrid SQL/noSQL database, which is introduced before the main content begins.

05:01

πŸ”§ Setting Up Auto Formatting with Black in VS Code

The presenter demonstrates how to use an auto-formatter in Visual Studio Code (VS Code) to adhere to PEP 8 guidelines, specifically the rule that no line should exceed 80 characters. The auto-formatter 'black' is recommended and installed via pip. The tutorial includes instructions on how to set up 'black' as the default formatter in VS Code and how to enable auto-formatting upon saving a file, which simplifies the coding process by automatically handling line lengths and other formatting issues.

10:01

πŸ“ PEP 8 Guidelines: Line Length, Indentation, and Consistency

This section delves deeper into PEP 8, discussing the importance of line length not exceeding 80 characters and the use of four spaces for indentation. The presenter advises against using tabs for indentation to avoid potential errors. The emphasis is on consistency in coding style, whether it involves quotation marks, spaces around operators, or other stylistic choices. The section also introduces the concept of snake case for variable names and all-uppercase naming for constants, with examples provided for clarity.

15:02

πŸ“š Naming Conventions for Variables, Constants, Modules, Functions, Classes, and Exceptions

The script continues with an explanation of Python's naming conventions. Variables should use snake case, while constants are in all-uppercase with underscores separating words. Modules should be named in lowercase, with words separated by underscores if necessary. Functions follow the same naming as variables, while classes and exceptions should be named using Pascal case, with the first letter of each word capitalized. The presenter also discusses parameter naming conventions for methods in classes, emphasizing that the first parameter for an instance method should be 'self', and for a class method, it should be 'cls'.

20:02

πŸ›‘ Spacing Rules and Import Statement Conventions

The paragraph discusses spacing conventions in Python coding, including spacing around classes, methods, and functions, as well as the proper placement of import statements. It is recommended to have two blank lines between top-level classes and functions and one blank line between methods within a class. Import statements should be at the top of the file, with each import on a separate line, except when importing multiple items from the same module. The use of wildcard imports is discouraged, and a special note is made about placing imports after a module docstring if one is present.

25:03

🚫 Common Mistakes with Whitespaces and Quotation Marks

This section highlights common mistakes made with whitespaces in Python coding, such as improper spacing around brackets, commas, and function calls. It stresses the importance of consistency in spacing around operators and equal signs. The presenter also covers the correct use of single and double quotation marks, emphasizing that while both are allowed, one must be used consistently. An exception is made for triple-quoted strings, which should always use double quotation marks. The paragraph concludes with examples of incorrect whitespace usage and the correct practices.

πŸ‘€ Correct Usage of Operators and Inline Comments

The script addresses the proper use of operators, including showing the correct precedence and spacing around them. It points out common errors and provides examples of the correct usage, such as having spaces around assignment operators and avoiding uneven spacing around binary operators. Inline comments are also discussed, with the presenter explaining the correct placement and formatting, including at least two spaces from the code and one space after the hash sign, starting with a capital letter unless it doesn't make sense semantically.

πŸ”„ Checking Values and Using String Methods

The final paragraph covers various coding conventions related to checking values and using string methods in Python. It advises using 'is None' for explicit checks against None and 'is not' for negation checks, instead of '==' or '!='. The paragraph also discourages the use of empty except blocks and recommends catching specific exceptions. Additionally, it suggests using string methods 'startswith' and 'endswith' over string slicing for checking prefixes and suffixes, as they are considered more accurate and performant. The video concludes with a reminder that only a subset of PEP 8 has been covered and encourages viewers to like and subscribe for more content.

Mindmap

Keywords

πŸ’‘PEP 8

PEP 8 refers to the Python Enhancement Proposal 8, which is the Python style guide that provides conventions for writing code in Python. It is central to the video's theme as it is the main resource the speaker uses to educate viewers on the proper way to format and style Python code. The script mentions PEP 8 multiple times, emphasizing its importance in maintaining consistency and readability in Python programming.

πŸ’‘Guido van Rossum

Guido van Rossum is the inventor and creator of Python, and he is credited with writing PEP 8. His name is significant in the script as it establishes the authority of the style guide and lends credibility to the video's educational content. The script mentions Guido to highlight the origin and importance of the PEP 8 style guide.

πŸ’‘Consistency

Consistency in coding style is a key message in the video, emphasizing the importance of adhering to a set of rules or guidelines when writing code. It is mentioned in the context of following PEP 8, but also as a general principle that supersedes strict adherence to any particular rule if it enhances readability and maintainability. The script illustrates this with examples of different coding styles and the importance of being consistent within a project or codebase.

πŸ’‘Auto-formatter

An auto-formatter is a tool that automatically formats code according to predefined style guidelines, such as PEP 8. In the script, the speaker introduces 'black', a popular auto-formatter for Python, and demonstrates how it can be used in VS Code to automatically adhere to PEP 8 guidelines, such as line length and indentation, which simplifies the coding process and ensures stylistic consistency.

πŸ’‘Indentation

Indentation in Python is not only used for readability but is also a language requirement to define code blocks. The script explains that PEP 8 recommends using four spaces for indentation and warns against using tabs due to potential issues in different editors. The concept is exemplified in the script by showing how auto-formatters like 'black' can ensure correct indentation.

πŸ’‘Naming Conventions

Naming conventions are a set of rules for how to name variables, constants, functions, classes, and modules in Python. The script goes into detail about the different naming styles, such as snake_case for variables, UPPER_SNAKE_CASE for constants, and PascalCase for classes and exceptions. These conventions are part of the PEP 8 guidelines and contribute to the readability and maintainability of Python code.

πŸ’‘String Quotes

The use of single vs. double quotes for strings in Python is a point of style rather than syntax, as both are functionally equivalent. However, the script points out that PEP 8 recommends being consistent with the choice of quotes and using double quotes for triple-quoted strings, such as docstrings. This advice is part of the overarching theme of consistency in coding style.

πŸ’‘Docstrings

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition, serving as a documentation string. The script mentions that if a module has a docstring, imports can be placed after it, which is an exception to the general rule of placing imports at the top of a Python file.

πŸ’‘Imports

Imports in Python are used to bring in modules and packages from the standard library or external libraries. The script discusses the proper placement of import statements, emphasizing that they should be at the top of the file, except after module docstrings, and should each be on a separate line, except when importing multiple items from the same module.

πŸ’‘Whitespace

Whitespace in Python refers to spaces and blank lines used to separate and structure code. The script explains the correct usage of whitespace around operators, after commas, and in other contexts to enhance readability. It also touches on common mistakes, such as incorrect spacing in function calls and parameter assignments.

πŸ’‘Comments

Comments in Python are used to annotate code and are ignored by the interpreter. The script discusses in-line comments, explaining the proper use of whitespace around the comment marker (#) and the importance of starting comments with a capital letter for readability. Comments are part of the video's broader message on maintaining clear and understandable code.

πŸ’‘None Check

Checking for 'None' is a common operation in Python to verify if a variable is null or undefined. The script advises using 'is None' explicitly rather than '==' to avoid confusion with other falsy values in Python. This is an example of a specific coding practice discussed in the video to ensure code clarity and correctness.

πŸ’‘Exception Handling

Exception handling in Python is done using try and except blocks to manage errors gracefully. The script emphasizes the importance of catching specific exceptions rather than using a bare 'except:', which can make debugging more difficult by catching unexpected errors. This advice aligns with the video's theme of writing clear and maintainable code.

πŸ’‘String Methods

Python strings have various methods for checking their content, such as 'startswith' and 'endswith'. The script suggests using these methods instead of string slicing for performance and accuracy, illustrating the video's focus on efficient and idiomatic Python coding practices.

Highlights

Introduction to the importance of adhering to Python's PEP 8 style guide for writing clean and consistent code.

PEP 8 is a comprehensive style guide authored by Python's creator, Guido van Rossum, with conventions and best practices.

Highlighting the common mistakes made by developers when not following PEP 8, affecting code consistency and readability.

The significance of line length not exceeding 80 characters in Python code for better readability and maintenance.

Demonstration of using auto-formatting tools like 'black' to automatically adhere to PEP 8 guidelines, saving time and effort.

Explanation of how to set up auto-formatting in Visual Studio Code for Python development.

The rule of using spaces for indentation in Python, with a standard of four spaces per indentation level.

Consistency in code styling is emphasized as more important than strict adherence to PEP 8 where readability is concerned.

Variable naming conventions in Python, advocating for snake_case to avoid common errors like camelCase or PascalCase.

Constants in Python should be named in all upper case with underscores separating words, signifying their unchanging nature.

Module naming conventions suggest lowercase with underscores for separation, aligning with variable naming but preferring single words.

Function naming follows the same snake_case convention as variables, contrasting with class naming which uses PascalCase.

Parameter naming conventions for methods in classes, emphasizing 'self' for instance methods and 'cls' for class methods.

Spacing conventions for classes, methods, and functions, advocating for two blank lines between top-level definitions and one line for methods within classes.

Import statements should be placed at the top of Python files, with exceptions only for module docstrings, and should not mix distinct imports on the same line.

Avoiding wildcard imports and the use of specific imports from modules for clarity and better code maintenance.

Quotation mark usage in Python, with no preference between single and double but a requirement for consistency within a codebase.

Triple quoted strings for docstrings should always use double quotation marks, according to PEP 8.

Common whitespace mistakes in Python coding, such as incorrect spacing around brackets, commas, and operators.

Proper usage of spaces around assignment operators and equality checks, emphasizing consistency and correct operator precedence.

The correct way to check if a variable is equivalent to 'None' using 'is None' instead of '==' to avoid false positives with other falsy values.

Using 'is not' instead of 'not is' for checking inequality, although both are functionally equivalent, 'is not' is the conventional choice.

The importance of catching specific exceptions in try-except blocks rather than using a general except block to improve error handling.

Using string methods 'startswith' and 'endswith' over slicing for checking string prefixes and suffixes for better performance and clarity.

Conclusion summarizing the key points covered in the video, highlighting the most important and commonly mistaken aspects of PEP 8.

Transcripts

play00:08

Hello everybody and

play00:09

welcome to another

play00:10

YouTube video. So in

play00:11

today's video, I am

play00:12

going to show you the

play00:13

proper way to write

play00:15

Python code. And when I

play00:17

say that, what I mean is

play00:18

I'm going to show you

play00:19

the proper Python

play00:20

conventions and go

play00:21

through the most

play00:22

important parts of the

play00:23

Python style guide,

play00:24

which is known as pep

play00:25

eight. Now pep eight is

play00:27

a very large Python

play00:28

style guide. I'll leave

play00:29

a link in the

play00:30

description to it. It's

play00:31

written by Guido, who is

play00:32

the inventor and creator

play00:34

of Python. And while

play00:35

there's just a ton of

play00:36

things in there,

play00:37

obviously I can't cover

play00:38

everything, but I'll

play00:39

show you kind of the

play00:40

things that I see people

play00:40

making the mistake on

play00:41

most commonly and what I

play00:43

consider to be the most

play00:44

important things that

play00:45

you kind of need to know

play00:46

from that guideline.

play00:47

With that said, if you

play00:48

don't follow that

play00:49

guideline, it's not

play00:50

necessarily incorrect.

play00:51

Your code is not wrong.

play00:52

It just does not adhere

play00:53

to it, and some people,

play00:54

If you're working in

play00:55

maybe a large

play00:56

organization, for

play00:56

example, may be a little

play00:58

bit upset that you're

play00:58

not being consistent

play01:00

with how Python code

play01:01

should be written. So

play01:02

with that said, let's go

play01:03

ahead and get into the

play01:04

video after a quick word

play01:05

from our sponsor, before

play01:07

we get started, I need

play01:08

to thank Harbor DB for

play01:09

sponsoring this video.

play01:11

Harper DB is a hybrid

play01:12

SQL slash no SQL

play01:13

database that is

play01:14

accessed to be a single

play01:15

rest API endpoint.

play01:17

Harper DB provides a

play01:18

scalable and flexible

play01:19

database that improves

play01:20

your app's performance.

play01:21

With the ability to run

play01:22

sequel on Jace on data,

play01:24

you no longer have to

play01:25

pick a database based on

play01:26

the structure of your

play01:27

data. Harper DB will run

play01:29

anywhere and ingest any

play01:30

type of data. At scale,

play01:32

with Harper DB, you can

play01:33

perform all crud

play01:34

database operations

play01:35

using only one single

play01:37

endpoint. You can

play01:38

execute sequel on Jason

play01:39

data and insert data via

play01:41

Jaison CSV or sequel

play01:42

queries. Harper DB, Elsa

play01:44

removes the headache of

play01:45

database connectivity

play01:46

and set up by providing

play01:47

a geo distributed

play01:48

database that improves

play01:49

speed performance and

play01:50

latency. Harper DB is

play01:52

incredibly fast allowing

play01:53

one single node to

play01:54

handle up to a hundred

play01:55

thousand requests per

play01:56

second. It can also

play01:58

globally replicate data

play01:59

at the speed of the

play02:00

internet to put this

play02:01

into perspective, Harper

play02:02

DB benchmarks at speeds,

play02:04

37 times faster than

play02:06

Mongo DB. If you need

play02:07

any help along the way,

play02:08

Harbor DB has detailed

play02:10

documentation and tons

play02:11

of customizability for

play02:12

more advanced

play02:13

developers. Thanks again

play02:14

to Harper DV for

play02:15

sponsoring this video,

play02:17

spin up a free forever

play02:18

instance today, like

play02:19

clicking the link in the

play02:20

description. All right,

play02:21

so let's go ahead and

play02:22

get started in front of

play02:23

me here. You can see, I

play02:24

have pep eight, the

play02:25

style guide for Python

play02:27

code. This is available

play02:28

on the Python website.

play02:29

It's written by Guido

play02:31

and I just quickly

play02:31

wanted to show you what

play02:32

the table of contents

play02:33

looked like. So you can

play02:34

see how much stuff is

play02:35

actually in here. So

play02:36

obviously I won't go

play02:37

through all of this. As

play02:38

I mentioned, I'm

play02:39

probably covering, you

play02:39

know, 10% of what's

play02:41

actually in this

play02:42

document, but a lot of

play02:43

the stuff in here is

play02:43

just really nitpicky and

play02:45

something that you

play02:46

probably don't really

play02:47

need to know. Anyways,

play02:48

there you go. That is

play02:49

the guide. So what I'm

play02:51

going to do now is go to

play02:52

vs code, which is the

play02:53

IDE I'm going to use for

play02:55

this video. And the

play02:56

first thing I want to

play02:57

show you is just how to

play02:58

make your life a little

play02:58

bit easier if you're

play03:00

trying to write code

play03:01

according to the pep

play03:02

eight style guideline.

play03:04

So one of the main

play03:04

things in the pep eight

play03:06

style guideline is any

play03:07

line cannot exceed 80

play03:10

characters in Python. So

play03:12

to give you an example,

play03:13

take a look at this. I

play03:14

have this long function

play03:15

name with long

play03:16

arguments. Obviously

play03:17

this is just an example.

play03:18

And if we go here and

play03:19

look at the bottom, we

play03:20

can see this as 116

play03:22

characters. So really

play03:23

what the pep a guideline

play03:25

wants us to do is split

play03:26

this line up, such that

play03:28

it only has max 80

play03:30

characters on the first

play03:31

line. So we could do

play03:32

something simple like

play03:34

this, right? And then

play03:35

kind of get the

play03:36

indentation correct

play03:37

here.

play03:37

And then that would be

play03:38

fun. However, if we

play03:39

don't want to have to do

play03:40

this ourself, what we

play03:42

can do is actually use a

play03:43

format or an auto

play03:44

format, or where, when

play03:45

we save our file, it

play03:47

will automatically fix a

play03:48

ton of formatting issues

play03:49

for us. So I'm just

play03:50

going to show you how to

play03:51

set that up quickly,

play03:52

such that you don't have

play03:53

to automatically format

play03:54

and change the line

play03:55

length and do all that

play03:56

kind of stuff. So if

play03:57

you're working in vs

play03:58

code, then what you can

play03:59

do is start by

play04:00

installing an auto

play04:01

format or using PIP,

play04:02

which is called black.

play04:04

Now black is probably

play04:05

the most popular format

play04:06

or for Python, but the

play04:08

way you do this is PIP

play04:09

install and then black.

play04:11

Now I'm not going to

play04:12

exactly go through what

play04:13

PIP is. If you're not

play04:14

familiar with that, but

play04:16

try this command. If it

play04:17

doesn't work, you can

play04:18

try PIP three PIP, three

play04:19

should work. If you're

play04:20

on Mac or Linux. And

play04:22

then if that doesn't

play04:22

work, you can do Python,

play04:23

hyphen M PIP install.

play04:25

And if that doesn't work

play04:26

Python three hyphen M

play04:28

PIP install black, all

play04:30

right. So once you've

play04:31

installed the auto

play04:32

format or with PIP, then

play04:33

if you're working in vs

play04:34

code, what you're going

play04:35

to need to do is go to

play04:36

settings.

play04:37

So I'm going to go to

play04:37

settings settings, and

play04:39

then you're going to go

play04:39

for the Python and this

play04:41

should be the format

play04:42

provider like that. So

play04:45

if you look up Python

play04:46

format provider, then

play04:47

what you're going to do

play04:48

is select black here. By

play04:49

default, you have auto

play04:50

pep, eight doesn't work

play04:51

as well as black. So I'm

play04:52

going to select black.

play04:54

And now if you go to

play04:56

format here and you

play04:58

check the format on

play04:59

save, so format of file

play05:00

and save so

play05:01

automatically format it

play05:02

whenever you save the

play05:03

file, what's going to

play05:04

happen. When I save this

play05:06

file is going to auto

play05:08

format this line for me.

play05:09

So you can see it's made

play05:10

it such that this line

play05:11

here is exactly 80

play05:13

characters long. Just

play05:15

want to show that to you

play05:16

because that saves you a

play05:17

lot of time when you're

play05:18

actually trying to write

play05:19

code, according to the

play05:20

pep eight style

play05:20

guidelines, and it will

play05:21

fix other things for you

play05:22

as well, not just line

play05:24

length. All right. So

play05:25

now that I've covered

play05:26

the auto format or we'll

play05:27

start getting into the

play05:28

pep eight guideline more

play05:29

specifically. So the

play05:30

first thing you saw is

play05:31

that the maximum line

play05:32

length is 80 characters.

play05:34

So as soon as you get to

play05:35

80 characters, you

play05:36

should go down to the

play05:36

next line.

play05:37

Technically the maximum

play05:39

line length is 79

play05:40

characters, but the last

play05:42

character on the line

play05:43

where you go down to the

play05:43

next one is the 80th

play05:44

character, right? And

play05:46

then the next important

play05:47

rule in the pep eight

play05:48

style guideline is the

play05:49

indentation should be

play05:50

using spaces and you

play05:52

should be using four

play05:53

spaces for all of your

play05:54

indentations. So you can

play05:55

see down here, I have

play05:56

spaces and I have four

play05:57

and I'm using spaces. So

play05:59

just make sure you

play06:00

indent using spaces and

play06:01

four spaces, a lot of

play06:02

editors actually indent

play06:04

using Tam. It's fine if

play06:06

you indent using tab,

play06:07

but it can cause some

play06:08

issues and you can get

play06:09

some weird errors. So

play06:10

it's always preferable

play06:11

to use spaces and four

play06:12

spaces. All right, now,

play06:14

moving forward, the rest

play06:15

of the rules that I'm

play06:16

going to show you here

play06:17

are typically considered

play06:18

the best practice and

play06:19

what you should use.

play06:21

However, there's always

play06:22

exceptions to these

play06:23

rules and if they make

play06:24

your code less readable

play06:25

or they don't make sense

play06:26

to implement, then it's

play06:27

always fine to kind of

play06:28

ignore them and go with

play06:29

whatever style that

play06:30

you're using. Really the

play06:32

most important thing

play06:33

when you're writing any

play06:34

code, not even just

play06:34

Python code is whatever

play06:36

style you go with. It's

play06:37

consistent. So if you

play06:39

use single quotation

play06:40

marks for strings,

play06:41

always use single

play06:42

quotation marks for

play06:43

strings.

play06:43

If you're doing a space

play06:44

around your operators,

play06:45

always use a space

play06:46

around your operators,

play06:47

just make sure that

play06:48

whatever you're doing is

play06:49

consistent. That's way

play06:50

more important than

play06:51

following the strict

play06:52

rules that I'm going to

play06:53

show you here. All

play06:54

right. So now what I

play06:55

want to do is get into

play06:56

Nate. All right. So

play06:57

let's start with the

play06:58

naming convention for

play07:00

variables. So the naming

play07:01

convention for your just

play07:02

typical variables in

play07:03

Python is you snake case

play07:05

or word underscore word,

play07:07

convention, case,

play07:08

whatever you want to

play07:08

call it. So what I mean

play07:09

by that is if you have a

play07:10

variable, that is two

play07:11

words, sorry, this

play07:12

should be lowercase.

play07:14

Then what you want is

play07:15

something like this

play07:16

hello world, as opposed

play07:17

to something like this,

play07:19

this would be the

play07:20

incorrect case. This is

play07:21

camel case. Instead you

play07:23

want to use snake case.

play07:24

This is probably the

play07:25

most common error I see

play07:26

in Python. Uh, people

play07:27

use camel case when they

play07:28

should really be using

play07:29

snake case for their

play07:30

variables. Now, some

play07:32

people will also do

play07:32

something like this.

play07:33

This is known as Pascal

play07:34

case. This is also

play07:35

incorrect. You don't

play07:36

want to do that for

play07:38

variables. Now, if you

play07:39

add a third word here,

play07:40

same thing, you would

play07:41

just separate again with

play07:42

another underscore.

play07:44

There you go. Simple.

play07:45

That's all you need to

play07:45

know for the naming

play07:47

convention for

play07:47

variables. Now, the

play07:49

naming convention for

play07:50

Constance is to do all

play07:52

upper cases and then

play07:53

snake case.

play07:54

There's probably a name

play07:55

for this, but it looks

play07:56

something like this. So

play07:57

let's say you had a

play07:58

constant variable. You

play07:59

would name it like this.

play08:00

Notice it's highlighting

play08:01

in a different Cub. Now

play08:03

he constant variable is

play08:04

a variable that stores a

play08:05

value that you do not

play08:07

expect to change in the

play08:08

program. And that relate

play08:09

should not change in the

play08:10

program at all. So an

play08:11

example of a constant

play08:12

might be something like

play08:13

a color that you're

play08:14

going to use to for

play08:15

styling your website or

play08:16

styling a game or

play08:18

something that's never

play08:18

going to change. In this

play08:20

case. Again, you do an

play08:21

all capitals and then

play08:22

you would, you know, put

play08:23

whatever the value of

play08:24

the constant is. And

play08:25

then this variable

play08:26

should never have its

play08:27

value changed. It's

play08:28

should only ever just be

play08:29

used in the program as a

play08:31

constant. Now there's

play08:32

not really a way to

play08:33

enforce that this is not

play08:34

going to be changed in

play08:35

other programming

play08:36

languages. There is, but

play08:37

regardless, when you

play08:38

have a constant, you

play08:39

normally want you to put

play08:40

this at the very top of

play08:41

the program below any

play08:43

import statements and

play08:44

then have it in all

play08:45

capitals. Now, if you

play08:46

had two words, you could

play08:48

do something like this

play08:49

BG underscore color.

play08:50

This would be correct

play08:51

standing for a

play08:52

background color. You do

play08:53

it with an underscore in

play08:55

all capitals. And again,

play08:56

this goes at the top of

play08:57

your program below any

play08:59

imports statements.

play09:01

Okay?

play09:01

So that is the naming

play09:01

convention for

play09:02

constants. Now the

play09:04

naming convention for

play09:05

modules, that would be

play09:06

the actual Python file

play09:08

itself is to have it all

play09:10

in lowercase. So usually

play09:12

want one word, all

play09:13

lowercase. But if you do

play09:15

need to have multiple

play09:16

words, then you want to

play09:17

separate that with

play09:18

underscores. So just

play09:19

like you would name a

play09:19

variable, that's how you

play09:20

name your modules,

play09:21

except it's preferred

play09:23

that you just have one

play09:24

word and you don't have

play09:25

multiple words. If you

play09:26

do have multiple words,

play09:27

separate them with

play09:28

underscores. Now the

play09:30

naming convention for

play09:30

functions is the same as

play09:32

four variables. So if I

play09:33

have my hello, oops,

play09:36

underscore world

play09:37

function, this is

play09:38

correct. You do not want

play09:39

to name a function low

play09:41

world like that. Again,

play09:43

this isn't really

play09:43

incorrect. You can do

play09:45

it. There's nothing

play09:45

wrong with doing that.

play09:46

It just doesn't follow

play09:47

the Python convention.

play09:49

Okay. So that is

play09:50

functions. Now, lastly,

play09:51

we need to go over

play09:52

classes and exceptions.

play09:54

So when you name a

play09:55

class, you want this to

play09:56

be in Pascal case. That

play09:58

means an uppercase on

play09:59

the first word,

play10:00

uppercase on the second

play10:01

word. And you're having

play10:02

them kind of in camel

play10:03

case notation. So let me

play10:05

just type it out and

play10:05

you'll see what I mean.

play10:06

Let's name this base

play10:08

class or something. This

play10:09

is what you want. So you

play10:10

want a capital on the

play10:11

first word? No, no

play10:12

underscore and a capital

play10:14

on the second word.

play10:15

And that's how you name

play10:16

a class. Okay. Pretty

play10:18

straightforward. And

play10:19

then if you just had one

play10:19

word, you would just do

play10:21

it like that. Now,

play10:22

whenever you name an

play10:23

exception, same thing

play10:24

you want it to be in

play10:25

Pascal case. Now, the

play10:26

reason for that is an

play10:27

exception is a class in

play10:29

Python. And so it should

play10:30

have the same naming

play10:32

convention. So if you

play10:33

were to create an

play10:34

exception, let's just

play10:35

say, you know, this is

play10:36

called face exception or

play10:38

something. I think that

play10:39

that's actually a

play10:39

built-in class in

play10:40

Python. Let's just call

play10:41

this X exception. Then

play10:44

you would name it like

play10:44

this. Hopefully that

play10:46

makes sense. But that's

play10:47

really all of the

play10:47

important naming

play10:48

conventions in Python

play10:49

that you need to know.

play10:50

All right. The next

play10:51

thing that I want to

play10:51

cover here is just some

play10:52

parameter naming

play10:53

conventions related to

play10:55

methods in classes. So

play10:57

let's say we have a

play10:57

class. We can call this

play10:59

class test and let's say

play11:00

we have a test method.

play11:01

And this method is an

play11:02

instance method. Then

play11:04

the first parameter for

play11:05

this method should be

play11:06

named self. So a lot of

play11:08

people know this and

play11:09

they probably actually

play11:09

think you have to name

play11:11

this self, but

play11:12

technically I could name

play11:13

this S I can name this

play11:14

inst or something,

play11:16

whatever you name that,

play11:17

whatever I want, it will

play11:18

still work. It will

play11:18

still hold the instance,

play11:20

the method was called

play11:21

on, but by convention it

play11:22

should always be named

play11:24

self.

play11:24

So again, the first

play11:25

parameter in an instance

play11:26

method should always be

play11:27

named self. Now in the

play11:29

same way, if you have a

play11:30

class methods, so let's

play11:31

go at class method like

play11:33

that. And then we

play11:34

define, I don't know,

play11:34

let's go CLS underscore

play11:36

method B first parameter

play11:37

here. It should be named

play11:39

CLS. So again, a

play11:40

convention first prouder

play11:42

of any class method

play11:43

should be named CLS

play11:45

standing for class.

play11:46

There you go. Simple

play11:48

enough. Okay. So that

play11:49

was all I needed to

play11:49

cover for this. Just

play11:50

make sure you self NCLs

play11:52

as you are for first

play11:53

parameter values for

play11:55

class and for instance

play11:56

methods. All right. So

play11:57

the next convention I

play11:58

have, you has to do with

play11:59

spacing out classes,

play12:01

methods, and functions.

play12:02

Again, people mess this

play12:03

up all of the time, even

play12:05

if you decide not to

play12:06

follow these rules, just

play12:07

make sure you stay

play12:07

consistent with how you

play12:09

decide to space these

play12:10

out. So let's define a

play12:11

few classes here and

play12:13

functions and methods.

play12:14

So I'm going to show you

play12:14

what I mean. So let's

play12:16

have a, our class Fu and

play12:18

let's define in a knit.

play12:21

Okay. And we can just do

play12:22

a pass in here. Let's

play12:24

define a Fu method and

play12:28

let's do a pass inside

play12:30

of here. Okay. Let's

play12:31

define a function. We'll

play12:32

call this bar and let's

play12:35

do a pass. All right. So

play12:36

this is what I have

play12:37

right here.

play12:38

Now, any top level

play12:40

functions or classes

play12:42

should be separated by

play12:44

two white spaces or

play12:45

sorry, two blank lines.

play12:47

So what I mean by that

play12:48

is that this is correct.

play12:49

You want to separate

play12:49

your classes with two

play12:50

spaces. Here are two

play12:51

blank lines and then

play12:53

same thing here with the

play12:55

function. Now, the

play12:56

reason for this is these

play12:57

are in the top level,

play12:58

top level, meaning kind

play12:59

of furthest left

play13:00

indentation level. These

play13:01

are all global to this

play13:03

file. And so that means

play13:04

you space them out with

play13:05

two blank lines. However

play13:07

methods inside of the

play13:08

classes are spaced out

play13:10

with one blank line. So

play13:11

that's really all you

play13:12

need to know here. Just

play13:13

make sure all of your

play13:14

functions and all of

play13:15

your classes. Let's just

play13:16

do another one. Here,

play13:17

have two lines in

play13:18

between them. If they're

play13:19

in the top level, if

play13:21

they're nested inside of

play13:22

something, then that

play13:23

might change. And if you

play13:25

have methods here inside

play13:26

of a class, you separate

play13:27

them with one line. All

play13:29

right. So next I'm going

play13:30

to talk about imports.

play13:32

Now, import should

play13:32

always go at the very

play13:34

top of your file, unless

play13:36

there's some specific

play13:36

exception, in some

play13:37

reason why you can't

play13:38

import something at the

play13:39

very top of your file,

play13:40

but you always want all

play13:41

of your imports at the

play13:42

top of the file. And

play13:43

they're just on the next

play13:44

line from each other.

play13:45

So let me show you what,

play13:46

I mean, you have

play13:47

something like import S

play13:49

import sys, this is

play13:50

correct. You've imported

play13:52

things properly. Now you

play13:53

can actually import

play13:54

things on the same line.

play13:56

So import OSTP and

play13:57

import sys, but this is

play13:59

not the Python

play14:00

convention. And pep

play14:01

eight says you should

play14:02

not do this. So I should

play14:03

not import two distinct

play14:05

modules on the same

play14:06

line. I should split

play14:07

them into two lines like

play14:10

this. And again, top of

play14:12

your program, now the

play14:13

exception is that if

play14:14

you're going to be

play14:15

importing things from a

play14:16

module, you can import

play14:18

multiple of them on the

play14:19

same line. So maybe I

play14:20

say from OSTP import

play14:22

path and import, I don't

play14:23

know, stat or something.

play14:24

I dunno what other thing

play14:25

I would import for the

play14:26

less, but you get the

play14:27

idea. This is fine

play14:28

because I'm importing

play14:29

two things from one

play14:31

individual module. And

play14:32

so I can import them on

play14:34

one line. That is okay.

play14:36

You don't need to do

play14:37

something like from S

play14:39

import if I can type

play14:42

this stat like that.

play14:44

Okay. So that is kind of

play14:45

the basic rules with

play14:46

import. Now, one more

play14:47

important thing is you

play14:48

should not use the wild

play14:50

card import. What I mean

play14:51

by that is you shouldn't

play14:52

do something like from,

play14:55

uh, oh, S import asterix

play14:58

that is incorrect. You

play14:59

shouldn't do that. This

play15:00

is just not good to use

play15:01

because this will import

play15:02

everything.

play15:03

And it doesn't clearly

play15:04

define what parts of

play15:06

OSTP you're actually

play15:07

going to be using in

play15:08

your program. So just

play15:09

avoid this, if you can.

play15:10

All right. So one more

play15:11

important note here, the

play15:12

only time when you're

play15:13

not going to have your

play15:14

import statements and

play15:15

let me change this to a

play15:15

proper import statement

play15:17

at the top of your

play15:17

program is if you have a

play15:19

module doc string. So if

play15:20

you have a module doc

play15:21

string, this is a string

play15:22

that describes what's in

play15:24

this pipe on module.

play15:25

Then your imports can go

play15:26

after that. Otherwise

play15:28

I'm going to go at the

play15:28

very top of the program.

play15:29

And then after your

play15:30

imports, you're usually

play15:31

going to have any of

play15:32

your globally defined

play15:33

variables and constants,

play15:35

and then any of your top

play15:36

level functions and

play15:37

classes. All right, so

play15:38

now I'm going to cover

play15:39

strings and specifically

play15:40

what type of quotation

play15:41

marks you can use. So

play15:43

this is a big issue in

play15:43

Python as well. And I'm

play15:44

very guilty of doing

play15:46

this, but in Python, the

play15:47

single and double

play15:48

quotation marks are

play15:49

completely equivalent.

play15:50

And there actually is no

play15:51

preference on whether

play15:52

you use the single or

play15:54

the double quotation

play15:55

marks. The only rule is

play15:56

if you've use double

play15:57

quotation marks, you

play15:58

should always use double

play15:59

quotation marks. And if

play16:00

you single quotation

play16:01

marks, you should always

play16:02

use single quotation

play16:03

marks.

play16:03

The only exception to

play16:04

that would be if you

play16:05

need to actually embed a

play16:06

single or double

play16:07

quotation mark inside of

play16:08

a string. So it's

play16:09

preferential. If you

play16:10

want to put a double

play16:11

quotation mark inside of

play16:13

a string that you wrap

play16:14

the string in single

play16:15

quotation marks. So if I

play16:16

actually wanted to say

play16:17

something like, hello,

play16:18

double quotation mark,

play16:19

for some reason that I

play16:20

would put that inside of

play16:21

single quotation marks

play16:23

and in the other way

play16:23

round, if I wanted to

play16:25

use a single quotation

play16:26

mark, then I would wrap

play16:27

that inside of double

play16:28

quotation marks. Now

play16:29

that is opposed to doing

play16:31

this. So you can

play16:32

actually use an escape

play16:34

character in Python to

play16:35

escape this quote, such

play16:37

that this string is

play16:37

maintained, but it's

play16:38

preferred to just wrap

play16:39

with the opposite

play16:40

quotation marks rather

play16:41

than using an escape

play16:42

character. Although

play16:43

sometimes you do need to

play16:44

use the escape

play16:45

character. So that's

play16:46

kind of the role with

play16:47

quotation marks, just be

play16:48

consistent on whatever

play16:49

set you decide to use.

play16:50

Doesn't matter if you

play16:51

single or double, there

play16:52

is no preference in, at

play16:53

the style guidelines.

play16:54

However, when you are

play16:55

writing a doc string or

play16:56

a triple quoted string

play16:58

like this, you should

play16:59

always use a double

play17:01

quotation marks. Don't

play17:02

know why that's a thing

play17:03

I would have imagined

play17:04

you could use single or

play17:05

double as long as you're

play17:05

consistent, but it says

play17:07

you should always use

play17:08

double quotation marks

play17:09

whenever you're writing

play17:09

a triple quoted string.

play17:11

All right, so next, I'm

play17:12

going to talk about

play17:13

incorrect usage of white

play17:15

spaces. So I'm just

play17:15

going to paste a bunch

play17:16

of examples here and

play17:17

then show you what I

play17:18

mean on the incorrect

play17:19

usage of white spaces.

play17:21

So let's look at this

play17:22

one. For example, we

play17:23

have some functions,

play17:24

spam ham indexing one,

play17:26

and then we're creating

play17:26

some dictionary here.

play17:28

This is correct. You do

play17:29

not want to have white

play17:30

spaces between a, a

play17:32

bracket, race,

play17:33

parentheses, whatever it

play17:35

may be, and whatever

play17:35

comes after that. So

play17:37

looking at this, this

play17:38

would be incorrect. You

play17:39

do not want to have

play17:40

spaces like this. You

play17:41

can see all of the white

play17:42

spaces showing up, just

play17:43

don't do that, make

play17:44

everything look like

play17:45

this. All right. The

play17:46

next example is this.

play17:47

You do not want to have

play17:48

white between a trailing

play17:50

comma and a closing

play17:51

parentheses. So if you

play17:53

have something like

play17:54

this, you want to leave

play17:55

it like this, right? You

play17:56

don't want to have a

play17:56

space between the

play17:57

trailing comma and the

play17:58

closing parentheses, the

play17:59

next situation in which

play18:00

you do not. I want to

play18:01

have a white space is

play18:02

right here. So you do

play18:03

not want to, I have a

play18:04

white space in between a

play18:05

function call or in

play18:06

between something and an

play18:07

opening prophecy

play18:08

followed by arguments to

play18:10

some call. So whether

play18:12

this is a function,

play18:13

whether this is a class,

play18:14

doesn't matter, you

play18:15

don't want to have this.

play18:16

So don't separate your

play18:17

arguments from the

play18:18

actual call that you're

play18:19

making. This is the

play18:20

correct way that you

play18:21

should call a function,

play18:22

initialize a class,

play18:23

whatever it may be. All

play18:24

right? So this is the

play18:24

next example of a white

play18:26

space usage that you

play18:27

want to know. The first

play18:28

thing to note here is

play18:29

when you're declaring a

play18:29

variable, you always

play18:30

want to have at least

play18:31

one space on each side

play18:33

of the equal sign. So

play18:34

this is correct. Notice

play18:35

this is incorrect,

play18:36

right? Because we don't

play18:37

have any spaces on each

play18:38

side of the equal sign

play18:40

like that. Now, another

play18:41

thing is when you're

play18:42

using a operator like

play18:43

the plus sign or the

play18:45

minus sign, typically

play18:46

you are going to want to

play18:47

have spaces on either

play18:48

side of this operate.

play18:49

The only time in which

play18:50

you shouldn't have, that

play18:51

is when you're trying to

play18:52

show the precedents of

play18:53

operators, then you can

play18:55

actually kind of jam the

play18:56

operands together with

play18:58

the operator. Seems a

play19:00

little bit weird, but

play19:00

have a look at this

play19:01

example right here. So

play19:02

in this situation, it's

play19:03

fine to have a plus B

play19:05

and a minus B user

play19:06

trying to show these

play19:08

happen before the

play19:09

multiplication. So

play19:10

that's the only time

play19:11

when it's okay to kind

play19:11

of mess with the spacing

play19:14

here, uh, is when you're

play19:15

going to try to show the

play19:16

precedent of operators.

play19:18

Now, when you're using

play19:19

the multiplication sign,

play19:20

same thing, it's fine to

play19:21

have two things squished

play19:22

together here, because

play19:23

we're trying to show

play19:24

that these are going to

play19:25

happen before the

play19:26

addition operation.

play19:28

Okay? When you're using

play19:29

something like plus

play19:29

equals or minus equals,

play19:31

you want one space on

play19:32

each side of this

play19:33

operator. And when you

play19:35

are using any operation,

play19:37

you want the same amount

play19:38

of space on each side of

play19:39

the operator. So

play19:41

something like this is

play19:41

no good because I have

play19:43

no space here and one

play19:44

space here. So whatever

play19:45

you do, just make sure

play19:46

it's consistent. You

play19:47

should always have the

play19:48

same number of spaces on

play19:49

the left and right-hand

play19:50

side of any operative.

play19:52

Now, if you're looking

play19:53

at this, this is

play19:54

considered incorrect.

play19:55

First of all, because

play19:55

you don't have the same

play19:56

number of spaces and you

play19:57

should always have one

play19:58

space here. This one is

play20:00

considered incorrect

play20:00

because you're not

play20:01

properly showing the

play20:02

precedence of operators.

play20:04

I make this mistake all

play20:05

the time, but you should

play20:06

do X times two minus

play20:07

one, same thing here.

play20:09

You're not correctly

play20:10

showing the precedence

play20:11

of operators. So you

play20:12

should do that. And

play20:14

that, and then this one

play20:15

you could argue whether

play20:16

it's incorrect or not,

play20:17

but again, it wants you

play20:18

to do this, to show the

play20:20

precedents of the

play20:21

operators. All right?

play20:22

So the next example here

play20:23

kind of contradicts what

play20:24

I just showed you, but

play20:25

this is specific to

play20:26

white spaces in the

play20:27

default value for

play20:29

parameters in a function

play20:30

or for arguments. So

play20:32

this is correct. The

play20:34

reason this is correct

play20:35

is because you do not

play20:35

want any white spaces on

play20:37

the left or right hand

play20:38

side of the equal sign.

play20:39

When you're defining the

play20:40

default value of a

play20:41

parameter, same thing.

play20:42

When you are calling a

play20:43

function with default

play20:44

values or sorry, you're

play20:46

assigning a arguments.

play20:47

That's what I meant to

play20:48

say. You want to make

play20:49

sure you don't have any

play20:50

space so you can see why

play20:51

this is incorrect here.

play20:52

We would need to remove

play20:53

the spaces in all of

play20:55

these, for this to now

play20:56

follow the pep eight

play20:57

style guide. All right.

play20:59

So now I'm going to talk

play21:00

to you about inline

play21:01

comments. This is super

play21:02

simple, but these are

play21:03

things again that people

play21:04

mess up a lot. And it's

play21:05

going to paste in an

play21:06

example here. So you can

play21:07

see this is correct, and

play21:09

this is incorrect. So an

play21:10

in-line comment is a

play21:12

comment that goes on the

play21:13

same line as some

play21:14

expression, variable,

play21:15

whatever. In this case,

play21:17

this is correct because

play21:18

we have at least two

play21:19

spaces between the line

play21:21

and the comment. The

play21:22

comment has at least one

play21:24

space between the pound

play21:25

sign and what the actual

play21:26

content of the comment

play21:27

is. And the comments

play21:29

starts with an opera

play21:30

case.

play21:31

So that is kind of the

play21:31

rules for an in-line

play21:32

comment, at least two

play21:34

spaces between it and

play21:35

whatever the expression

play21:36

is on that line. One

play21:37

space after the pound

play21:38

sign, and then you want

play21:40

it to start with a

play21:40

capital, unless it

play21:41

doesn't make sense. I

play21:42

would start with a

play21:43

company. Now this is

play21:44

wrong because we only

play21:45

have one space here. We

play21:47

don't have a space

play21:47

between the pound sign

play21:48

and then full on to be

play21:49

even more wrong. We

play21:50

could do this. This

play21:51

would need to be a

play21:51

capital to make this

play21:52

correct. We would need

play21:53

it to look like this. So

play21:55

again, these rules

play21:56

aren't super, super

play21:56

strict, but just make

play21:57

sure you're consistent

play21:58

with however you're

play21:59

doing your inline

play21:59

comments. If you only do

play22:01

one white space, that's

play22:02

fine, but just make sure

play22:03

you always only do one

play22:04

white space. All right.

play22:06

So the next convention I

play22:07

have for you is a quick

play22:08

one, but this has to do

play22:09

with checking. If a

play22:10

value is not. So a lot

play22:12

of times what people

play22:13

will do to check if some

play22:14

variable is none is

play22:15

they'll do the

play22:16

following. If X like

play22:18

that, now this is fine.

play22:20

This will actually tell

play22:21

you if X is equal to

play22:23

none, but it will also

play22:25

tell you if X is false.

play22:27

So this is why you want

play22:28

to be explicit.

play22:29

If you're checking, if a

play22:30

variable is equal to

play22:31

none, because if I do

play22:32

something like X equals

play22:34

false, this, if

play22:35

statement will still

play22:36

trigger. If I do

play22:37

something like X equals

play22:38

zero, it will still

play22:39

trigger by do something

play22:40

like X is equal to an

play22:40

empty string. This will

play22:41

still trigger. And so if

play22:43

you only want to check,

play22:43

if the value of X is

play22:45

equal to none, then you

play22:46

should say, if X is none

play22:49

like that, now it's

play22:50

important that you do is

play22:51

none and not double

play22:53

equals none. This is

play22:54

just another rule you

play22:56

want to make sure you're

play22:56

never checking. If

play22:58

something is equal to

play22:58

none using double equal

play23:00

signs, you want to use

play23:01

it. So that's kind of

play23:03

the only real here when

play23:04

you're checking

play23:04

equivalents with none,

play23:05

be explicit and use is

play23:07

none as opposed to

play23:09

double equals none. All

play23:10

right. So the next

play23:11

convention here is

play23:12

pretty simple, but if

play23:13

you were trying to

play23:13

check, if the value of

play23:14

something is not

play23:16

something then you want

play23:17

to use is not, not, not

play23:20

is now. This is not

play23:21

specific to checking the

play23:22

value. None you could

play23:23

check, you know, let's

play23:24

just say like X, uh,

play23:26

this would be the exact

play23:26

same thing. And let me

play23:28

just make sure I use the

play23:29

same quotation marks

play23:30

here. You want to make

play23:30

sure you're using is

play23:31

not, not, not is now.

play23:33

These will actually give

play23:34

you the exact same

play23:35

result.

play23:35

It just, the convention

play23:36

to use is not. Now the

play23:38

reason I'm saying

play23:39

that'll give you the

play23:39

same result, even though

play23:40

it looks like it won't

play23:41

is because the knot is

play23:42

going to be applied to

play23:43

Fu is X not to food. So

play23:46

it's not going to negate

play23:47

Fu and then check not Fu

play23:49

is X. So it won't do

play23:51

this. What it will

play23:52

actually do is this

play23:55

okay? So it is the same

play23:56

result, but you just

play23:57

want to make sure you're

play23:58

using is not alright. So

play24:00

the next convention here

play24:01

is really simple, has to

play24:02

do with a try and accept

play24:03

block. And really the

play24:05

only thing you need to

play24:05

know here is you should

play24:06

always accept a specific

play24:08

exception. A lot of

play24:10

people don't do this. I

play24:11

don't do this all the

play24:12

time. In fact, all of

play24:13

the rules I've stayed

play24:13

here. I've broken at

play24:14

some point in time, but

play24:16

you always want to make

play24:16

sure you're accepting a

play24:17

specific exception, not

play24:19

doing an empty except

play24:21

block like this. There

play24:22

was a bunch of different

play24:23

reasons why that's the

play24:24

case. I won't really get

play24:25

into them, but just make

play24:26

sure you're almost

play24:27

always accepting a

play24:28

specific exception. And

play24:29

if you need to accept

play24:30

multiple specific

play24:31

exceptions, then do

play24:32

another, except like

play24:34

this, don't just do an

play24:35

empty except block like

play24:36

that. All right. So this

play24:37

is actually our last

play24:38

convention or rule right

play24:40

here.

play24:40

And this is, if you were

play24:41

trying to check if a

play24:42

string starts with or

play24:44

ends with something you

play24:46

should use the dot

play24:47

starts with or dot ends

play24:50

with as opposed to using

play24:51

a string slice. So these

play24:53

are just better. These

play24:55

are apparently supposed

play24:56

to be more accurate and

play24:57

more performance than

play24:58

using a slice. And so

play24:59

that's the reason why

play25:00

you're supposed to use

play25:01

them. This is weird. I

play25:02

didn't know about this

play25:03

at all. This is one of

play25:04

the main things that I

play25:05

was like, whoa. Okay.

play25:06

That's interesting. I

play25:07

hadn't heard that

play25:07

before. So just check or

play25:10

not check use starts

play25:11

with or ends with, as

play25:12

opposed to using a

play25:13

string slice when you

play25:14

want to check the prefix

play25:15

or suffix of a string.

play25:18

So with that said,

play25:18

that's pretty much going

play25:19

to wrap up this video.

play25:21

Again, I only covered a

play25:22

small subset of the pep

play25:24

eight guideline. Just

play25:25

wanted to go through

play25:26

what I thought was kind

play25:26

of the most interesting,

play25:27

most important and

play25:28

commonly mistaken stuff

play25:30

in Python. Hopefully you

play25:31

guys found some value

play25:32

from this video, if you

play25:33

did make sure they like

play25:35

subscribe to the channel

play25:36

and I will see you in

play25:37

another one..

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Python CodingPEP 8Coding ConventionsCode StyleBest PracticesAuto FormattingNaming ConventionsIndentation GuideString HandlingError Checking