Python Programming Exercises 1

Similar documents
ENGR (Socolofsky) Week 02 Python scripts

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

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)

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

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

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Lecture 2: Python Arithmetic

Python for Bioinformatics Fall 2014

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Science One CS : Getting Started

C++ Programming: From Problem Analysis to Program Design, Third Edition

Variables, expressions and statements

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

Expressions and Variables

Introduction to Python

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

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

>>> * *(25**0.16) *10*(25**0.16)

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Python for Non-programmers

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

Fundamentals: Expressions and Assignment

Reserved Words and Identifiers

Lecture 1: Hello, MATLAB!

Built-in Types of Data

Full file at

BASIC ELEMENTS OF A COMPUTER PROGRAM

Outline. Data and Operations. Data Types. Integral Types

Chapter 2 Working with Data Types and Operators

Notes on Chapter 1 Variables and String

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

Algorithms and Programming I. Lecture#12 Spring 2015

Getting Started with Python

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Physics 514 Basic Python Intro

Fundamentals of Programming CS-110. Lecture 2

Visual C# Instructor s Manual Table of Contents

Objectives. In this chapter, you will:

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

CS 102 Lab 3 Fall 2012

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

The Very Basics of the R Interpreter

Chapter 2: Basic Elements of C++

Phys Techniques of Radio Astronomy Part 1: Python Programming

Full file at

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS1110 Lab 1 (Jan 27-28, 2015)

AMS 27L LAB #1 Winter 2009

Introduction to Computer Programming for Non-Majors

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

Introduction to Programming

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

Fundamentals of Programming (Python) Getting Started with Programming

Introduction to programming with Python

Graphics calculator instructions

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

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

The C++ Language. Arizona State University 1

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Introduction to Python Programming

Lecture 3 Tao Wang 1

Lecture 3. Input, Output and Data Types

Programming in Python 3

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

Downloaded from Chapter 2. Functions

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Dr Richard Greenaway

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

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

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

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

cs1114 REVIEW of details test closed laptop period

Python Unit

CS 112: Intro to Comp Prog

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

Exercise: Inventing Language

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

Lecture 1. Course Overview Types & Expressions

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

Elementary Programming

PROGRAMMING FUNDAMENTALS

Com S 127 Lab 2. For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out.

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

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

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

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

Senthil Kumaran S

BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER. Chapter 2

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Transcription:

Python Programming Exercises 1 Notes: throughout these exercises >>> preceeds code that should be typed directly into the Python interpreter. To get the most out of these exercises, don t just follow them to the letter, but experiment and be curious about the results you get. 1. Open a console and create a new directory for these exercises called ex1 (or whatever you want to call it). We will save all of our files from this session in the same directory. You should do this at the start of each practical session. mkdir ex1 cd ex1 Start the Python interpreter. This is what my Mac prints to the console when I start python3: python3 Python 3.4.2 (default, Feb 6 2015, 12:06:24) [GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/apple/clang-421.0.60))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> Everything in this course assumes you are using Python 3! As you can see, I am running version 3.4.2. If you are not running at least Python version 3.4, then either refer to the installation guide or ask one of the demonstrators for help. 2. The Python interpreter can be exited in two different ways: typing Ctrl-D (this is the EOF character) or typing quit() and pressing enter. Try both. 1

3. If you input a value to the Python interpreter, it will echo it to the console. Try a few examples of integers and floating-point numbers both in decimal and scientific notation: 1 >>> 10 2 10 3 >>> 0.001 4 0.001 5 >>> 1e-3 6 0.001 4. All values have a type. In these exercises we will focus on int (integer), float (floating-point) and str (character string) data types. The function type( ) will tell you the type of a value: 1 >>> type(0.001) Try it on a few of the values you used from the previous question. You do not need to type out everything all over again. Use the arrow keys to step through previous lines (up and down arrows) and move the cursor (left and right arrows). 5. Python assumes that numbers without a decimal place (e.g. 17) are integers and numbers with a decimal place (e.g. 17.0 or 17.000001) are floating-point numbers. You can force a variable to be the type you want by being explicit with int( ) and float( ). 1 >>> type(17) 2 >>> type(float(17)) Do the same by explicitly converting a float to an int. When we convert a float to an int, does the number get rounded up or down? 6. Strings are sequences of alphanumeric characters enclosed in double (e.g. hello ) or single (e.g. hello ) quotation marks. Strings containing numbers can be converted to integers and floats using int( ) and float( ) as well: 2

1 >>> "17" 2 >>> type("17") 3 >>> float("17") 4 >>> type(float("17")) Play around with converting strings to ints and floats. This is important because input from the user will often be in the form of a string, but needs to be converted to a numeric type to be used in calculations. 7. Python supports arithmetic operators for addition (+), subtraction (-), multiplication (*) and division (/). In the expression a + b, a and b are refered to as the operands (a fancy word for arguments) of the operator +. Try out some examples using ints with different operators, e.g. 1 >>> 10 * 3 Note that the type returned from division is not the same as the others: 1 >>> 10 / 3 8. Redo the above examples, but this time use different types as operands and observe the types of the returned values, i.e. int + float and float + float. Inspect the result with type( ) if necessary: 1 >>> type(10 * 3.0) 9. Python additionally supports modulus (%), exponentiation (**) and floor division (//). Test these operators out to ensure you understand what they do: 1 >>> 10 % 3 2 >>> 10 ** 3 3 >>> 10 // 3 3

10. Write an expression to calculate the following: x 2 + 3x + 1. Hint: you will need to declare a variable called x first, e.g. 1 >>> x = 5 11. Write an expression to calculate the following: 1 3x 2 +1. 12. You almost know enough to use Python as a scientific calculator. The missing pieces are useful constants like π and e and functions for roots, trigonometry, etc. Mathematical functions are declared in the math module: 1 >>> import math 2 >>> math.pi 3 >>> math.sqrt(16) Declare a variable called radius: 1 >>> radius = 3 and write an expression to calculate the volume of a sphere: 4 3 πr3. 13. Create a new file called helloworld.py and type into it the hello world example from the lecture slides: 1 # example hello world program 2 message = "hello world" 3 print(message) Run the program to ensure you have typed it in correctly: 4

python3 helloworld.py 14. The print function can accept multiple arguments. Change your program to be like the following and run it again: 1 # example hello world program 2 message1 = "hello" 3 message2 = "world" 4 print(message1, message2) Note that spaces are printed between each variable passed to print. 15. The input() function can be used to capture text typed by the user. input() can optionally take a string argument to give the user instructions (normally called a prompt). Take the program above and replace the value of the variable called message2 with a call to input with the argument Enter your name:. When you run the script it should read: python3 helloworld.py Enter your name: Alan hello Alan 16. Take your expression for the volume of a sphere and write a program that prints out the following: The volume of a sphere with radius 1.0 is 4.188790204786390 Change the value of the radius manually and rerun the program. 17. Change the script you just wrote to prompt the user for the radius. 5