Basic Syntax - First Program 1

Similar documents
Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

S206E Lecture 19, 5/24/2016, Python an overview

Key Differences Between Python and Java

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Introduction to Python

ENGR 101 Engineering Design Workshop

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Python I. Some material adapted from Upenn cmpe391 slides and other sources

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Chapter 2 Writing Simple Programs

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Python in 10 (50) minutes

Introduction to Problem Solving and Programming in Python.

Variable and Data Type I

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Jython. secondary. memory

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Variable and Data Type I

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Variables, expressions and statements

Short, Unique and Mysterious

SOFT 161. Class Meeting 1.6

Programming with Python

CSCE 110 Programming I

CS1 Lecture 3 Jan. 18, 2019

Introduction to Python, Cplex and Gurobi

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CMSC 201 Computer Science I for Majors

Some material adapted from Upenn cmpe391 slides and other sources

PYTHON- AN INNOVATION

Getting Started with Python

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Ruby: Introduction, Basics

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.

And Parallelism. Parallelism in Prolog. OR Parallelism

Typescript on LLVM Language Reference Manual

Fundamentals of Programming (Python) Getting Started with Programming

Table of Contents EVALUATION COPY

CS61A Notes Week 13: Interpreters

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

Fundamentals of Programming Session 4

Accelerating Information Technology Innovation

Introduction to Python! Lecture 2

About Variables in Python F E B 1 1 T H

Accelerating Information Technology Innovation

D R S H YA M N C H AW D A

What Version Number to Install

Conditionals and Recursion. Python Part 4

Python: common syntax

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

1 Lexical Considerations

Working with JavaScript

8. Control statements

LECTURE 02 INTRODUCTION TO C++

Lab 1: Course Intro, Getting Started with Python IDLE. Ling 1330/2330 Computational Linguistics Na-Rae Han

CIS 3260 Intro. to Programming with C#

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CS1 Lecture 3 Jan. 22, 2018

Full file at C How to Program, 6/e Multiple Choice Test Bank

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Overview of the Ruby Language. By Ron Haley

PROGRAMMING FUNDAMENTALS

61A Lecture 3. Friday, September 5

CSC Software I: Utilities and Internals. Programming in Python

Algorithms and Data Structures

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Introduction to Programming

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Lexical Considerations

Financial Accounting Tutorial

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Flow Control: Branches and loops

Introduction to Python

Statements 2. a operator= b a = a operator b

Introduction to Bioinformatics

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Beyond Blocks: Python Session #1

SSOL Language Reference Manual

Data Types and Variables in C language

VLC : Language Reference Manual

Variables, Expressions, and Statements

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Scripting Languages. Python basics

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSI33 Data Structures

RTL Reference 1. JVM. 2. Lexical Conventions

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Computer Programming : C++

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Transcription:

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, Python!";#hello world program run this program as follows: $ python test.py This will produce the following result: Hello, Python! (or you can run it from eclipse IDE)

First Program 2 All python files will have extension.py What error message will you get if you save the file as test.txt? print "Hello, Python!";#hello world program What if you mistype print? Or miss a Do you need the ;? (try to learn by OBSERVING your mistakes)

Python Identifiers 1 Identify (name) a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). What error messages will you get if you do not follow this?

Python Identifiers 2 Python does not allow punctuation characters such as @, $ and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python. (is this the same in Windows/Unix?)

Python Identifiers 3 Class names start with an uppercase letter and all other identifiers with a lowercase letter. Starting an identifier with a single leading underscore indicates by convention that the identifier is meant to be private. Starting an identifier with two leading underscores indicates a strongly private identifier. If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.

Reserved Words and exec not assert finally or break for pass class from print continue global raise def if return del import try elif in while else is with except lambda yield

Lines and Indentation No braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced. The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. Both blocks in this example are fine:

Correct Indentation if True: print "True" else: print "False Or even better if True: print "True" else: print "False

Incorrect Indentation if True: print "Answer" print "True" else: print "Answer" print "False (what will the error message be?) Try a few different example to understand.

What about this? if True: print "Answer" print "True" else: print "Answer" print "False"

Multi-Line Statements total = item_one + \ item_two + \ item_three Statements contained within the [], {} or () brackets do not need to use the line continuation character. days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

Quotation in Python Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals as long as the same type of quote starts and ends the string. The triple quotes can be used to span the string across multiple lines. E.g. word = 'word' sentence = "This is a sentence." paragraph = """This is a paragraph. It is made up of multiple lines and sentences. """ error message if you mixed quotes???

Comments in Python single line All characters after the # and up to the physical line end are part of the comment and the Python interpreter ignores them. # First comment print "Hello, Python!"; # second comment

Commenting multiple lines All of the lines below are ignored by the interpreter """ you can comment multiple lines like this ""

Waiting for the User The following line of the program displays the prompt, Press the enter key to exit and waits for the user to press the Enter key: raw_input("\n\npress the enter key to exit.") Here, "\n\n" are being used to create two new lines before displaying the actual line. Once the user presses the key, the program ends. This is a nice trick to keep a console window open until the user is done with an application.

Multiple Statements on a Single Line The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block. Here is a sample snip using the semicolon: import sys; x = 'foo'; The same as import sys; x = 'foo';

Multiple Statement Groups as Suites A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class, are those which require a header line and a suite. Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or more lines which make up the suite. For example:

For example: if expression : suite elif expression : suite else : suite