Presentation is loading. Please wait.

Presentation is loading. Please wait.

Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.

Similar presentations


Presentation on theme: "Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names."— Presentation transcript:

1 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).

2 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. Inline comments are single line comments. Although they can follow code on a line, you can’t have code after them once they’re started. As we’ll see, there are some other kinds of comments. In general, for beginners, a good rule of thumb is that a quarter of your code wants to be blank lines, and a quarter comments.

3 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. We mention this, just incase you’re doing one of our courses that use JavaScript or Java, or the advanced course, where we’ll see some C.

4 Compound statements In Python, lines are generally one statement per line and are grouped in sections by indenting and dedenting. 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 spaces print ("done") But it isn't good practice. Align sections at the same flow control: print (a) In most IDEs, you can indent by selecting a section of code and pushing the tab key, while you can dedent (the opposite) by holding the shift key while pushing the tab key. However, if you use an IDE, it is best to make sure it automatically converts tabs to spaces. There’s nothing wrong with tabs, as long as you are consistent in using them, but the Python Software Foundation recommends spaces.

5 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:

6 Delimiters Newlines and indenting/dedenting at the start of a line are special kinds of delimiter-ish things, as are ' " # \ Others (some of which also act as operators): ( ) [ ] { } , : . = -> += -= *= /= //= &= |= ^= >>= <<= **=

7 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. We'll try and use the Python terms. As we'll see in future lectures, these delimiters are especially important in Python.

8 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 needs quotemarks around 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... """)

9 Keywords The following are reserved for special uses and can’t be used for anything else: False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise E.g. they can't be used for variable names. Later in the course, we'll see some other things you need to be aware of when naming variables, but for now they can be pretty much anything else.

10 Example: import Import adds libraries of code (modules in Python) so we can use them. Usually at the top of the file we add: import module_name For example: import builtins From then on we can use code inside the library. We'll come back to imports when we look at the standard library.

11 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 them functions.

12 Builtins As well as keywords, there are some functions (like print()) built into the standard distribution. These are in a module called builtins. The shell loads this up automatically, but hides it with the name __builtins__ . This double-underscore notation is used in Python to hide things so they aren't accidentally altered. It is called 'dunder' notation It's easier to deal with if you import it normally. To see what's inside, enter: >>> import builtins >>> dir(builtins) dir() is one of the builtin functions. 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. In general, we'll write functions with following parentheses, to mark them out from variables.


Download ppt "Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names."

Similar presentations


Ads by Google