Visualize ComplexCities

Similar documents
Decisions, Decisions. Testing, testing C H A P T E R 7

Flow Control: Branches and loops

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

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

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

Control Structures. CIS 118 Intro to LINUX

Control Structures 1 / 17

Begin to code with Python Obtaining MTA qualification expanded notes

Text Input and Conditionals


Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

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

Introduction to Python

CSCE 110: Programming I

CP215 Application Design

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

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

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

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

Chapter 5 Conditional and Iterative Statements (Part-II) To carry out repetitive task, python provides following iterative/looping statements:

Control of Flow. There are several Python expressions that control the flow of a program. All of them make use of Boolean conditional tests.

Introduction to Python (All the Basic Stuff)

Jarek Szlichta

2.Raspberry PI: Architecture & Hardware Specifications

CS 1301 CS1 with Robots Summer 2007 Exam 1

CME 193: Introduction to Scientific Python Lecture 1: Introduction

MEIN 50010: Python Flow Control

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Flow Control. CSC215 Lecture

Intro. Scheme Basics. scm> 5 5. scm>

Programming with Python

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CS 112: Intro to Comp Prog

Chapter 2: Functions and Control Structures

Overview of List Syntax

CS1 Lecture 3 Jan. 22, 2018

Introduction to Python

An Introduction to Python

Programming to Python

Pupil Name. Year. Teacher. Target Level. Key Stage 3 Self-Assessment Year 9 Python. Spelling Test No 3. Spelling Test No 2. Spelling Test No 1

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

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

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

Python at Glance. a really fast (but complete) ride into the Python hole. Paolo Bellagente - ES3 - DII - UniBS

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

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

Conditionals & Control Flow

(Python) Chapter 3: Repetition

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Introduction to Python

18.1. CS 102 Unit 18. Python. Mark Redekopp

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

Python 2.7 will also work for this workshop due to certain language rules, however I prefer working with the most current stable version.

Python - Variable Types. John R. Woodward

CSC Web Programming. Introduction to JavaScript

APT Session 2: Python

CMSC201 Computer Science I for Majors

Basic Syntax - First Program 1

Introduction to Bioinformatics

egrapher Language Reference Manual

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Fundamentals of Programming (Python) Getting Started with Programming

Java+- Language Reference Manual

Python for Informatics

MITOCW watch?v=0jljzrnhwoi

Lecture 8. Conditionals & Control Flow

If Statements, For Loops, Functions

Python memento TI-Smart Grids

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

CSC Software I: Utilities and Internals. Programming in Python

CPTS 111, Fall 2011, Sections 6&7 Exam 3 Review

Sequence Types FEB

Control flow statements

CS1 Lecture 3 Jan. 18, 2019

Notes on Chapter 1 Variables and String

PREPARING FOR PRELIM 1

ENGR 102 Engineering Lab I - Computation

Python - Conditional Execution

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

Beyond Blocks: Python Session #1

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

COMS 469: Interactive Media II

ENGR 101 Engineering Design Workshop

Python for C programmers

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Student Database Challenge Problem

Python: common syntax

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

PYTHON NUMPY TUTORIAL CIS 581

Test #2 October 8, 2015

Transcription:

Introduction to Python Chair of Information Architecture ETH Zürich February 22, 2013

First Steps Python Basics Conditionals Statements Loops User Input Functions

Programming? Programming is the interaction between the programmer and the computer. The Computer evaluates the written code and executes the instructions.

Python Interactive programming language. Script based programming possible. Object-oriented programming. Can be used to control Blender.

Interactive Mode Programming: Hello world! 1. Open the Python console. 2. Type: print( Hello world! ) 3. Hit Enter. 4. The console should print: Hello world!

Script Based Programming: Hello world! 1. Open a new file. 2. Type: print( Hello world! ) 3. Press the Run File -Button. 4. The output should be Hello world! and that it executed successfully.

Variables Variables are locations to store values. The thing on the left of = is the variable and the thing on the right side of = the value stored in the variable. For example: bar = 23 foo = "Hello world!" ali = True abcd = [1,2,3,4,5]

Data Types We saw three data types on the previous slide: an integer, a string and a list. There exist many different data types. The standard data types are: Boolean Number String List Tuple Dictionary

Boolean A boolean can only have one of two values, it is either true or false. iamtrue = True iamfalse = False It is mostly used to store the results of comparisons. a = 1 b = 12 foo = a == b bar = a < b ali = a > b

Number There are two main ways of storing numbers. Integer number: Whole number. foo = 42 Floating point number: Real number. bar = 2.7182818

String A string is a sequence of characters. foo = "Hello world!" The character sequence has to be between quotation marks, so that python knows what the string is.

List A list is a sequence of values of any data type. foo = [42, [1,2,3], "Hello world!", 23, 73] The elements in a list can be accessed with an index. # Store the value of the second element to bar. bar = foo[1] Attention: The first element has index 0, the second 1, and so on!

List continued We can change the elements in a list. # Set the third element to 42. foo[2] = 42 We can also add new elements to the list. # Append a string. foo.append("the quick brown fox") To remove an element we remove the value of it. # Remove the first appearance of 23 foo.remove(23)

Tuple Similar to list but of fixed length. A tuple can not be changed after initialization. foo = (42, [1,2,3], "Hello world!", 23, 73) The only difference in initialization to a list is, tuples are enclosed within parentheses.

Dictionary A dictionary is a so called hash table. This means, that every element has a key, i.e. all elements are key-value pairs. foo = {"answer":42, "who": "brown fox"} We can access the element by using their key. # Set bar to the "answer" bar = foo["answer"]

Conditional Statements A very important construct in programming are the if statements. if condition: statement1 statement2... Python executes only the parts within an if statement, if it is valid. If the condition is not met, it proceeds with the part after the if block.

Interlude The elements of a block have to be intended. In the if clause below, the statements one and two belong to the block, N does not. What does this mean? if condition: statement1 statement2 statementn

Conditional Statements ctd. It is possible, to have alternative statements when the conditions do not hold. if condition: statement1 else statement2 if condition1: statement1 elif condition2: statement2 else statement3

Loops There are two major kinds of loops. The while loop and the for loop. while condition: statement1 statement2... for iterator in sequence: statement1 statement2... What is the difference between them?

while Loop This loops as long as the condition holds. What does the following do? a = 1 while a < 128: a = a + a print(a)

while Loop This loops as long as the condition holds. What does the following do? a = 1 while a < 128: a = a + a print(a) 2 4 8 16 32 64 128

for Loop This loops through all elements in the sequence. In every loop iteration it sets the iterator to one of the values in the sequence, beginning with the first element. What does the following do? for a in [1,2,3,4]: print(a*a)

for Loop This loops through all elements in the sequence. In every loop iteration it sets the iterator to one of the values in the sequence, beginning with the first element. What does the following do? for a in [1,2,3,4]: print(a*a) 1 4 9 16

Helpful Keywords for Loops break: Terminates the current loop and resumes after the loop. continue: Terminates the current iteration and returns to the beginning of the loop.

Helpful Keywords for Loops What does the following do? for a in range(0, 10): if a < 3: print (a * 100) elif a == 5: continue elif a > 7: break else: print(a)

Helpful Keywords for Loops What does the following do? for a in range(0, 10): if a < 3: print (a * 100) elif a == 5: continue elif a > 7: break else: print(a) 0 100 200 3 4 6 7

User Input The easiest way to have an interactive program is to use command line input. The program asks you to type something into the console. This can be done the following way. # Ask the user to enter his age. age = raw_input("enter your age: ")

User Age A small program which tells you if you are old or not. age = int(raw_input("enter your age: ")) if age < 20: print("you are young.") elif age < 50: print("you are in a fine age.") else: print("you are old.")

Functions When you have a code, which has to be executed in different spots of your program, it is more applicable to create a function than rewrite the code all the time. Earlier we called the range() function. You can define such functions also yourselves.

Functions Function definitions start with the keyword def. This is followed by the function name and parentheses (). When parameters are needed for the function, they are within the parentheses. def functionname( parameters ): statements return variable

Example Following a function, which squares the input. def square(a): ret = a * a return ret You can call the function from the console (also from scripts) by invoking the name of the function along with the right number and order of parameters. # Call the function square with the parameter 3. square(3)

Helpful Links http://www.tutorialspoint.com/python http://docs.python.org/3/