Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Variables and References in Python. "TAGAGAATTCTA” Objects s Names References >>> s = “TAGAGAATTCTA” >>>
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Types and Arithmetic Operators
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Recitation 1 Programming for Engineers in Python.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
Input, Output, and Processing
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Variables and Expressions CMSC 201 Chang (rev )
Functions Chapter 4 Python for Informatics: Exploring Information
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Variables, Expressions, and Statements
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Python Conditionals chapter 5
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Fundamentals of Programming I Overview of Programming
Catapult Python Programming Wednesday Morning session
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Building Java Programs
2.5 Another Java Application: Adding Integers
Lecture 1 Introduction to Python Programming
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Variables, Expressions, and IO
Computer Programming Methodology Introduction to Java
Introduction to C++ October 2, 2017.
Variables, Expressions, and Statements
Building Java Programs Chapter 2
Unit 2 Programming.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Introduction to C++ Programming
Lecture 2 Python Programming & Data Types
Chapter 2: Basic Elements of Java
Building Java Programs
CS 100: Roadmap to Computing
Margaret Derrington KCL Easter 2014
Variables, Expressions, and Statements
Lecture 2 Python Programming & Data Types
Building Java Programs Chapter 2
CS150 Introduction to Computer Science 1
Variables, Expressions, and Statements
Building Java Programs
Building Java Programs
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Building Java Programs Chapter 2
General Computer Science for Engineers CISC 106 Lecture 03
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Text Copyright (c) 2017 by Dr. E. Horvath
Building Java Programs
CS 100: Roadmap to Computing
Presentation transcript:

Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Python

Basics A simple interpreted language

PythonBasics A simple interpreted language no separate compilation step

PythonBasics $ python >>> A simple interpreted language no separate compilation step

PythonBasics $ python >>> print >>> A simple interpreted language no separate compilation step

PythonBasics $ python >>> print >>> print 'charles' + 'darwin' charlesdarwin >>> A simple interpreted language no separate compilation step

PythonBasics Put commands in a file and execute that

PythonBasics Put commands in a file and execute that $ nano very-simple.py

PythonBasics Put commands in a file and execute that $ nano very-simple.py print print 'charles' + 'darwin'

PythonBasics Put commands in a file and execute that $ nano very-simple.py print print 'charles' + 'darwin' $ python very-simple.py 3 charlesdarwin $

PythonBasics Use an integrated development environment (IDE)

PythonBasics Use an integrated development environment (IDE) Source file

PythonBasics Use an integrated development environment (IDE) Source file Execution shell

PythonBasics Variables are names for values

PythonBasics Variables are names for values Created by use

PythonBasics Variables are names for values Created by use: no declaration necessary

PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>>

PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>>

PythonBasics Variables are names for values Created by use: no declaration necessary variablevalue planet 'Pluto' >>> planet = 'Pluto' >>> print planet Pluto >>>

PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> variablevalue planet moon 'Pluto' 'Charon'

PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> variablevalue planet moon 'Pluto' 'Charon'

PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> variablevalue planet moon p 'Pluto' 'Charon'

PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> print p Pluto >>> variablevalue planet moon p 'Pluto' 'Charon'

PythonBasics A variable is just a name

PythonBasics A variable is just a name Does not have a type

PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>>

PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet 'Pluto' variablevalue string

PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue integer

PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected

PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected If nothing refers to data any longer, it can be recycled

PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected If nothing refers to data any longer, it can be recycled

PythonBasics Must assign value to variable before using it

PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>>

PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling

PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>

PythonBasics Must assign value to variable before using it Python does not assume default values for variables >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>

PythonBasics Must assign value to variable before using it Python does not assume default values for variables Doing so can mask many errors >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>

PythonBasics Must assign value to variable before using it Python does not assume default values for variables Doing so can mask many errors Anything from # to the end of the line is a comment >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>

PythonBasics Values do have types

PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>>

PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>>

PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>> Would probably be safe here to produce 'two3'

PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>> Would probably be safe here to produce 'two3' But then what should '2'+'3' be?

PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>> Would probably be safe here to produce 'two3' But then what should '2'+'3' be? Doing too much is as bad as doing too little…

PythonBasics Use functions to convert between types

PythonBasics Use functions to convert between types >>> print int('2') >>>

PythonBasics Use functions to convert between types >>> print int('2') >>> print 2 + str(3) 23 >>>

PythonBasics Numbers

PythonBasics Numbers bit integer (on most machines)

PythonBasics Numbers bit integer (on most machines) bit float (ditto)

PythonBasics Numbers bit integer (on most machines) bit float (ditto) 1+4j complex number (two 64-bit floats)

PythonBasics Numbers bit integer (on most machines) bit float (ditto) 1+4j complex number (two 64-bit floats) x.real, x.imagreal and imaginary parts of complex number

PythonBasics Arithmetic

PythonBasics Arithmetic Addition

PythonBasics Arithmetic Addition 'Py' + 'thon''Python'

PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction

PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26

PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy'

PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / 21.5

PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / / 21

PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / / 21 Exponentiation**2 **

PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / / 21 Exponentiation**2 ** Remainder%13 % 53

PythonBasics Prefer in-place forms of binary operators

PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>>

PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>>

PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> The same as years = years + 1

PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>>

PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>>

PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>> The same as years = years % 10

PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>> print years 5 >>>

PythonBasics Comparisons

PythonBasics Comparisons 3 < 5True

PythonBasics Comparisons 3 < 5True 3 != 5True

PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False

PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False Single = is assignment Double == is equality

PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False

PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True

PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True 1 3True But please don't do this

PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True 1 3True 3+2j < 5error

October 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.