Follow
Publications: 33 | Followers: 0

basic-syntax

Publish on Category: Birds 0

Statement atoms
The 'atomic' components of a statement are:delimiters(indents, semicolons, etc.);keywords(built into the language);identifiers(names of variables etc.);literals(values like 2 or "hello world")operators(maths symbols that do stuff).
Basic syntax
Each statement is generally a single line (statements can be stacked on a line with semicolons but this is unusual).Standard 'inline'comments(text for humans) start ## This is an inline comment.'Block' comments across multiple lines start """ and end """, or ''' and end '''"""This is a block comment."""You can have as many blank lines as you like to structure code.
Compound statements
In many languages, lines have some kind of end of line marker (e.g. a semi-colon) and code is divided into control flow blocks by braces/curly brackets:if (a < 10) {alert (a); // JavaScript}alert("done");But this allows errors (a missing bracket or semicolon can change the meaning), and requires coders to indent code inside the sections to make it clear where the control flow is.
Compound statements
In Python, lines are generally one statement per line and are grouped in sections byindentinganddedenting.You can use spaces or tabs, and as many as you like of either, but be consistent. We recommend using 4 spaces.In theory, this means formatting and syntax are one. In practice, the interpreter basically looks at whether indenting is more or less than previous lines, this works in some versions of Python:if a < 10 :print (a) # Using a mix of tabs and spacesprint ("done")But it isn't good practice. Align sections at the same flow control:if a < 10 :print (a)print ("done")
Compound statements
Overall, this causes less bugs, but you need to make sure your indenting is correct or the control flow changes.This:if a < 10 :print (a)print ("done")Is completely different to this:if a < 10 :print (a)print ("done")
Delimiters
Newlines and indenting/dedentingat the start of a line are special kinds of delimiter-ishthings, as are ' " # \Others (some of which also act as operators):( ) [ ] { }, : . ; @ = ->+= -= *= /= //= %= @=&= |= ^= &gt;>= <<= **=
Enclosing delimiters
Python especially uses three style of enclosing delimiters. These are what the Python documentation calls them:{}braces# Sometimes called curly brackets elsewhere.[]brackets# Sometimes called square brackets elsewhere.()parentheses# Sometimes called curved brackets elsewhere.
Line breaks
Lines can be joined together into a single line with a backslash, \ provided this isn't part of a piece of non-comment text or followed by a comment.Lines inside {} () [] can also be split (and commented); though non-comment text needsquotemarksaround each line.Triple quoted text may be split across lines, but the formatting (spaces;linebreaks) will be kept in the text when used (i.e. if it's not a comment).print \("""Daisy, Daisy,Give me your answer, do...""")
Keywords
The following are reserved for special uses and can’t be used for anything else:False class finally is returnNone continue for lambda tryTrue def from nonlocal whileand del global not withaselifif or yieldassert else import passbreak except in raise
Example: import
Import adds libraries of code (modulesin Python) so we can use them.Usually at the top of the file we add:importmodule_nameFor example:importbuiltinsFrom then on we can use code inside the library. We'll come back to imports when we look at the standard library.
Identifiers
These are labels stuck on things.The main use of identifiers is in constructing variables.But they are also used for naming, for example, procedures, or as Python calls themfunctions.
Builtins
As well as keywords, there are some functions (likeprint()) built into the standard distribution. These are in a module calledbuiltins.The shell loads this up automatically, but hides it with the name __builtins__ .Thisdouble-underscore notation is used in Python to hide things so they aren't accidentally altered. It is called 'dunder' notationIt's easier to deal with if you import it normally. To see what's inside, enter:>>> importbuiltins>>>dir(builtins)dir()is one of thebuiltinfunctions. Used with a module name it tells you what functions and variables are available inside the module. Used on its own, it tells you what identifiers are currently being used.

0

Embed

Share

Upload

Make amazing presentation for free
basic-syntax