age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Similar documents
CS 115 Lecture 4. More Python; testing software. Neil Moore

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

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

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

VLC : Language Reference Manual

Conditionals and Recursion. Python Part 4

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

Text Input and Conditionals

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Chapter 2 Getting Started with Python

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

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

printf( Please enter another number: ); scanf( %d, &num2);

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Ex: If you use a program to record sales, you will want to remember data:

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Introduction to Programming Using Java (98-388)

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Chapter 2: Using Data

CS1 Lecture 3 Jan. 18, 2019

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

CSC Web Programming. Introduction to JavaScript

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

ENGR 101 Engineering Design Workshop

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

2 nd Week Lecture Notes

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 106 Introduction to Computer Science I

IT 374 C# and Applications/ IT695 C# Data Structures

Java Bytecode (binary file)

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

LOON. Language Reference Manual THE LANGUAGE OF OBJECT NOTATION. Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee

4. Inputting data or messages to a function is called passing data to the function.

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Python: common syntax

Visual C# Instructor s Manual Table of Contents

C: How to Program. Week /Mar/05

Lecture Set 2: Starting Java

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

CSI33 Data Structures

Lecture Set 2: Starting Java

Basics of Java Programming

CS1 Lecture 3 Jan. 22, 2018

JME Language Reference Manual

CS Summer 2013

Chapter 2: Data and Expressions

Python for Non-programmers

Part III Appendices 165

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

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

Chapter 2 - Introduction to C Programming

Python Input, output and variables

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

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

L-System Fractal Generator: Language Reference Manual

Programming in Python 3

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

CS 1301 Exam 1 Answers Fall 2009

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

Flow Control: Branches and loops

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Introduction to Python

Programming for Engineers Introduction to C

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Key Differences Between Python and Java

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Algorithms and Programming I. Lecture#12 Spring 2015

CS 1301 CS1 with Robots Summer 2007 Exam 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Typescript on LLVM Language Reference Manual

Full file at

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

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Getting Started with Python

Lecture 3. Input, Output and Data Types

Fundamentals of Programming (Python) Getting Started with Programming

Introduction to C# Applications

Fundamentals of Programming Session 4

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

ASML Language Reference Manual

PLT Fall Shoo. Language Reference Manual

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Transcription:

Intro to Python

Python Getting increasingly more common Designed to have intuitive and lightweight syntax In this class, we will be using Python 3.x Python 2.x is still very popular, and the differences between the two are small Python 3.x is better optimized and more regularly improved Python 3.x has slightly different syntax, so be careful (especially when reading stuff on the web) Install from https://www.python.org/downloads/

Variables A variable is a box that we put some data in Example: age = 23 When we change the variable, we change what is in the box Example: age = age + 1 There are different data types Integers whole numbers (0,1,2,3.-1,-2,-3 ) Floating-point numbers 3.14 Strings Groups of text or even single characters (like a ) are considered strings Booleans true or false (the basis of all computing) and more (you can even create your own) In Python, you can also change the type of variable (it is loosely typed) Example: age = In my 20s Not true in Java!

Reading input from the user A function is a named piece of code that can return a value The input function is used to read input from the user input() just returns a string entered by the user input(prompt) displays the prompt string, then returns the string entered by the user IDLE example: input.py The input function always returns a string If you want to read a number from the user, use the additional functions int or float You have to cast the string into an int/float More on this later

Doing arithmetic Arithmetic is slightly complicated because there are two kinds of numbers, integers ( whole numbers ) and floating-point numbers or floats (numbers containing a decimal point) Operations are + (add), - (subtract), * (multiplication), two kinds of division, / and //, and % (modulo, or remainder of a division) When you use +, -, *, //, or % on just integers, you get an integer result // is called integer division (works as floor function, i.e., 7//2= 3) All other combinations return a float IDLE example: arithmetic.py

Using strings A string is a sequence of characters enclosed in either single quotes ' ' or double quotes " (no difference) Triple quotes also work in Python Some single characters cannot easily be entered directly into strings, and must be escaped (backslashed) \n represents a newline character \t represents a tab character \' represents a single quote (inside a singly-quoted string) \" represents a double quote (inside a doubly-quoted string) Single and double quotes don t need to be escaped with triple quotes Strings can be concatenated (joined) with the + operator IDLE example: strings.py

The print function Syntax: print(arguments) The arguments are values, variables, or expressions, separated by commas The arguments are printed (displayed on the screen) on a single line, separated by spaces Very useful function not only for printing text but also for printing the values of variables More about functions later If you re in IDLE, print is used automatically for the result

Using booleans The two boolean values are True and False The following comparison operators on numbers will give a boolean result: <, <=, ==,!= ( not equal ), >=, > 5 > 3 is True 3 > 5 is False x == 3 depends on the value of x in the program Booleans can also be assigned to variables Example: isxequalto3 = (x == 3) Booleans can be negated with not Use and and or to create more complex booleans

Branching Execute different branches of code depending on a condition Structure: if condition: do something else: do something else Could have multiple conditions with an elif IDLE example: branching.py

Loops A while loop executes some statements while some condition is true while condition: do something If condition is true, execute statements and repeat IDLE example: guessanumber.py A for loop iterates over some sequence for variable in sequence: do something variable iterates over all values in sequence IDLE example: forloop.py

Control Statements Control statements are used to decide whether and how often some other, controlled statements are executed If, elif, else statements While, for loops For every kind of control statement: The control statement ends in a colon, : The controlled statements are indented four spaces In IDLE, pressing the Tab key is the same as typing four spaces

Layout Every statement goes on a line by itself (not so in java!) Could put an optional semicolon at end of line (semicolon mandatory in java!) Indentation is crucial in python! If you don t indent correctly, your program may do something you don t expect

Functions A function is a named chunk of code that can be executed ( called ) by giving its name and whatever arguments it requires The arguments are matched up to the parameters There must be the same number of arguments as there are parameters, and they are matched up by position A function may (or may not) return a value Functions declared with a def keyword Functions are sometimes called methods More about the difference later IDLE example: gcd.py

Programs A program is code that has been saved to a file In python file has.py extension Can open in IDLE and execute by pressing F5 A program is executed as it is loaded in, top to bottom. It can be either: Just a collection of statements, executed one after the other, or A collection of functions that can be called individually from IDLE, or A collection of functions, plus special code to start the program and call the various functions as needed (more on that later)

Last comments Lines starting with # are comments Not executed by the program Note to humans looking at the program Use comments to tell what a program does or acknowledge someone else s code Do not add irrelevant comments or use comments to explain something that s obvious (b = a + c #b is assigned the sum of a and c) Errors are unavoidable most of programming is debugging A syntax error is one recognized by the compiler (the thing that gets your program ready to execute) and prevents it from even starting A runtime error is one that causes your program to crash A logic error or semantic error is one that causes your program to produce incorrect results A user error is when the user provides invalid input to the program, causing the program to crash or to produce incorrect results

Homework 1 Lunar Lander Due at 11:59pm on Tuesday, Sep. 12 Fun and simple game Use your fuel wisely so you can land safely on the moon Physics is very simplified so it shouldn t be difficult to code Mostly testing your loops, arithmetic and string parsing skills