Introduction to python programming

Slides:



Advertisements
Similar presentations
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Advertisements

Program Design and Development
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
PH2150 Scientific Computing Skills
Chapter 2 Variables.
CNG 140 C Programming (Lecture set 3)
4. Java language basics: Function
Topics Designing a Program Input, Processing, and Output
Python: Experiencing IDLE, writing simple programs
Introduction to Python
Chapter 2 - Introduction to C Programming
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Pamela Moore & Zenia Bahorski
Topics Introduction to Repetition Structures
Java Primer 1: Types, Classes and Operators
Debugging and Random Numbers
Lecture 1 Introduction to Python Programming
Variables, Expressions, and IO
Introduction to Programmng in Python
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
Fundamental of Java Programming Basics of Java Programming
Chapter 2 - Introduction to C Programming
Introduction to Python
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Arithmetic operations, decisions and looping
Introduction to Python
Introduction to Python
Chapter 2 - Introduction to C Programming
An Introduction to Java – Part I, language basics
String and Lists Dr. José M. Reyes Álamo.
Coding Concepts (Data- Types)
Topics Designing a Program Input, Processing, and Output
Python Primer 1: Types and Operators
Topics Designing a Program Input, Processing, and Output
For loops Taken from notes by Dr. Neil Moore
CS150 Introduction to Computer Science 1
Topics Designing a Program Input, Processing, and Output
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
12th Computer Science – Unit 5
And now for something completely different . . .
Primitive Types and Expressions
Javascript Chapter 19 and 20 5/3/2019.
Unit 3: Variables in Java
Chapter 2 Variables.
CprE 185: Intro to Problem Solving (using C)
Just Enough Java 17-May-19.
Class code for pythonroom.com cchsp2cs
Introduction to java Part I By Shenglan Zhang.
Variables and Constants
Introduction to Python
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Learning Python 5th Edition
Introduction to Python
Introduction to Computer Science
Programming for Business Computing Introduction
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to python programming Xing Fang School of Information Technology

About the lecturer A new faculty member at the School of IT. Received my Ph.D. in CS in May 2016. Beijing, China Have been programming since high school. Love Python programming.

Objectives Learn basic Python programming skills. Apply the skills to solve some real world problems.

Programming A cool way to use a computer Programming Languages C C++ Java Python ….

Programming Languages High-level programming languages Requires an “assistant” to run on computers The “assistant” is a compiler or an interpreter (or combined)

Features of Python Python is interpreted. Variables are loosely typed. (Do not need to declare variables.) Indentations are forced to group statements. Python can be learnt interactively.

Python Versions Python 2.7 and Python 3 Minor differences We will use Python 2.7

Get Ready Python download Online Python shell https://www.python.org/downloads/ Online Python shell https://www.python.org/shell/

Python Programming 101 The print function print “Welcome to ISU IT Camp” It is a Python statement. The statement will display the information in between the pair of double quotes. “Welcome to ISU Summer Camp” is a string. Python statements are case sensitive. print is colored in Python shell, so it is a reserved keyword.

PYTHON PROGRAMMING 101 Try the following statements print 3 print “3”

PYTHON PROGRAMMING 101 Variables In simplest terms, a variable is just a box that you can put stuff in. You can use variables to store all kinds of stuff, but for now, we are just going to look at storing numbers in variables.

PYTHON PROGRAMMING 101 Variables and Numbers a = 3 #initialization or definition a is a variable and its value now is 3 = is called the assignment operator a’s value can be changed at any time by assigning a new value to it: a = 1.2 Variables are also case sensitive.

PYTHON PROGRAMMING 101 Variables and Numbers a = 3; b = 4 a = a + b print a/b a += b a *= b a /= b a += c

PYTHON PROGRAMMING 101 Variables and Strings Strings can be enclosed in single quotes or double quotes: “A” or ‘A’ Strings can be assigned to variables: a = “Alice”; b = “Bob” Alice = “Alice” Strings can be concatenated together: b = “B”+”o”+”b” print a+b

PYTHON PROGRAMMING 101 Type Casting Transform an floating point number to an integer: int(3.7) Transform an integer to a floating point number: float(2) Transform a string to a floating point number: float(“3.2”) What is the result of int(float(“3.2”))?

PYTHON PROGRAMMING 101 Lists The most useful data structure Lists can hold different data types together: list_2 = [1,”2”,3.4] Lists can be indexed, concatenated, and sliced

PYTHON PROGRAMMING 101 List Indexing Indices start at 0 List_Name[index] list_2 = [1,”2”,3.4] list_2[0] gives you 1 list_2[2] gives you 3.4 list_2[-1] gives you 3.4

PYTHON PROGRAMMING 101 List Concatenation List Slicing list_1+list_2 sub_list_1 = list_1[:2] sub_list_1 = list_1[:-1] sub_list_1 = list_1[1:2]

PYTHON PROGRAMMING 101 Lists are mutable list_2 = [1,”2”,3.4]

PYTHON PROGRAMMING 101 List as an object A list is an object. An object can use the “.” (dot) operator to call some methods list_1.count(1) list_1.append(5) list_1.remove(5) list_1.pop(0)

PYTHON PROGRAMMING 101 Making decisions using if statement The logic: If something is True, then take the action. Example: a=3;b=2 if (a>b): print “a is larger than b”

PYTHON PROGRAMMING 101 The if else statement The logic If something is true, then take action 1. Otherwise, take action 2. Example: a=3;b=2 if (a>b): print (“a is larger than b”) else: print(“a is less than b”)

SUMMARY FOR DAY 1 Python is interpreted. Python 2.7 Variables, numbers, strings. Lists If/If-else statements

PYTHON PROGRAMMING 101 for loop People use loops to execute a series of repeatable actions for loop involves using the keyword, for Example: list_1 = [1,2,3,4] for number in list_1: print number

PYTHON PROGRAMMING 101 for loop Another way to do it: Another example: for index in range(len(list_1)): print list_1[index] Another example: square = [] for x in range(10): square.append(x**2)

PYTHON PROGRAMMING 101 Algorithms a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.

PYTHON PROGRAMMING 101 Python scripts File extension is .py After the execution, whatever defined in the script will be still there to use.

PYTHON PROGRAMMING 101 Problem solving #1 Deciding the root conditions of quadratic equations

PYTHON PROGRAMMING 101 Quadratic equations

PYTHON PROGRAMMING 101  

PYTHON PROGRAMMING 101 Problem solving #1 The algorithm: The root conditions of quadratic equations The algorithm: Get the value of a, b, and c. See if a is equal to zero, if it is, then return. Otherwise, continue to next step. Compute delta. Making decisions: If delta is larger than or equal to zero, if delta is a negative number.

PYTHON PROGRAMMING 101 Problem solving #2 Find the maximum number of a list [3,2,1,5,4] Computational thinking The Algorithm Hint: You want to use the if statement and the for loop

PYTHON PROGRAMMING 101 Problem solving #2 The algorithm Find the maximum number of a list The algorithm Initialize a variable with a sufficiently small value. Start looping through the list, if the value of the list is larger than the value of the variable, assign the value of the list to the variable.

PYTHON PROGRAMMING 101 Problem solving #2 The algorithm Find the minimum number of a list The algorithm

PYTHON PROGRAMMING 101 while loop a = 0 while (a < 3): print (“Welcome to ISU.”) a += 1

PYTHON PROGRAMMING 101 The Numerical Python Package (Numpy) For scientific computing http://www.numpy.org/

PYTHON PROGRAMMING 101 Import the package The random family import numpy import numpy as np The random family np.random.randint(0,11,10)

PYTHON PROGRAMMING 101 Practice Find the maximum number of a randomly generated list that has 1000 integers

PYTHON PROGRAMMING 101 Numpy.random.choice Numpy.random.shuffle np.random.choice([1,2,3,4],3) np.random.choice([1,2,3,4],3,replace=False) Numpy.random.shuffle np.random.shuffle([1,2,3,4])

PYTHON PROGRAMMING 101 Problem solving #3 Rules: Fill out the blanks using numbers from 1 to 9 The numbers can be used more than once Follow the sequence and the arithmetic precedence

PYTHON PROGRAMMING 101 Solution Try to pick up 9 random numbers and fit each of them in a blank If the computation result matches, then return the 9 numbers as the solution

PYTHON PROGRAMMING 101 Dictionary Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be strings and numbers.

PYTHON PROGRAMMING 101 Dictionary Examples: d = {} d = {“apple”:1,”grape”:5,”orange”:7} key:value Keys are unique d is an object

PYTHON PROGRAMMING 101 Dictionary return a list of keys d.keys() return a list of values d.values() loop through a dictionary d = {1:2,3:4,5:6} for k,v in d.iteritems(): print k,v

PYTHON PROGRAMMING 101 File I/O Read from a file f = open(“C:\\Users\\xfang13\\Desktop\\infor.txt”) f is a file object. for line in f: print line f.close() it is always a good behavior to close the file once you have done with it.

PYTHON PROGRAMMING 101 File I/O Read from a file with open(“C:\\Users\\xfang13\\Desktop\\infor.txt”) as f: for line in f: print line The file will be closed automatically, once the lines are printed.

PYTHON PROGRAMMING 101 File I/O Write to a file with open(“C:\\Users\\xfang13\\Desktop\\infor.txt”,”w”) as f: f.write(“Xing Fang\n”) f.write(“Assistant Professor\n”) f.write(“School of IT\n”) f.write(“Illinois State University”)

PYTHON PROGRAMMING 101 Simple file parsing with open(“C:\\Users\\xfang13\\Desktop\\infor.txt”) as f: for line in f: print line There are some annoying characters, which we want to remove. line = line.strip(‘\n.’)

PYTHON PROGRAMMING 101 Simple file parsing Let’s further extract the words. with open(“C:\\Users\\xfang13\\Desktop\\infor.txt”) as f: for line in f: line = line.strip(‘\n.’) print line.split(“ ”)

PYTHON PROGRAMMING 101 Counting the appearance of a word We will use a dictionary to save the information