Introduction to python

Slides:



Advertisements
Similar presentations
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
Advertisements

Chapter 2 Writing Simple Programs
Python quick start guide
V Avon High School Tech Club Agenda Old Business –Delete Files New Business –Week 18 Topics: Intro to HTML/CSS: Questions? Summer Work Letter.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Python Basics Tom LeFebvre. Sept , 2002Python Basics2 Python Language Very High-Level Language Scripting Language (no compiling) Simple syntax Easy.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Python & NetworkX Youn-Hee Han
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
PYTHON PROGRAMMING LANGUAGE.
Introduction to python programming
Chapter 2 Writing Simple Programs
Scientific Programming in Python -- Cheat Sheet
PH2150 Scientific Computing Skills
Introduction to Python
Introduction to Programming
Module 5 Working with Data
Introduction to Python
Computer Programming Fundamentals
Making Choices with if Statements
Intro to Computer Science CS1510 Dr. Sarah Diesburg
COMPSCI 107 Computer Science Fundamentals
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Intro To Pete Alonzi University of Virginia Library
Python: Control Structures
Introduction to Python
Variables, Expressions, and IO
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Functions CIS 40 – Introduction to Programming in Python
PHP Introduction.
Prepared by Kimberly Sayre and Jinbo Bi
PH2150 Scientific Computing Skills
Python Review.
Introduction to Python
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Coding Concepts (Basics)
More Looping Structures
Suggested Layout ** Designed to be printed on white A3 paper.
Introduction to Programming
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Python Basics Wen-Yen Hsu, Hsiao-Lung Chan Dept Electrical Engineering
Python Basics with Jupyter Notebook
Introduction to Python
File Organization.
COMPUTER PROGRAMMING SKILLS
Hash Maps Implementation and Applications
More Looping Structures
Introduction to Programming
Numpy, pylab, matplotlib (follow-up)
More Basics of Python Common types of data we will work with
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Python – May 20 Reading data from the Web
Learning Python 5th Edition
Presentation transcript:

Introduction to python Group discussion 0 Introduction to python

Why Python? Free and open source Large set of scientific computing libraries Many other libraries when needed Code is readable and easy to write

Running Python in this class Install VirtualBox and downloading image -Link at bottom of Relate page https://relate.cs.illinois.edu/course/cs357-f16/ Running the image File -> Import appliance Run code from Notebook Link on desktop

Introduction to Python Printing print(‘Hello world’) Variables Don’t need to specify type Commenting #for line comments ‘’’Triple single quotes for block comments’’’

If, else if, else blocks Practice it: if stockPrice < targetPrice: print(‘better buy’) elif stockPrice > targetPrice: print(‘better sell’) else print(‘don’t do anything’) Else if -> elif Practice it: Write a program in using if, elif, and else statements that returns a letter grade (A, B, C, D, F) given a variable g with a numerical grade.

For loops and while loops for i in range(0, 10): print(i) time = 0.0 endTime = 10.0 while time < endTime: time += 0.1 print(time) For loops iterate over lists Practice it: Write a program that sums the numbers 1 to 100 and prints the result

Creating Numpy arrays Import numpy as np B = np.array([[0,0,17,31],[0,0,0,5],[0,0,0,0],[0,14,0,0]]) print(B) #Alternative method shape = [4, 4] B = np.zeros(shape) #<---- the dimensions need to be inside a list B[0, 2] = 17 B[0, 3] = 31 B[1, 3] = 5 B[3, 1] = 14

Accessing numpy arrays and matrix multiplication Matrix locations accessed the same way they are set x = B[0, 3] Use @ operator to multiply two matrices of appropriate dimensions together C = A @ B Practice it: Write a program that creates two (2x2) numpy matrices A and B, multiplies them together using @,  and stores the result in C.

Python functions Basic synax Practice it: def functionName(parameter0, parameter1,…): #Do stuff in here… ans = … return ans Example:      def getHello(helloVersion):      if helloVersion == 0:                                 return ‘Hello world’                           else           return ‘Hello Illinois’ Practice it: Write a function that takes two Numpy matrices, A and B, as parameters and returns the sum of their [0, 0] entries.

Matplotlib, math functions, and numpy math functions import matplotlib.pyplot as plt import math import numpy as np x = np.linspace(0, 2*math.pi, 30, endpoint=True) y = np.sin(x) z = np.cos(x) plt.plot(x,y, label='sin(x)') plt.plot(x,z, label='cos(x)') plt.legend(loc=0) plt.xlabel('x') plt.ylabel('y') plt.title('Demo plot') plt.show()

Python dictionaries Data structure of key-values pairs with fast read and write (provided you know the key) Implemented as hash table under the hood d ={'hammer': 5, 'nails': 2, 'drillbit': 3, 'screwdriver': 1, 'bike': 1, 'mattress': 2, 'pillow': 1, 'stuffed animal': 2} #Alternatively we might need to update an existing dictionary warehouseList = ['hammer', 'nails', 'hammer', 'hammer', 'screwdriver', 'drillbit', 'nails', 'mattress', 'stuffed animal', 'drillbit', 'hammer', 'pillow', 'bike', 'hammer', 'mattress', 'drillbit', 'stuffed animal'] d = dict() #empty dictionary for i in warehouseList: #Note that we can iterate directly over a list if(d.get(i) == None): #None is a special value like NULL d.update({i: 1}) else: d.update({i: d.get(i) + 1}) print(d)

Python dictionaries (cont.) Helpful functions d.keys() – returns a list of keys in the dictionary d.values() – returns a list of values in the dictionary d.items() – returns all key-value pairs in the dictionary d.get(key) – returns the value paired with key or None if there is no value paired with key

Nested dictionaries The values of a dictionary can be any object. Since dictionaries are objects we can make them a value inside another dictionary #Test number – score pairs tyreseScores = {1:80, 2:85, 3:90} heinrichScores = {1:90, 2:75, 3:80} kimScores = {1:95, 2:90, 3:60} liScores = {1:83, 2:93, 3:100} #Student – gradebook pairs studentsDict = {'Tyrese':tyreseScores, 'Heinrich':heinrichScores, 'Kim':kimScores,'Li':liScores} print(studentsDict) Practice it: Write code that outputs the score of each student on each test.

End!