MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Size: px
Start display at page:

Download "MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON"

Transcription

1 MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

2 Table of contents 2 1. Learning Outcomes 2. Introduction 3. The first program: hello world! 4. The second program: hello (your name)! 5. More data types 6. Important things to know 7. If Statement 8. The third program: boy or girl? 9. For Statement 10. The fourth and fifth programs: your name + hello students! 11. While Statement 12. The sixth program: security word 13. Functions 14. Seventh program: my functions 15. Interesting links 16. Exercises

3 3 1. Learning Outcomes

4 Learning Outcomes 4 State the basis of Python programming Discover the data types Learn the different types of statements Do some simple programs

5 5 2. Introduction

6 Why Python? 6 Easy to learn Powerful programming language Efficient high-level data structures Simple but effective approach to object-oriented programming Elegant syntax and dynamic typing

7 How to use Python? 7 From the shell or from the saved programs. The shell executes each instruction as you type it Useful for experiments Each line starts with three chevron: >>> The saved programs are executed all at once. If something isn t correct, an error line will appear in red explaining which kind of mistake it is and where it is

8 How to get Python? 8 From a PC, downloadable for free in the official website:

9 How to get Python? 9 From Raspberry Pi, it is already downloaded and ready to use in the desktop (IDLE or IDLE 3)

10 Comments 10 They start with a hash character (#) They finish at the end of the line

11 Numbers 11 int: signed integer numbers long: long integers (can also be represented in octal or hex) float: real numbers, fractional part, floating point complex: complex numbers (j for the imaginary part)

12 Numbers 12 0x indicates hexadecimal L, l indicates long integer It is better to write an uppercase L to avoid confusion with the number 1

13 13 3. The first program: hello world!

14 First steps 14 Open the IDLE File / New File Write your code on the new window! Save it Save before running the program (ctrl + s) If not, it will ask you to save it. Always say yes! To run: Run / Run Module or F5 Be careful!!! Python is case sensitive To interrupt the program: ctrl+c

15 Hello world! 15

16 Hello world! 16

17 17 4. The second program: hello (your name)!

18 Hello (your name)! 18 Now, try it yourself! Create a questionnaire and swap places with your classmates to see who has the best one

19 19 5. More data types

20 Strings 20 Contiguous set of characters in between quotation marks, which can be simple ( ) or double ( ) Accessing a single positing of a string: String[n] Where n is the desired position Accessing more than one position in a string: String[0:2] Take the positions 0 and 1 of the string The first number is included The second isn t!! Remember: the first position is included, the last isn t

21 Strings 21 Accessing more than one position in a string: String[:2] Take the first two positions If there is no number 0, from the beginning String[2:] Take from the second position to the end If there is no number the end + concatenation * repetition

22 Strings 22 Now, try it yourself! Create a string and play with it

23 Lists 23 Enclosed within square brackets ([ ]) Separated by commas (,) It is possible to have different types of data within the same string They can be accessed as the strings

24 Lists 24 Now, try it yourself! Create a list (or two) and play with them

25 Tuples 25 Similar to lists, but tuples CAN T BE MODIFIED Only-read list Enclosed within parentheses ( ( ) ) Separated by commas

26 Tuples 26

27 Modifying Lists 27 Now, try it yourself! Take the lists that you ve just created and modify some elements

28 Dictionary 28 Enclosed within curly brackets ({ }) Separated by commas (,) It is possible to have different types of data within the same dictionary Function (variable) in (dictionary) It tells us whether the variable is in the dictionary (True) or not (False)

29 Dictionary level 1 29 Now, try it yourself! Create your own shopping list

30 Dictionary level 2 30 Now, try it yourself! Create your own personalised messages for checking the items on your shopping list

31 Dictionary level 3 31 Now, try it yourself! Swap places with your classmates and see who gets the highest score

32 Converting types 32 int(x [,base]) long(x [,base] ) float(x) complex(real [,imag]) str(x) tuple(s) list(s) hex(x) oct(x) Converts x to an integer. base specifies the base if x is a string. Converts x to a long integer. base specifies the base if x is a string. Converts x to a floating-point number. Creates a complex number. Converts object x to a string representation. Converts s to a tuple. Converts s to a list. Converts an integer to a hexadecimal string. Converts an integer to an octal string.

33 Calculator * will return the same type of number if one is float, then the result will be float / will always return a float (decimals) // forces a floor division (no decimals) div % calculates the remainder mod

34 Calculator 34 Integer Float Now, try it yourself! Do some Maths directly on the Shell

35 35 6. Important things to know

36 Important things to know 36 Declaration: = Used to define the value of a variable or a constant Comparison: == Used to compare one variable or constant with a value It appears in statements such as if and while print ( ) len(string) help(topic) parentheses needed! it gives you the length of a string it opens the help menu about the topic No need for semicolon at the end of the sentences! Key words in lowercase!

37 Important things to know 37 Identifiers: Must start with a letter or an underscore Punctuation $,, %, etc. are not allowed \n It creates a line break You can write as many as you want together to create more than one blank line. eg. \n\n\n would create 3 blank lines True and False (Boolean) must have the first letter upper-case! If so, they should be red (automatic)

38 Important things to know 38 If we don t want to end the program until the user has pressed the enter key: input( Press the enter key to exit ) (Yes, it has to be the enter key) If you are printing strings and numbers in the same line (the same print statement), you should convert the numbers to string using the function str() print( Your favourite number is +str(favnumber))

39 Important things to know 39 If we need that a string occupies more than one line, start it with triple quotes ( ) or ( ) If a string has to contain an apostrophe ( ), it should be written with a backslash before it to avoid Python interpreting it as the end of the string eg. print( They aren\ t studying hard enough.)

40 Coding Styles 40 Use 4-space indentation, and no tabs. Wrap lines so that they don t exceed 79 characters. Use blank lines to separate functions and classes, and larger blocks of code inside functions. When possible, put comments on a line of their own. Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4)

41 Common Mistakes 41 Double check that: The indentation of all loops is correct The blank spaces ( ) are where they should be Extra spaces may lead into errors All brackets are closed When editing the code, whichever is enclosed in the brackets will be momentary highlighted in grey You have colons (:) after statement words The spelling of the variables, functions and constants is the same throughout all the code Remember that Python is very sensitive!

42 42 7. If Statement

43 if 43 if (condition 1): Action 1 if (condition 1): else: Action 1 Action 2 if (condition 1): Action 1 elif (condition 2): Action 2 elif (condition 3): Action 3

44 44 8. The third program: boy or girl?

45 Boy or girl? version 1 45 We need to convert the input to an integer! Now, try it yourself! Create your own personalised messages and options

46 Boy or girl? version 1 46

47 Boy or girl? version 2 47 Now, try it yourself! Create your own personalised messages

48 Boy or girl? version Now, try it yourself! Create your own personalised messages with different options and different questions

49 49 9. For and Range Statements

50 For 50 In other programming languages, for is used to iterate a sequence over certain conditions defined by the user but not in Python! In Python, it iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. for (i) in (string):

51 For 51 Now, try it yourself! Create your own list and iterate on it to print all of its elements on separate lines Why not trying adding something after each word?

52 Range 52 Is like the for function in other programming languages for (i) in range(n): Remember that Python works with 0 as the first element! Now, try it yourself!

53 Fourth and fifth programs: your name + hello students

54 Your name in different lines 54 Now, try it yourself! Create your own question and iterate it

55 Hello students 55 Now, try it yourself! Create your own list and iterate on it to print all of its elements on separate lines with a message

56 While Statement

57 While 57 The loop is executed as long as the condition is true Operands for the condition: < > == <= >=!= less than greater than equal to less than or equal to greater than or equal to different to while (True): Infinite loop!

58 While 58 Now, try it yourself! Create your own loop with some operations on it

59 Sixth program: security word

60 Security word level 1 60

61 Security word level 2 61 If the user has typed the security word wrong 5 times, then wait for 30 seconds before allowing the user to type it again. To do so: import time (at the very beginning of the program) when you want the delay: time.sleep(s) being s the number of seconds you want the program to stop

62 Security word level 3 62 If the user writes down the security word wrong 5 times, wait for 30 seconds. The second time that the user writes 5 wrong security words, wait for 60 seconds. The third time, wait for 90 seconds. The fourth, 120 seconds. And so on

63 Security word level 3 63

64 Security word PRO 64 Now, we are going to emulate a login system from any website/computer First, you need to ask the user for a login name

65 Functions

66 Functions 66 Useful for shortening the code and automatising sets of actions that will be repeatedly used together and in the same order def function_name(internal_name_input1, internal_name_input2): print(output1, output2) To call it: function_name(value_1st_input, value_2nd_input)

67 Functions 67 Now, try it yourself! Create your own function and call it as man times as you want with different input values

68 Seventh program: my functions

69 My functions 69 Create a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers. Make sure that it works with any list of numbers, regardless of its length. For that, create a couple of lists which have different lengths and try to call the function with each.

70 My functions solutions 70 Now, try it yourself! Create your own list and use it in a function

71 My functions solutions 71 Now, try it yourself! Create your own list and use it in a function

72 Interesting links

73 Interesting links

74 Exercises Remember to do the flowchart and the pseudo code BEFORE programming!!!

75 0: A, B, C, D or E? 75 Take the pseudo code of the exercise where the user is asked the result of an exam (0-100) and the program returns the equivalent mark in terms of A, B, C, D or E.

76 0: A, B, C, D or E? solution 76

77 1: Histogram 77 Create a program which, given 7 inputs, writes down a histogram. (Note that some of the inputs can be 0) eg. 2, 5, 6, 8, 10, 7, 11 ** ***** ****** ******** ********** ******* ***********

78 1: Histogram solution 78

79 1: Histogram solution 79

80 1: Histogram solution with for and list 80

81 2: 99 bottles of beer 81 Level 1: Traditional song in the USA and Canada 99 bottles of beer on the wall, 99 bottles of beer. Take one down, pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take one down, pass it around, 97 bottles of beer on the wall. etc. Create a Python program which generates all the verses of this song.

82 2: 99 bottles of beer 82 Level 2: Modify the last verse like this (underlined): 1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, no more bottles of beer on the wall. No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.

83 2: 99 bottles of beer 83 Level 3: If you have developed your code using range, now use while If you have developed your code using while, now use range Level 4: Learn the song and sing it!

84 2: 99 bottles of beer solution 84 Using range

85 2: 99 bottles of beer solution 85 Using while

86 2: 99 bottles of beer solution 86

87 3: age guess 87 Level 1: start with the basic functionality Ask the year when the user was born Display the age Level 2: Polish the program asking the user if he or she has already celebrated the birthday this year. This will mean a ± 1 year. Level 3: Greet the user and make a sentence with his or her name plus his or her age eg. (name), you re (age) years old!

88 3: age guess 88 Level 4: If the user has born this year, display a special message eg. You are still a baby! Level 5: Avoid negative ages by displaying a message if the user enters a year superior to that we are in (2014) eg. Oops! You aren t born yet! Level 6: In the same situation as before, ask the user to come back in X years time, when he or she will be born eg. Come back in (X) years time!

89 3: age guess 89 Level 7: Instead of pressing 1 or 2 to know if the birthday has been celebrated yet, ask the user to write yes or no (lowercase) Level 8: Create age blocks with personalised messages for each 1~12: 13~19: 20~30: 31~65: >65: You are a child! You are a teenager! You are a youngster! You are an adult! You are a senior!

90 3: age guess solution 90 Levels 1 to 6

91 3: age guess solution 91 Levels 7 and 8

92 The random function with a list Import the random module at the very first line of the program. import random 2. Create a list of things to be randomly picked. This means that if you want a random animal, you will have to create a list of animals. 3. Assign to a variable a random value of the list. my_fav_animal = random.choice(animals) 4. Now you can use my_fav_animal as usual!

93 The random function with a list 93 Now, try it yourself! Create your own list and randomly pick one of its items

94 The random function with numbers Import the random module at the very first line of the program. import random 2. Assign to a variable a random value of the defined range. random_num = random.randint(1,100) 3. Now you can use your random_num as usual!

95 The random function with numbers 95 Now, try it yourself! Randomly pick one number from a defined range

96 4: Story time 96 You are going to create a story which will be different every time you run the program For that, we will create different lists and use the random function

97 4: Story time 97 Ask the user his or her name and gender and store them in appropriate variables Create a variable called pronoun which will obviously be he, she or it. Assign it depending on the gender Create four lists: Roles, names, actions and places eg. roles = [ knight, princess, prince, wizard ] They can have as many items as you want and not all the list have to have the same number of words

98 4: Story time 98 Now write the story, all in the same line! Suggestion: write the story in a variable and then do a print of this variable If you aren t feeling imaginative, you can borrow this story: Once upon a time, there was a (role) called (user s name). (pronoun) and some friends found themselves in the magic land of (place). This land was ruled by (name) the (role2). All of a sudden a mysterious voice spoke to them from high in the sky and said: you must (action) (name) the (role2) to lift the curse of not being able to use technology

99 4: Story time solution 99

100 5: Guess the number 100 Level 1: The program will randomly generate a number between 1 and 100 and the user will have to guess it. To help the user, the program will say whether the number to be guessed is higher or lower than that the user entered. Level 2: Interact with the user and display personalised messages with his or her name.

101 5: Guess the number 101 Level 3: Count how long does it take to the user to guess the number and display different messages depending if the user has guessed it in the first round or not. Level 4: If the number guessed by the user is really close to the actual number (let s say it s 5 or less units lower or higher), display a special message

102 5: Guess the number a bit of help 102 Level 4: if users_num < random_num: if users_num random_num <= 5 print ( You\ re almost there! Just a little bit higher! ) if users_num > random_num: if users_num random_num >= 5 print ( You\ re almost there! Just a little bit lower! )

103 6: Hangman 103 I guess you all know how to play hangman! The user can only fail 5 times Level 1: Fixed word The remaining guesses are just a number on the screen (not the actual hangman) Level 2: List of words, random choice of them

104 6: Hangman 104 Level 3: Display a hangman. You can do one like this: O \_ _/ / \ d b Level 4: Give the option that another user writes down the secret word! (If there is only one player, the program will choose the secret word. If there are two players, the first user will write the word down and the second will have to guess it). Hint: after the 1 st player has written down the word, introduce a lot of spaces so the 2 nd player can t see the word!

105 6: Hangman solution 105 Levels 1 to 3

106 6: Hangman solution 106 Level 4

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

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

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python CS95003 - Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / 2014 Subjects 1) Beginning with Python 2) Variables 3) Strings 4) Basic arithmetic operators 5) Flow control 6) Comparison

More information

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

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

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

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

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

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

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

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1 Review Midterm Exam 1 Review Midterm Exam 1 Exam on Monday, October 7 Data Types and Variables = Data Types and Variables Basic Data Types Integers Floating Point Numbers Strings Data Types and Variables

More information

Expressions and Variables

Expressions and Variables Expressions and Variables Expressions print(expression) An expression is evaluated to give a value. For example: 2 + 9-6 Evaluates to: 5 Data Types Integers 1, 2, 3, 42, 100, -5 Floating points 2.5, 7.0,

More information

ENGR 101 Engineering Design Workshop

ENGR 101 Engineering Design Workshop ENGR 101 Engineering Design Workshop Lecture 2: Variables, Statements/Expressions, if-else Edgardo Molina City College of New York Literals, Variables, Data Types, Statements and Expressions Python as

More information

Lecture 3. Input, Output and Data Types

Lecture 3. Input, Output and Data Types Lecture 3 Input, Output and Data Types Goals for today Variable Types Integers, Floating-Point, Strings, Booleans Conversion between types Operations on types Input/Output Some ways of getting input, and

More information

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

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s 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

More information

Variables, expressions and statements

Variables, expressions and statements Variables, expressions and statements 2.1. Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. The values we have seen so far are 2 (the

More information

COLLEGE OF ENGINEERING, NASHIK-4

COLLEGE OF ENGINEERING, NASHIK-4 Pune Vidyarthi Griha s COLLEGE OF ENGINEERING, NASHIK-4 DEPARTMENT OF COMPUTER ENGINEERING Important PYTHON Questions 1. What is Python? Python is a high-level, interpreted, interactive and object-oriented

More information

Python Intro GIS Week 1. Jake K. Carr

Python Intro GIS Week 1. Jake K. Carr GIS 5222 Week 1 Why Python It s simple and easy to learn It s free - open source! It s cross platform IT S expandable!! Why Python: Example Consider having to convert 1,000 shapefiles into feature classes

More information

Variable and Data Type I

Variable and Data Type I Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad September 24, 2016 Variable is reserved a location in memory to store

More information

Variable and Data Type I

Variable and Data Type I The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Intro. To Computers (LNGG 1003) Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad February 18, 2017 Variable is reserved

More information

Python: common syntax

Python: common syntax Lab 09 Python! Python Intro Main Differences from C++: True and False are capitals Python floors (always down) with int division (matters with negatives): -3 / 2 = -2 No variable data types or variable

More information

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

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators 1 Professor: Sana Odeh odeh@courant.nyu.edu Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators Review What s wrong with this line of code? print( He said Hello ) What s wrong with

More information

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners Getting Started Excerpted from Hello World! Computer Programming for Kids and Other Beginners EARLY ACCESS EDITION Warren D. Sande and Carter Sande MEAP Release: May 2008 Softbound print: November 2008

More information

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

Decisions, Decisions. Testing, testing C H A P T E R 7 C H A P T E R 7 In the first few chapters, we saw some of the basic building blocks of a program. We can now make a program with input, processing, and output. We can even make our input and output a little

More information

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

Introduction to: Computers & Programming: Review prior to 1 st Midterm Introduction to: Computers & Programming: Review prior to 1 st Midterm Adam Meyers New York University Summary Some Procedural Matters Summary of what you need to Know For the Test and To Go Further in

More information

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS 1439-1440 1 Outline 1. Introduction 2. Why Python? 3. Compiler and Interpreter 4. The first program 5. Comments and Docstrings 6. Python Indentations

More information

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION LESSON 15 NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION OBJECTIVE Learn to work with multiple criteria if statements in decision making programs as well as how to specify strings versus integers in

More information

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

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2011 Python Python was developed

More information

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

S206E Lecture 19, 5/24/2016, Python an overview S206E057 Spring 2016 Copyright 2016, Chiu-Shui Chan. All Rights Reserved. Global and local variables: differences between the two Global variable is usually declared at the start of the program, their

More information

An introduction to Python

An introduction to Python supported by Abstract This guide provides a simple introduction to Python with examples and exercises to assist learning. Python is a high level object-oriented programming language. Data and functions

More information

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python CONDITION CONTROLLED LOOPS Introduction to Programming - Python Generating Random Numbers Generating a random integer Sometimes you need your program to generate information that isn t available when you

More information

CS 102 Lab 3 Fall 2012

CS 102 Lab 3 Fall 2012 Name: The symbol marks programming exercises. Upon completion, always capture a screenshot and include it in your lab report. Email lab report to instructor at the end of the lab. Review of built-in functions

More information

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

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2 Basic Concepts Computer Science Computer - Science - Programming history Algorithms Pseudo code 2013 Andrew Case 2 Basic Concepts Computer Science Computer a machine for performing calculations Science

More information

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 17. Fundamental Concepts Expressed in JavaScript Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program

More information

Python - Variable Types. John R. Woodward

Python - Variable Types. John R. Woodward Python - Variable Types John R. Woodward Variables 1. Variables are nothing but named reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

More information

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

CS 115 Lecture 4. More Python; testing software. Neil Moore CS 115 Lecture 4 More Python; testing software Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 8 September 2015 Syntax: Statements A statement

More information

Python Unit

Python Unit Python Unit 1 1.1 1.3 1.1: OPERATORS, EXPRESSIONS, AND VARIABLES 1.2: STRINGS, FUNCTIONS, CASE SENSITIVITY, ETC. 1.3: OUR FIRST TEXT- BASED GAME Python Section 1 Text Book for Python Module Invent Your

More information

Chapter 2 Getting Started with Python

Chapter 2 Getting Started with Python Chapter 2 Getting Started with Python Introduction Python Programming language was developed by Guido Van Rossum in February 1991. It is based on or influenced with two programming languages: 1. ABC language,

More information

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications Hello World! Computer Programming for Kids and Other Beginners by Warren Sande and Carter Sande Chapter 1 Copyright 2009 Manning Publications brief contents Preface xiii Acknowledgments xix About this

More information

Fundamentals: Expressions and Assignment

Fundamentals: Expressions and Assignment Fundamentals: Expressions and Assignment A typical Python program is made up of one or more statements, which are executed, or run, by a Python console (also known as a shell) for their side effects e.g,

More information

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

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes GIS 4653/5653: Spatial Programming and GIS More Python: Statements, Types, Functions, Modules, Classes Statement Syntax The if-elif-else statement Indentation and and colons are important Parentheses and

More information

Introduction to Python

Introduction to Python Introduction to Python خانه ریاضیات اصفهان فرزانه کاظمی زمستان 93 1 Why Python? Python is free. Python easy to lean and use. Reduce time and length of coding. Huge standard library Simple (Python code

More information

Topic 2: Introduction to Programming

Topic 2: Introduction to Programming Topic 2: Introduction to Programming 1 Textbook Strongly Recommended Exercises The Python Workbook: 12, 13, 23, and 28 Recommended Exercises The Python Workbook: 5, 7, 15, 21, 22 and 31 Recommended Reading

More information

UNIVERSITÀ DI PADOVA. < 2014 March >

UNIVERSITÀ DI PADOVA. < 2014 March > UNIVERSITÀ DI PADOVA < 2014 March > Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined syntax. Easy-to-read: Python code is much more clearly defined and visible

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

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

Ex: If you use a program to record sales, you will want to remember data: Data Variables Programs need to remember values. Ex: If you use a program to record sales, you will want to remember data: A loaf of bread was sold to Sione Latu on 14/02/19 for T$1.00. Customer Name:

More information

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

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING MULTIPLE SELECTION To solve a problem that has several selection, use either of the following method: Multiple selection nested

More information

CSCE 110 Programming I

CSCE 110 Programming I CSCE 110 Programming I Basics of Python (Part 1): Variables, Expressions, and Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2013 Tiffani

More information

LOOPS. Repetition using the while statement

LOOPS. Repetition using the while statement 1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

More information

The C++ Language. Arizona State University 1

The C++ Language. Arizona State University 1 The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/58 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

cs1114 REVIEW of details test closed laptop period

cs1114 REVIEW of details test closed laptop period python details DOES NOT COVER FUNCTIONS!!! This is a sample of some of the things that you are responsible for do not believe that if you know only the things on this test that they will get an A on any

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Fall 2016 Lab 09 Advanced Debugging CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working

More information

Python Programming Challenges

Python Programming Challenges Python Programming Challenges Name Complete the tasks enclosed and complete a self review for each task. Eg: Yes or no errors? Yes syntax errors (write in the error) or No your errors/solve the problem?

More information

Part III Appendices 165

Part III Appendices 165 Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.

More information

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops PRG PROGRAMMING ESSENTIALS 1 Lecture 2 Program flow, Conditionals, Loops https://cw.fel.cvut.cz/wiki/courses/be5b33prg/start Michal Reinštein Czech Technical University in Prague, Faculty of Electrical

More information

Getting Started Values, Expressions, and Statements CS GMU

Getting Started Values, Expressions, and Statements CS GMU Getting Started Values, Expressions, and Statements CS 112 @ GMU Topics where does code go? values and expressions variables and assignment 2 where does code go? we can use the interactive Python interpreter

More information

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/60 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1] CSCI1101 Final Exam December 18, 2018 Solutions 1. Determine the value and type of each of the expressions below. If the question has two lines, assume that the statement in the first line is executed,

More information

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world! 6.S189 Homework 1 http://web.mit.edu/6.189/www/materials.html What to turn in Do the warm-up problems for Days 1 & 2 on the online tutor. Complete the problems below on your computer and get a checkoff

More information

Starting. Read: Chapter 1, Appendix B from textbook.

Starting. Read: Chapter 1, Appendix B from textbook. Read: Chapter 1, Appendix B from textbook. Starting There are two ways to run your Python program using the interpreter 1 : from the command line or by using IDLE (which also comes with a text editor;

More information

An Introduction to Python for KS4!

An Introduction to Python for KS4! An Introduction to Python for KS4 Python is a modern, typed language - quick to create programs and easily scalable from small, simple programs to those as complex as GoogleApps. IDLE is the editor that

More information

CMSC 201 Computer Science I for Majors

CMSC 201 Computer Science I for Majors CMSC 201 Computer Science I for Majors Lecture 02 Intro to Python Syllabus Last Class We Covered Grading scheme Academic Integrity Policy (Collaboration Policy) Getting Help Office hours Programming Mindset

More information

Python Day 3 11/28/16

Python Day 3 11/28/16 Python Day 3 11/28/16 Objectives Review Concepts Types of Errors Escape sequences String functions Find the Errors bookcost = int(input("how much is the book: ")) discount = float(input("what is the discount:

More information

Chapter 2 Working with Data Types and Operators

Chapter 2 Working with Data Types and Operators JavaScript, Fourth Edition 2-1 Chapter 2 Working with Data Types and Operators At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics

More information

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

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof. GridLang: Grid Based Game Development Language Language Reference Manual Programming Language and Translators - Spring 2017 Prof. Stephen Edwards Akshay Nagpal Dhruv Shekhawat Parth Panchmatia Sagar Damani

More information

More about Loops and Decisions

More about Loops and Decisions More about Loops and Decisions 5 In this chapter, we continue to explore the topic of repetition structures. We will discuss how loops are used in conjunction with the other control structures sequence

More information

L-System Fractal Generator: Language Reference Manual

L-System Fractal Generator: Language Reference Manual L-System Fractal Generator: Language Reference Manual Michael Eng, Jervis Muindi, Timothy Sun Contents 1 Program Definition 3 2 Lexical Conventions 3 2.1 Comments...............................................

More information

Algorithms and Programming I. Lecture#12 Spring 2015

Algorithms and Programming I. Lecture#12 Spring 2015 Algorithms and Programming I Lecture#12 Spring 2015 Think Python How to Think Like a Computer Scientist By :Allen Downey Installing Python Follow the instructions on installing Python and IDLE on your

More information

(Python) Chapter 3: Repetition

(Python) Chapter 3: Repetition (Python) Chapter 3: Repetition 3.1 while loop Motivation Using our current set of tools, repeating a simple statement many times is tedious. The only item we can currently repeat easily is printing the

More information

Key Differences Between Python and Java

Key Differences Between Python and Java Python Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts. Python is designed to be used interpretively.

More information

DaMPL. Language Reference Manual. Henrique Grando

DaMPL. Language Reference Manual. Henrique Grando DaMPL Language Reference Manual Bernardo Abreu Felipe Rocha Henrique Grando Hugo Sousa bd2440 flt2107 hp2409 ha2398 Contents 1. Getting Started... 4 2. Syntax Notations... 4 3. Lexical Conventions... 4

More information

Introduction to Python Programming

Introduction to Python Programming 2 Introduction to Python Programming Objectives To understand a typical Python program-development environment. To write simple computer programs in Python. To use simple input and output statements. To

More information

PYTHON- AN INNOVATION

PYTHON- AN INNOVATION PYTHON- AN INNOVATION As per CBSE curriculum Class 11 Chapter- 2 By- Neha Tyagi PGT (CS) KV 5 Jaipur(II Shift) Jaipur Region Python Introduction In order to provide an input, process it and to receive

More information

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

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts Simple Data Types There are a number of data types that are considered primitive in that they contain only a single value. These data

More information

AN INTRODUCTION PROGRAMMING. Simon Long

AN INTRODUCTION PROGRAMMING. Simon Long AN INTRODUCTION & GUI TO PROGRAMMING Simon Long 2 3 First published in 2019 by Raspberry Pi Trading Ltd, Maurice Wilkes Building, St. John's Innovation Park, Cowley Road, Cambridge, CB4 0DS Publishing

More information

Condition Controlled Loops. Introduction to Programming - Python

Condition Controlled Loops. Introduction to Programming - Python Condition Controlled Loops Introduction to Programming - Python Decision Structures Review Programming Challenge: Review Ask the user for a number from 1 to 7. Tell the user which day of the week was selected!

More information

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

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018 Python Input, output and variables Lecture 23 COMPSCI111/111G SS 2018 1 Today s lecture What is Python? Displaying text on screen using print() Variables Numbers and basic arithmetic Getting input from

More information

1/11/2010 Topic 2: Introduction to Programming 1 1

1/11/2010 Topic 2: Introduction to Programming 1 1 Topic 2: Introduction to Programming g 1 1 Recommended Readings Chapter 2 2 2 Computer Programming Gain necessary knowledge of the problem domain Analyze the problem, breaking it into pieces Repeat as

More information

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMP Primitive and Class Types. Yi Hong May 14, 2015 COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015 Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to

More information

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

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu) I. INTERACTIVE MODE VERSUS SCRIPT MODE There are two ways to use the python interpreter: interactive mode and script mode. 1. Interactive Mode (a) open a terminal shell (terminal emulator in Applications

More information

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid: 1 STRINGS Objectives: How text data is internally represented as a string Accessing individual characters by a positive or negative index String slices Operations on strings: concatenation, comparison,

More information

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below. Name: Date: Instructions: PYTHON - INTRODUCTORY TASKS Open Idle (the program we will be using to write our Python codes). We can use the following code in Python to work out numeracy calculations. Try

More information

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

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

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration STATS 507 Data Analysis in Python Lecture 2: Functions, Conditionals, Recursion and Iteration Functions in Python We ve already seen examples of functions: e.g., type()and print() Function calls take the

More information

3 The Building Blocks: Data Types, Literals, and Variables

3 The Building Blocks: Data Types, Literals, and Variables chapter 3 The Building Blocks: Data Types, Literals, and Variables 3.1 Data Types A program can do many things, including calculations, sorting names, preparing phone lists, displaying images, validating

More information

Downloaded from Chapter 2. Functions

Downloaded from   Chapter 2. Functions Chapter 2 Functions After studying this lesson, students will be able to: Understand and apply the concept of module programming Write functions Identify and invoke appropriate predefined functions Create

More information

Introduction to Programming in Turing. Input, Output, and Variables

Introduction to Programming in Turing. Input, Output, and Variables Introduction to Programming in Turing Input, Output, and Variables The IPO Model The most basic model for a computer system is the Input-Processing-Output (IPO) Model. In order to interact with the computer

More information

At full speed with Python

At full speed with Python At full speed with Python João Ventura v0.1 Contents 1 Introduction 2 2 Installation 3 2.1 Installing on Windows............................ 3 2.2 Installing on macos............................. 5 2.3

More information

Python Games. Session 1 By Declan Fox

Python Games. Session 1 By Declan Fox Python Games Session 1 By Declan Fox Rules General Information Wi-Fi Name: CoderDojo Password: coderdojowireless Website: http://cdathenry.wordpress.com/ Plans for this year Command line interface at first

More information

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage: Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Variables You have to instruct your computer every little thing it needs to do even

More information

Variables, Constants, and Data Types

Variables, Constants, and Data Types Variables, Constants, and Data Types Strings and Escape Characters Primitive Data Types Variables, Initialization, and Assignment Constants Reading for this lecture: Dawson, Chapter 2 http://introcs.cs.princeton.edu/python/12types

More information

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays CS0007: Introduction to Computer Programming Review If the == operator has two array variable operands, what is being compared? The reference variables held in the variables.

More information

Condition Controlled Loops. Introduction to Programming - Python

Condition Controlled Loops. Introduction to Programming - Python + Condition Controlled Loops Introduction to Programming - Python + Repetition Structures n Programmers commonly find that they need to write code that performs the same task over and over again + Example:

More information

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100? 1 CS 105 Review Questions Most of these questions appeared on past exams. 1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100? b.

More information

Programming to Python

Programming to Python Programming to Python Sept., 5 th Slides by M. Stepp, M. Goldstein, M. DiRamio, and S. Shah Compiling and interpreting Many languages require you to compile (translate) your program into a form that the

More information

Basic Syntax - First Program 1

Basic Syntax - First Program 1 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

More information

JME Language Reference Manual

JME Language Reference Manual JME Language Reference Manual 1 Introduction JME (pronounced jay+me) is a lightweight language that allows programmers to easily perform statistic computations on tabular data as part of data analysis.

More information