Python Syntax - Everything you need to know!
Summary
TLDRThis video tutorial offers an in-depth introduction to Python syntax, focusing on indentation with spaces or tabs, emphasizing readability and uniformity. It covers the significance of using snake case for variables and functions, pascal case for classes, and capitalized snake case for constants, as per PEP 8 guidelines. The tutorial also explains the use of comments with hashtags, string literals with different quote types, and techniques for managing long statements with backslashes. Aimed at beginners, the video simplifies complex concepts and encourages interactive learning through a provided repository for hands-on practice.
Takeaways
- đ Python uses indentation with spaces or tabs for code structuring, unlike languages like JavaScript that use semicolons.
- đ The PEP 8 style guide recommends using four spaces for indentation if spaces are chosen, and one tab if tabs are used.
- đ Consistency is key: never mix spaces and tabs in Python code.
- đ Functions in Python are defined using the `def` keyword, followed by the function name and colon.
- đ Comments in Python are made using the `#` symbol, which is essential for code readability and collaboration.
- đ String literals in Python can be declared using single quotes, double quotes, or triple quotes for multi-line strings.
- đ Multi-line strings are useful for preserving formatting, such as line breaks, in the output.
- đ Long statements in Python can be split into multiple lines using a backslash for better readability.
- đ Casing conventions in Python are important for readability: snake_case for variables, functions, methods, and modules; PascalCase for classes; and UPPER_CASE for constants.
- đ For further learning, the video suggests checking out the Python category on cosec.com for tutorials, written articles, and coding repositories.
Q & A
What is the primary focus of the video?
-The primary focus of the video is to teach the basics of Python syntax, including code structuring, indentation, and commenting.
Why is indentation important in Python?
-Indentation is important in Python because it is used for code structuring, distinguishing blocks of code such as functions, loops, and conditionals.
What is the recommended number of spaces for indentation according to PEP 8?
-According to PEP 8, the recommended number of spaces for indentation is four.
Can you use tabs for indentation in Python?
-Yes, you can use tabs for indentation in Python, but it is recommended to use spaces for consistency, and mixing spaces and tabs is discouraged.
How does Python handle the end of statements compared to other languages like JavaScript?
-Python does not require a semicolon at the end of statements like JavaScript does. Instead, Python uses indentation to define the end of a statement.
What is the purpose of comments in Python code?
-Comments in Python code serve to explain the code, making it easier to understand for both the developer and others who may read the code. They are also useful for temporarily disabling code during testing.
How do you create a comment in Python?
-In Python, you create a comment by starting the line with a hashtag (#), and the rest of the line is treated as a comment and not executed.
What are the different ways to declare strings in Python?
-In Python, you can declare strings using single quotes ('string'), double quotes ("string"), triple single quotes ('''string'''), or triple double quotes ("""string""") for multi-line strings.
Why would you use multi-line strings in Python?
-Multi-line strings are used in Python when you want to preserve whitespace and line breaks in the output, which is not possible with single-line strings.
How can you separate long statements in Python for better readability?
-You can separate long statements in Python for better readability by using a backslash (\) at the end of the line before the line break.
What are the casing conventions for different elements in Python according to PEP 8?
-According to PEP 8, variables, functions, methods, and modules should be in snake_case, classes should be in PascalCase, and constants should be in UPPER_SNAKE_CASE.
Outlines
đ Python Syntax Basics
This paragraph introduces the basics of Python syntax, emphasizing the importance of indentation using white spaces or tabs. It explains that Python uses indentation to structure code, contrasting with languages like JavaScript that use semicolons. The speaker recommends using tabs for uniformity but also mentions the PEP 8 style guide, which suggests using four spaces if spaces are chosen. The paragraph also touches on the debate between tabs and spaces, highlighting the need to be consistent. The tutorial includes a practical example of creating a function in Python and how indentation works within it. The benefits of using indentation are discussed, such as better readability and ease of understanding code structure.
đ The Role of Comments in Python
The second paragraph delves into the significance of comments in Python programming. It highlights how comments serve as a tool for communication among developers and as a memory aid for oneself when revisiting old projects. The paragraph explains that comments are not executed and are marked by a hashtag. It also stresses the importance of commenting in professional programming, as it aids collaboration and code understanding. The tutorial demonstrates how to write single-line comments and briefly mentions the use of multi-line comments, although the focus remains on the syntax and purpose of comments rather than their detailed usage.
đ€ Understanding Python Strings and Long Statements
This paragraph focuses on Python's handling of strings, known as string literals. It explains the different ways to declare strings using single, double, or triple quotes, and the importance of matching the type of quote at the beginning and end of the string. The paragraph demonstrates the use of multi-line strings for preserving spaces between lines when printed, contrasting this with single-line strings. It also touches on the use of backslashes to separate long statements for better readability, providing an example of an if statement that continues across lines. The speaker emphasizes the importance of proper casing in Python, according to PEP 8 guidelines, for variables, functions, methods, modules, classes, and constants.
đ Key Python Syntax Elements
The final paragraph summarizes the key elements of Python syntax covered in the tutorial. It reiterates the use of spaces or tabs for indentation, the importance of snake case for variables and functions, pascal case for classes, and capitalized snake case for constants. It also reminds viewers of the use of backslashes to split long statements and the use of different quote types for string literals. The paragraph concludes by encouraging viewers to explore more Python tutorials on the website, suggesting that these resources will further enhance their understanding of Python programming.
Mindmap
Keywords
đĄIndentation
đĄPEP 8
đĄSyntax
đĄComments
đĄStrings
đĄMulti-line Strings
đĄLong Statements
đĄCasing
đĄSnake Case
đĄPascal Case
Highlights
Introduction to Python syntax tutorial for beginners.
Python tutorials available on cosec.com for further learning.
Emphasis on the importance of indentation in Python code structure.
Discussion on the personal preference between using spaces or tabs for indentation.
Recommendation to adhere to PEP 8 style guide for indentation consistency.
Explanation of how to use tabs and spaces for proper code indentation.
Demonstration of automatic indentation in Python code editors.
Illustration of creating and calling a function in Python.
Clarification on the absence of semicolons at the end of statements in Python.
Advantages of using indentation for code readability and structure.
Introduction to the use of comments in Python with '#'.
Explanation of the importance of comments for code clarity and collaboration.
Guidelines on declaring strings in Python using single, double, or triple quotes.
Demonstration of printing strings with single and multi-line formats.
Explanation of when to use multi-line strings for formatting purposes.
Introduction to long statements and their separation using a backslash.
Discussion on the importance of casing in Python for readability.
Explanation of snake case, pascal case, and capitalized snake case in Python naming conventions.
Summary of the key points covered in the Python syntax tutorial.
Invitation to explore more Python tutorials on cosec.com.
Transcripts
hey guys what's up everyone in today's
video i will show you everything that
you need to know about the python syntax
as you know we have a bunch of new
python tutorials on our website
cosec.com and this is one of the first
tutorials you should look at if you are
interested in learning the python
programming language i'm going to show
you all of this in a replied repository
which you can go and fork and play
around with yourself this is the best
way to practice python i'll leave a link
to that in the video description and you
can also find a link in the written
article for that you can just fork it
you can create a free account and then
directly code along so i recommend you
to do that before we get started so in
python we use white spaces or
indentation for code structuring if you
have been coding in other languages such
as javascript before you probably have
seen that certain statements are ended
with a semicolon like so this is not the
case in python in python everything is
structured using white space
it is up to you if you want to use
spaces or tabs which kind of is a
ongoing discussion between people who
prefer tabs over spaces or vice versa
but it's really personal preference we
like to use tabs because it keeps
everything more uniform
but do note if you use spaces according
to the official pep8 style guide you
should use four spaces for indentation
and if you use tabs just one tab is fine
you should never mix spaces and tabs
always stick with one so if you use a
one tab here that's one tab but if you
actually look at four spaces one two
three four that's a bit further and then
indented um but that's fine if you use
tabs which we do recommend you for ease
of use uh then
one tab is just fine
so let's look at an actual python code
snippet now don't worry about any of the
things that i'm writing here don't need
to understand it it's simply about
indentation if you create a function in
python we use the dev keyword and then
we do the name of our function
and then we do parentheses on the end
and a colon and then we do a print
statement here and you can see while i
was pressing enter here it already
automatically indented our code which
means that we are now working inside of
this function so we do
hello from our function this is a simple
print statement you will learn more
about this later again this is only
about the structure of the code and now
if i hit enter once more then i'm still
inside of this code block and if i hit
enter once more at least here in replied
every editor is different but most of
them have some form of auto indentation
and that's how you can work and if you
want to actually get back into the
function you can just go back one step
up and then press the tab key and you
will be in the exact same line here
and this is exactly what we are talking
about so this is the indentation
now if we actually want to call the
function we do our function and
parentheses on the end
and then if we run our code our code
will run and print the statement that's
coming from our function but again don't
focus on what is all of that here just
focus on the indentation part and you
can see that this is clearly indented
one tab in
you may also recognize that we don't
need any kind of semicolon at the end of
our function statement or of our print
function here
we don't need any of that so this not
only allows us to write less code but
also makes it more of a human readable
language that actually reads a little
bit like english
to summarize here
why or what are some advantages of using
this approach there is better
readability clearly defined functions by
using indentation indentation is
required which makes reading other
developers code easier so if you jump
into code of some other developer that
you haven't been working with before and
it's all correctly indented you can
directly see that this is part of the
function and if it would be indented
that way you would see that it's
actually not in the function and the
function is empty so once you develop a
little bit of an eye for that then you
can better understand why we indent code
there is also a lower barrier of entry
it makes learning python syntax easier
compared to other languages
so
all of that green syntax you see up here
is a comment and comments are essential
in programming let's write another
comment down below here
so this is a single
line comment in python
and
it won't
run
if you execute your
code comments and programming languages
are an essential tool to describe your
code not only if you work together with
other developers as previously mentioned
but also for yourself if you have a
project that you haven't been working on
for longer periods of time you will
forget parts or sometimes all of it
all of its functionality
comments help us to remember what we
have written and with pointing out key
elements of our codes
functionality writing comments is
required in most programming related
jobs since you almost always collaborate
with other developers on different
projects commenting in python is
straightforward to write a comment you
simply put a hashtag in front of your
comment like so
and this indicates a comment to the
compiler and it won't run this piece of
text when you run your code another
essential part of the python syntax or
understanding the python syntax are
strings or so called string literals so
if you're declaring strings in python
you have a bunch of options here you can
declare a string let's first create a
variable here let's call it our string
this is how you declare a variable in
python if you haven't seen that before
and we can go and we can declare a
string by using single quotes we can
call that our string
but we can also use double quotes
instead of single quotes and it doesn't
really make a difference we just have to
make sure to not use too many quotes so
we only use double quotes for this and
this will work just the same and to
prove that to you let's just print that
out here print
our string
and we quickly
comment out that function call here and
that's also a way how you can just
comment out code that you don't want to
run when you're testing something so
this is printing out our string
let's put that back to single quotes to
demonstrate that it's actually working
and then if you print that out
that prints out exactly the same we can
also use triple single quotes or triple
double quotes but it's crucial that you
always start and end a string by using
the same type of quotes for example if
you start your string with a double
quote you can't end it with a single
quote so let's look at some examples
below to better understand that whole
concept
let's create a single
line
string here
and give that
single quotes and then we say this
is a single line string using single
quotes okay so this is our single line
now we print it all out in the end let's
create a bunch of strings that we can
look at the output all together then we
create another
single line
string and we equal that to
this is another single
line string using double
quotes
okay this is our second string
and then we create a multi-line string
so to create multi-line strings we are
going to use triple single quotes or
triple double quotes we do multi
line
string
equals and then we do triple quotes and
if you just hit the button three times
it will automatically create triple
quotes here so you can see we have six
quotes all together and now we can go
ahead and we can do
this is a multi
line string using triple quotes
now let's create a final one that we
call another
multi
line string and equal that to
three
double quotes
uh triple quotes
yeah two
okay now i'm getting confused it's six
triple six double quotes and uh then we
do this is a multi-line string using
triple double oh my god triple double
quotes okay there we go now if you want
to print out all of that we can simply
do that by using a print statement so we
do print
single
line
string
and then we just copy that a bunch of
times so we don't always need to reprint
it or retype it we do that four times
and we do
it's the other one called
another
single line string and then we do
multi-line string
multi
line string
and then we do another multi-line string
another
multi-line
string there we go so if you print all
of that out
we get the result and you immediately
can see that there's actually a space
between these and there is no space in
here
so why do we actually use multi-line
strings we use multi-line strings if we
want to have some space in between the
lines that we are printing and this is
exactly what we are seeing here now if
we go to the triple single quotes
uh line of code and we say this is a
multi-line string using triple quotes
and we just hit enter and we gifted some
space in between and then we run the
code then you can actually see that
those
lines have actually been separated so
this is not the case if we do that in a
single line string so if we would do
that here it would already give us an
error because it recognizes that it's
only single quotes or single double
quotes in that case and it doesn't allow
us to hit the enter button but the same
is true for the triple double quotes
here we can also use the enter key here
to create some space between the print
statements and if we look at that again
you can see that that is actually
printing out with space in between
so to summarize that we use
multi-line strings to print out spaces
between our lines and we use usually
single
single line strings using single quotes
or double quotes when we just want to
print out a single line with any space
in between
so this is also an essential part of
python syntax so the next thing we want
to take a brief look at are so called
long statements in the python
programming language it is sometimes
required to use longer statements that
need to be separated for better
readability and we can utilize a
backslash to achieve that so what i'm
talking about here is if you write a
very long statement that would exceed
the line here and in your code editor
you maybe would not see the line anymore
because it got too long then we can
utilize a backslash to separate that
line
much like in those multi-line strings so
to do that i create a simple if
statement here to demonstrate that for
you don't worry about the logic here it
doesn't it's not necessary it's just
about the syntax
so if we do a simple uh statement here
where we check if some numbers are equal
to another or bigger or smaller than the
other one
we can just do that by creating a simple
number game here and then we do another
end statement and now let's say if we
want to continue this
logic on the next line we can do a
backslash here and then we press enter
now we need to indent our code here a
little bit for that to work and then we
do another
condition down here and then we do a
colon at the end and then we want to
print out something if that all if all
these conditions are true so we do multi
line statement is running
and let's actually check if that works
and it is working the multiline
statement is running so this is good to
know because if you run a very long
statement or if you write very long
statements here then you might run out
of space and it gets hard to read so
separating them in different lines makes
it a lot easier
the last thing i want to quickly talk
about when it comes to understanding a
basic python syntax is casing
so by casing i mean how do we actually
write our variable names and so on and
so forth
so we do casing and then we look at some
examples so while sticking to casing
isn't mandatory for your code to work it
is highly recommended to stick with the
recommended casing standards for python
according to pep8 and those standards
are
variables functions methods and modules
are written in snake case classes are
written in pascal case and constants are
written in capitalized snake case we
highly recommend sticking to these
guidelines which helps tremendously with
making your code better readable both
for you and anyone who works with you
let's take a quick look at some of the
examples below then you better
understand what i'm actually talking
about so camel case means capitalizing
the first letter of each word except the
first one and removing all spaces we do
python
syntax and equal that to a string that's
camel case
so this is how camel case looks like
that's actually not used in python a lot
very rarely i see that and it's not
recommended in the style guide so this
is just for you to understand what camel
case is the next one is important that's
pascal case capitalizing all first
letters and removing all spaces so we do
python syntax it would look like this
and then we say it's pascal
case
okay so pascal case is only used for
class names in python
for class names
okay so we got that the next one is
snake case snake case is written like
this python syntax
and then we equal that to
snake
snake case is used for variables
functions methods and modules
is used for
variables
functions
methods
and modules okay
and the last one is capitalized
snake case
so we do
python
syntax
and
capitalized is used for constants is
used for
constants
now you don't need to worry about what
constants are we have another article
and video when you want to learn more
about constants i'll link it in the
video description below and you will
learn everything you need to know about
pyson if you just check out our python
category on cosec.com
so to summarize what you have learned
today python code is indented with
spaces or tabs that was the first thing
we were talking about
in python we use snake case for
variables functions methods and modules
we use pascal case for classes we use
capitalized snake case for constants as
you j as you just have seen here we can
separate long statements with the use of
a backslash we can use different types
of quotes for string literals that's
what you have seen here
and finally we can use hashtags for
writing comments now if you want to
learn more about python we highly
recommend checking out our python
category on cosec.com if you click on
learn on the top navigation bar and
click on python there are all of our
python tutorials and all of our python
tutorials usually come with a
corresponding replica that you can
directly code with us and with a
corresponding video as well if you
prefer video content over written
content make sure to bookmark the
website and also make sure to check out
our regular videos alright guys that's
it thank you for watching and i hope to
see you back in the next video
Voir Plus de Vidéos Connexes
Write Python Code Properly!
Python - Variables - W3Schools.com
Basic Syntax Of A C Program: C Tutorial In Hindi #5
Python Tutorial for Beginners 2: Strings - Working with Textual Data
P_06 print() function & String concatenation(using +) in Python | Python Tutorials for Beginners
Curso Python #04 - Primeiros comandos em Python3
5.0 / 5 (0 votes)