Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Structured programming
Chapter 2 Writing Simple Programs
Introduction to JavaScript for Python Programmers
Recitation 1 Programming for Engineers in Python.
Python Control of Flow.
Python quick start guide
Python.
Python Programming Fundamentals
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Introduction to Python
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Functions Reading/writing files Catching exceptions
Computer Science 101 Introduction to Programming.
If statements while loop for loop
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
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.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
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.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Chapter 2 Writing Simple Programs
Control Flow (Python) Dr. José M. Reyes Álamo.
Python Review 1.
Introduction to Programming
Introduction to Python
Introduction to 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.
Topics Introduction to Repetition Structures
Introduction to Programming
Introduction to Programming
Arithmetic operations, decisions and looping
Introduction to Python
Introduction to Programming
Python Primer 2: Functions and Control Flow
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
An Introduction to Python
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
G. Pullaiah College of Engineering and Technology
CISC101 Reminders All assignments are now posted.
COMPUTER PROGRAMMING SKILLS
Introduction to Programming
Introduction to Programming
Introduction to Programming
Class code for pythonroom.com cchsp2cs
COMPUTING.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Python Basics of the Language

Install Python Find the most recent distribution for your computer at: However, for this class, download and execute Python MSI installer for your platform – For version 2.7 This version works with your Python textbook! – You may need to be logged in as administrator to your computer to run the installation

Modes of Interaction with Python You can interact with the Python interpreter through two built-in modes: – Basic (command line) – IDLE (may need to download other software!) Command line mode: – You can find and double click the Python.exe executable (e.g., python27) in the downloaded folder – Make sure to right click it, and then create a shortcut to it, and pin it to the taskbar! – Click the icon on your taskbar to start the command line You will get the >>> Python prompt Type exit at the command prompt to get out of it! >>> exit//or ctrl z – Note: you can use the Home, Pg up, Pg dn, and End keys, to scroll through previous entries, and repeat them by pressing the Enter key!

Basic Command Line

IDLE This is the integrated development environment for Python Involves interactive interpreter with editing and debugging capabilities You can find and double click the Pythonw.exe executable (e.g., python27) in the downloaded folder Pin it to the taskbar

Python IDLE Shell Window The IDLE (Python shell window) is more convenient than the basic python command line Provides automatic indentation and highlights the code Can use the Home, Pg up, Pg dn, and End keys to search the buffer in your session

Learn Basic parts of Python Like other languages, Python has many data types – These can be manipulated with operators, functions, and methods We can also make our own types (classes) and instantiate and manipulate them with operators, functions, and our own methods It also has conditional and iterative control, e.g.: – if-elif-else – while loop – for loop

Python Naming Style Function name is lower case def add ():#adds numbers; returns the result; ends with : Variable name is lower case (are case sensetive) inputFieldName fc#holds a feature class as in: fc=“Roads.shp” Note: variables Road and road are different! Class name is UpperCamelCase Class StrikeSlipFault Constant is all caps, e.g. PI, SPREADING_RATE Indentation: 4 spaces per level, do not use tabs – IDLE does it for you!

Data types - Practice 10/3 = 3, but 10/3.0 = The // operator is for integer division (quotient without remainder). 10//3 = 3 and 4//1.5 = 2, and 13//4 = 3

Use Built in functions

Strings

Strings …

Formatting strings with the string modulus operator %

Booleans

List x [:-2] -> [‘hydroxide’, ‘phosphate’, ‘carbonate’] # up to, but not including index -2, i.e., gets rid of the last two x [2:] -> [‘carbonate’, ‘oxide’, ‘silicate’] # values from position 2 to the end (inclusive)

List Index PYTHON lang = [‘PYTHON’]# the whole ‘lang’ list lang [-2] = ‘O’# at -2 lang [2:5] = ‘THO’# between 2 and 5, not including 5 lang [:3] = ‘PYT’# up to 3, not including 3 lang [-2:-4] = ‘TH’# from -4 and -2 lang [-3:] = ‘HON’# from -3 to the end

Lists have mixed types Lists are like Java arrays

List’s Built-in Functions

Sets: unordered collection of objects

Sets are unordered collection of objects and can be changed Duplicates are removed, even when you add them The ‘in’ keyword checks for membership in the set Sets

if-elif-else >>> if condition1:# use colon after any statement! body1# do this if condition is true. Notice the indentation elif condition2:# notice the colon body2# otherwise do this …# put more elifs if needed else condition: bodyn# otherwise, if no condition was true except this, do bodyn

The While Loop >>> while (condition):# NOTE: condition expression evaluates to true or false body# If condition evaluates to true (x<y in the example), it will execute else:# optional, rarely used! post-code# If condition evaluates to false, post-code will execute Note: When typing in the IDLE, press enter after (x=x+2) (while the statement is highlighted), and then press backspace key to go to the beginning of line, then press enter to execute the loop!

While loop … The r and n are said to be local variables because they are declared inside the function. If you want them to be available to other functions outside, we can declare them to be global, for example: global r = 1

The For Loop It iterates in order over the items in the iterable sequence (e.g., list, tuple, string) for item in sequence: # starts from the first element of the sequence body # if there is an item, body will execute; it goes to the second item else: # is optional (rarely used as in while loop!) post-code

The For Loop The loop searches the list one item at a time. For each item in the list it checks to see if it is an integer, if not an integer, it skips it, and then checks to see if it divides by eight without a remainder, if it divides by 8, then it prints the message and gets out of the loop!

Loop with specific indices

Function Definition def function_name (arg1, arg2, …)# arg means argument or parameter! body # Sometimes we pass a known value for one of the arguments: def function_name (arg1, arg2=default2, arg3) # e.g., power (x, y=3) body

Function …

input () function

Built in Numeric Functions abs, divmod, float, hex, long, max, min, oct, pow, round # there are a lot more for trigonometry, etc. ‘none’ is a special data type for empty value – e.g., when a function does not return a value – It is used as a placeholder