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

Slides:



Advertisements
Similar presentations
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Advertisements

Introduction to Python
Chapter 2 Writing Simple Programs
CSC 9010: Natural Language Processing
Python.
Python Basic Syntax. Basic Syntax - First Program 1 All python files will have extension.py put the following source code in a test.py file. print "Hello,
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
2440: 211 Interactive Web Programming Expressions & Operators.
Input, Output, and Processing
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Introduction to Programming with RAPTOR
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Few More Math Operators
Chapter 2 Writing Simple Programs
C++ LANGUAGE MULTIPLE CHOICE QUESTION
C++ First Steps.
Software Development.
Topics Designing a Program Input, Processing, and Output
CSC201: Computer Programming
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Introduction to Python
Pamela Moore & Zenia Bahorski
Chapter 2, Part I Introduction to C Programming
Variables, Expressions, and IO
Introduction to Scripting
Programming for Geographical Information Analysis: Core Skills
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Intro to PHP & Variables
Programming for Geographical Information Analysis: Core Skills
Statements, Comments & Simple Arithmetic
Introduction to Python
Python Primer 2: Functions and Control Flow
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Lecture 2 Python Programming & Data Types
Chapter 1: Computer Systems
Coding Concepts (Sub- Programs)
CMSC 202 Java Primer 2.
Topics Designing a Program Input, Processing, and Output
Lecture 2 Python Programming & Data Types
Computing Fundamentals
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Topics Introduction to Functions Defining and Calling a Function
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Module 2 - Part 1 Variables, Assignment, and Data Types
Topics Designing a Program Input, Processing, and Output
Beginning Python Programming
Unit 3: Variables in Java
CIS 136 Building Mobile Apps
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Python Reserved Words Poster
Presentation transcript:

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

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.

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.

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:

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): ( ) [ ] { } , : . ; @ = -> += -= *= /= //= %= @= &= |= ^= >>= <<= **=

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.

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... """) https://docs.python.org/3/library/stdtypes.html#string-methods

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.

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.

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.

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.