Introduction to computers and Python. Matthieu Choplin

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

Introduction to programming with Python

CSCI 121: Anatomy of a Python Script

CSI Lab 02. Tuesday, January 21st

Python Input, output and variables

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

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

An Introduction to Python (TEJ3M & TEJ4M)

Welcome to Python 3. Some history

Python Unit

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

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

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

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Introduction to Programming

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Scripting Languages. Diana Trandabăț

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

Introduction to Computer Programming for Non-Majors CSC 2301, Fall The Department of Computer Science

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

from scratch A primer for scientists working with Next-Generation- Sequencing data Chapter 1 Text output and manipulation

Computer Components. Software{ User Programs. Operating System. Hardware

Lecture 1. basic Python programs, defining functions

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

Introduction to Computer Programming for Non-Majors

Table of Contents EVALUATION COPY

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

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

Getting Started with Python

Variables, expressions and statements

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

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

Python for Non-programmers

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

CPS122 Lecture: From Python to Java

Programming Environments

Module 1: Introduction to Computers, Programs, and Java

Programming in Python 3

Introduction to Programming

Lecture 4. Defining Functions

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

Fundamentals of Programming (Python) Getting Started with Programming

[301] Bits and Memory. Tyler Caraza-Harter

Not-So-Mini-Lecture 6. Modules & Scripts

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

Fundamentals: Expressions and Assignment

Winter 2019 CISC101 1/17/2019

An Introduction to Python

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

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

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

Lecture 3. Input, Output and Data Types

CS1110 Lab 1 (Jan 27-28, 2015)

Python Programming Exercises 1

Programming for Engineers in Python. Recitation 1

R programming I expressions, variables, and data types. Luigi Cerulo University of Sannio

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Writing and Running Programs

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

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Introduction to Java Applications

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

Algorithms and Programming I. Lecture#12 Spring 2015

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Python language: Basics

Mobile App:IT. Methods & Classes

Programming with Python

C H A P T E R 1. Introduction to Computers and Programming

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

Downloaded from Chapter 2. Functions

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CSCE 110 Programming I

Programming for Engineers in Python. Autumn

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

CS 111X - Fall Test 1

COMP 110 Project 1 Programming Project Warm-Up Exercise

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

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

Introduction to Scientific Computing

CS112 Lecture: Primitive Types, Operators, Strings

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

ENGR 102 Engineering Lab I - Computation

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

Topic 2: Introduction to Programming

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Computer Principles and Components 1

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

Server side basics CS380

Variables and Constants

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

CS1114 Spring 2015 Test ONE ANSWER KEY. page 1 of 8 pages (counting the cover sheet)

Introduction to Python

A First Program - Greeting.cpp

Unit E Step-by-Step: Programming with Python

Expressions and Variables

Transcription:

Introduction to computers and Python Matthieu Choplin matthieu.choplin@city.ac.uk http://moodle.city.ac.uk/ 1

Objectives To get a brief overview of what Python is To understand computer basics and programs To understand what statement, variable and expressions are To explain the differences between syntax errors, runtime errors, and logic errors. 2

Python's popularity Python has been amongst the top 10 programming languages for more than a decade according to the Tiobe index 3

Who uses Python? 4

Before going more into details... What is a computer? (1/2) An electronic device that is receiving data input, storing (in RAM) and processing (in the CPU) them and producing information in output. 5

What is a computer? (2/2) 6

What is a program? Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages. 7

Different types of Programming Languages Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code. The programs in machine language are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010 The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that multiply two number: area = 5 * 5 8

Computer works using binary logic. It is extremely difficult for humans to program in binary. Computer languages have to be translated to binary logic for the computer to understand. Two types of translation: Compilation Interpretation 9

Compilation (1/2) The compiler translates the entire source program into a program for the target machine (object code). The object code is then loaded onto the target machine and executed. Translation and execution are separate activities. 10

Compilation (2/2) Advantages: Programs are only translated once, and the execution can be remote from the target machine. Execution is fast, because it is not interleaved with translation. The source program text does not have to be available. Disadvantages: Run-time checks are more difficult, and are usually not performed. Compilers tend to be large complex programs. 11

Interpretation (1/2) A software that translates each statement of a source program and executes it to the target machine's language. Cycle of actions: Read one statement from the source program. Translate it into one or more statements in the target machine's language. Execute those statements on the target machine. Translation and execution are interleaved. 12

Interpretation (2/2) Advantages: At run-time, the interpreter knows the current situation on the target machine, and it is therefore easier to perform runtime checks. Interpreters are usually small programs. Disadvantages: The interleaving of translation and execution means that programs are translated each time they are executed, and execution is therefore slow. The source program text has to be made available. 13

Comparison of interpreted vs compiled 14

Python Syntax Statement Variable Expression Indentation Comments 15

Python Syntax - Statement A statement represents an action or a sequence of actions. It does something. To display the greeting "Welcome to Python", we use the print statement: print("welcome to Python") 16

Python Syntax - Expression It represents something, like a number or a string. Expressions are nothing but values, except they can have operations like addition or subtraction. 1 # is an expression 2 + 3 # is also an expression "hello" # as well 17

Python Syntax - Variable(1/3) It is a space created in memory (in RAM) where we can temporarily store values or data. We use the sign '=' for assigning a value to a variable. You can think of it like a box. For example, we store the value (or expression) 1 in the box (i.e the variable) a. Notice that we do not specify the type of the variable, python sees it automatically. This is what we call the "duck typing" or "dynamic typing" a = 1 18

Python Syntax - Variable(2/3) You can use the type builtin function to determine the type of a value or variable 19

Python Syntax - Variable (3/3) a = 1 The variable has a name so that we can reuse it. When we use a variable, it is for retrieving the value that it is holding. Remix 20

Python Syntax - Indentation The indentation is the increase or decrease of space between the left margin and the first character of the line. The code need to be properly indented, else python will raise an error. For example, what is wrong here? Remix 21

Python Syntax - Comments If you want to comment a line, you can use the # (pound sign) that you place before the commented line You can also comment multiple lines using ''' (triple quote) before and after the commented paragraph Example Remix main.py 1 2 3 4 5 6 7 8 print("this line is not commented") # print("this line will not do anythin") ''' print("nothing to see here") print("""this whole block is commented. And it will not show anything when we run the program""") ''' 22

The IDE (1/2) We are going to familiarize ourselves with Pycharm. Pycharm is an IDE (Integrated Development Environment), it helps you to write code with syntax highlighting, auto-completion and a lot of other features. Open the python terminal from Pycharm by going to: View > Tool Windows > Python Console The python terminal is useful for experimenting python code directly, without to run or "launch" a script 23

The IDE (2/2) To run a python script Select the project that you have just created and do a right click and select "New" and "Python file" Notice the extension of the file that you have created, it should be.py Write print("hello World") in the file You can then run the file as a python script: in the menu at the top you click on Run and Run 24

Experimenting with the python shell in the IDE You can do simple arithmetic operations x = 1 radius = 1.0 # Assign 1 to variable x # Assign 1.0 to variable radius # Assign the value of the expression to x y = 5 * (3 / 2) + 3 * 2 x = y + 1 # Assign the addition of y and 1 to x area = radius * radius * 3.14159 # Compute area 25

Exercise 1: run the code as a python script After having experimented in the python shell (or python interpreter): Take the previous script and put it in a NEW python file. Run the script 26

Exercise 2: Add a python file to the project Download the python file with this link (right click and save as) Run the script in Pycharm Change the value of the variable to create your email at city (with the extension @city.ac.uk) Can you explain what is the operator "+" for a string? 27

The different type of errors (1/3) Syntax Error For example, when we forget a quote to close a string print("welcome to Python) 28

The different type of errors (2/3) Runtime Error For example a division by zero (which is impossible) print(1/0) 29

The different type of errors (3/3) Logic error When a program does not perform the way it was intended to. For instance, a wrong formulae for converting pound in kg pounds = float(input("enter weight in pound: ")) # convert pound in kilogramme kilograms = pounds / 0.454 It should be: pounds = float(input("enter weight in pound: ")) # convert pound in kilogramme 30

Exercise 3: Write a program that converts pounds into euros. The values can be hard coded for now (it means that the program will not be dynamic) Use comments Use variables Use print 31

The input function myname = input() The input() function waits for the user to type some text on the keyboard and press ENTER. 32

Exercise 4: Write a program that ask the user what amount is to be converted in euros, convert it and display the result. Hint: we are going to need the function input and the function float 33

Some simple Data Types in Python Numeric: int, float String: str 34

Introspection Built in functions that enables to introspect your code help() dir() 35

Exercise 5 Make a word that a user input in UPPER CASE, i.e. all the letters of the word should be in capital Use the input function. Put the word in a variable. Use a built in function (the help function) to find the method for that. Use print 36