MEIN 50010: Python Introduction

Similar documents
MEIN 50010: Python Strings

MEIN 50010: Python Flow Control

Algorithms and Programming I. Lecture#12 Spring 2015

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

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

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

EL2310 Scientific Programming

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

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

CSE 351. Introduction & Course Tools

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

EL2310 Scientific Programming

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

Table of Contents EVALUATION COPY

At full speed with Python

Python Programming Exercises 1

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

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

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

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

Python for Non-programmers

CSCE 110 Programming I

1. The Mac Environment in Sierra Hall 1242

Scripting Languages. Python basics

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

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

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

ENGR 101 Engineering Design Workshop

Getting Started with Python

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Lecture 03 Bits, Bytes and Data Types

Visual C# Instructor s Manual Table of Contents

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

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

COMP s1 Lecture 1

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

The current topic: Python. Announcements. Python. Python

6.096 Introduction to C++ January (IAP) 2009

Physics 514 Basic Python Intro

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

Fundamentals of Programming (Python) Getting Started with Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CSI32 Object-Oriented Programming

Working with Shell Scripting. Daniel Balagué

Full file at

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

1. The Mac Environment in SIE 1222

Computational Physics Operating systems

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Maciej Sobieraj. Lecture 1

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

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

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

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

Types, lists & functions

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Zheng-Liang Lu Java Programming 45 / 79

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

Work relative to other classes

Introduction to Python

Python Unit

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Computer Organization and Assembly Language. Lab Session 3

Professor Peter Cheung EEE, Imperial College

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

Chapter 2 Working with Data Types and Operators

DaMPL. Language Reference Manual. Henrique Grando

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

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Lecture 3. More About C

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

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

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

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

Python for Astronomers. Week 1- Basic Python

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

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

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

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

Full file at

Lecture 1. Types, Expressions, & Variables

Language Reference Manual simplicity

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Topic 1: Introduction

Chapter 1 INTRODUCTION

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

Python Class-Lesson1 Instructor: Yao

Part III Appendices 165

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

CSI Lab 02. Tuesday, January 21st

Program Organization and Comments

CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic. Aaron Stevens 28 March Overview/Questions

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Transcription:

: Python Fabian Sievers Higgins Lab, Conway Institute University College Dublin Wednesday, 2017-10-04

Outline Goals Teach basic programming concepts Apply these concepts using Python Use Python Packages and libraries

What is Computer Programming? Writing software Multistep process to solve a problem Formulate the problem Generate set of steps to solve problem Algorithm Verify Algorithm Correctness, resource requirements Implementation Write code in programming language Testing/Debugging Coverage Distribution/Documentation/Maintenance

Purpose of Programming Generate sequence of instructions that solve a problem or automate a task Excellent practice of critical/logical thinking Modern biology is data intensive Everyday life is IT intensive good skill to have Implement in a programming language C/C++ Java Perl PHP Python and many, many more...

Python High level language, handles grunt work Clean, concise, unambiguous syntax Rapid application development Interpreted language, Open source, Flexible, Scalable Many resources (libraries, tutorials, forums) https://docs.python.org https://stackoverflow.com/questions/tagged/python http://www.diveintopython3.net/ (book) Large community official Google language increasing bioinfo usage.

Interpreted vs Compiled Python (looks like English): print("hello World!") C (needs to be compiled): include <stdio.h> int main(int argc, char *argv[]){ printf("hello World!\n"); return 0; }

Low Level Language Assembly (what the computer likes):.model tiny.data message db Hello, World!.code org 100h start: mov ah,9 mov dx,offset message int 21h ret end start

A Bit about Bits There are 10 kinds of people Those who understand binary And those who don t

What is a Bit? Computer microprocessor (CPU) composed of many switches called bits Two configurations for a switch on or off true or false 0 or 1 Combine/manipulate bits with Boolean algebra and, or, not build up other operations with these

What is a Byte? Usually, 1 Byte = 8 bits Smallest addressable unit of memory Represent integers (base 10) by their powers of 2 0 10 = 00000000 2 0 2 7 +0 2 6 +0 2 5 +0 2 4 +0 2 3 +0 2 2 +0 2 1 +0 2 0 1 10 = 00000001 2 0 128+0 64+0 32+0 16+0 8+0 4+0 2+1 1 2 10 = 00000010 2 0 128+0 64+0 32+0 16+0 8+0 4+1 2+0 1 3 10 = 00000011 2 0 128+0 64+0 32+0 16+0 8+0 4+1 2+1 1. 255 10 = 11111111 2 1 128+1 64+1 32+1 16+1 8+1 4+1 2+1 1 Floating Point Number binary expansion of 0.1 doesn t terminate, same as decimal expansion of 1/3 (0.333...) 0.1 10 = 0/2 + 0/4 + 0/8 + 1/16 + 1/32 + 0/64 + 0/128 + 1/256 + 1/512... 10 = 0.000110011... 2

A Variable is an object that stores data which can change Program can t do anything useful without variables can only do one thing, no flexibility Declare it (name it) name refers to location in memory name can be whatever you like (should be descriptive, reflect scope, conform to local standards) in Python name must begin with letter in Python name cannot be reserved (key) word

Data Types integer: number of sheep = 39 maximum value (depending on platform) 2 31 1 = 2, 147, 483, 647 (32-bit) or 2 63 1 9.2 10 18 (64-bit) float: weight = 75.4 maximum value 1.8 10 308 Boolean: Answer = False complex: phase = 3+2j String: greeting = Hello World! a sequence of characters surrounded by (single, double or triple) quotes Collection of variables List: elements = [ H, He, Li, Be ] Dictionary: GerEng = { Hund : dog, Katze : cat }

Dynamic Typing Python uses dynamic typing model for variables don t need to be declared in advance don t have a type associated with them, values do >>>x = 2 # x refers to an integer >>>x = Hello # now x refers to a string >>>x = True # and now to a Boolean Python uses strong dynamic typing Applying operations to incompatible types is not permitted >>>1 + hello Traceback (most recent call last): File <stdin>, line 1, in <module> TypeError: unsupported operand type(s) for +: int and str

Basic Can t do anything with varibles unless we have operators are tokens which carry out operations on data/variables Assign a value with the assignment operator = a=5 Mathematical operators + - * / ** % // Comparison operators ==!= > < >= <= String operators concatenate +, repeat *, slice [:]

Basic Addition: + >>>2+3 5 Subtraction: - >>>2-3 -1 Multiplication: * (asterisk) >>>2*3 6 Division: / >>>2/3 0.6666666666666666 Exponent: ** >>>2**3 8 >>>2**0.5 1.4142135623730951 Modulo: % (remainder) >>>14%4 2 (14 = 3 * 4 + 2) Integer Division: // >>>14//4 3 (14 = 3 * 4 + 2)

Boolean Expressions Equal to: == (note double-equal sign) >>>2 == 3 False >>>3 == 3 True Not equal to:!= >>>2!= 3 True >>>3!= 3 False Less than: < >>>2 < 3 True >>>3 < 3 False Less than or equal: <= >>>2 <= 3 True >>>3 <= 3 True >>>4 <= 3 False Greater / Greater or equal: > / >=

Boolean Expressions Test multiple conditions with and Only true if both conditions met >>>(1 < 2) and (3 < 4) True >>>(1 < 2) and (3 < 3) False Test multiple conditions with or True if either condition met >>>(1 < 2) or (3 < 3) True >>>(1 > 2) or (3 > 4) False

Operator precedence In mathematics multiplication before addition 1 + 2 3 = 1 + (2 3) = 7 (1 + 2) 3 = 9 Same precedence in Python (and other languages)

A function performs specific task on data stored in variable Main purpose of this course is to learn how to write your own functions Python has built-in functions

abs(x) Return the absolute value of a number bin(x) Convert an integer number to a binary string input([prompt]) Write prompt to standard output, then read a line from input len(s) Return the length (the number of items) of an object open(file, mode) Open file and return file object print(objects, file) Print objects to the text stream file. Sometimes you will see object.method(arg), e.g., Hello World.find( W ) essentially same as function

Counting Computer scientists use zero based counting >>> Hello World.find( H ) 0 Slice index goes from start index up to but not including end index >>> Hello World [1:4] ell >>> Hello World [:4] Hell >>> Hello World [7:] orld Slice from end using negative index >>> Hello World [-1] d

Using Python Python can be used in 2 ways: Interactive Python Write-as-you-go mode, useful for learning and testing small programs Typed statements are evaluated immediately Write and run script files Create script file with list of Python commands Use plain text editor to create/modify script Contents of script are evaluated line by line as script is run

The Python Starting the Python Linux/Unix Open terminal window (gnome, konsole, xterm) and type python3 Mac OSX Open terminal and type python3 Windows From Start Menu search for cmd Type python We are using Python 3 not 2.6/2.7 Line starting with >>> (or...) are printed by interpreter Line not starting with >>> is output

Using Python interactively Exit interpreter with quit() alternatively Mac/Linux: Control-D Windows: Control-Z followed by return

Using Python Second way to use Python Create script file with list of Python commands Use plain text editor to create file Linux: GEdit, Emacs, Vi,... Mac OSX: TextEdit, TextWrangler, Vi,... Windows: Notepad++ Python inbuilt: IDLE Windows: All Programs Python3.X IDLE IDLE displays Python interpreter window to begin script, choose menu File New Window start typing script, when done, menue File Save to execute, choose menu Run Run Module output written to interpreter window

Python + Command Prompt Python scripts can be run from Windows command prompt Select Programs Accessories Command Prompt (or search for cmd) You may need to tell Wndows where to find Python C:\> set path=%path%;c:\python34 Change to directory containing your Python script C:\> cd mein50010 Run script by passing filename as argument C:\mein50010\> python hello.py Mac & Linux work similarly

Interpreted vs Compiled figure Peadar O Gaora lecture GENE 30040 Assembly version Hello World www.codecodex.com/wiki/hello world Python keywords docs.python.org/3/reference/lexical analysis.html Operator precedence docs.python.org/3/reference/expressions.html Built-in functions docs.python.org/3/library/functions.html Truth table en.wikipedia.org/wiki/boolean algebra Colm Ryan COMP50050