A Python Tour: Just a Brief Introduction

Slides:



Advertisements
Similar presentations
An Introduction to Programming By :- Vishal Hirani B.Tech II year (CSE)
Advertisements

Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
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.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Adapted from John Zelle’s Book Slides1 CS177 Python Programming Chapter 1 Computers and Programs.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Python From the book “Think Python”
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Unit 1 Basic Python programs, functions Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
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.
CSC 1010 Programming for All Lecture 2 Introduction to Python Some material based on material from Marty Stepp, Instructor, University of Washington.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
A Python Tour: Just a Brief Introduction "The only way to learn a new programming language is by writing programs in it." -- B. Kernighan and D. Ritchie.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Introduction to Computer Programming using Fortran 77.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Python Programming Module 1 Computers and Programs Python Programming, 2/e1.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
And now for something completely different…
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to Programming
Development Environment
UMBC CMSC 104 – Section 01, Fall 2016
Python: Experiencing IDLE, writing simple programs
Topics Introduction Hardware and Software How Computers Store Data
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
Lesson 1 An Introduction
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to Programming
Introduction to Programming
basic Python programs, defining functions
Introduction to Programming
Iterations Programming Condition Controlled Loops (WHILE Loop)
TRANSLATORS AND IDEs Key Revision Points.
Do you know this browser?...
CS190/295 Programming in Python for Life Sciences: Lecture 1
Hello World 2 What does all that mean?.
Introduction to Python
basic Python programs, defining functions
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Programming
A look at Python Programming Language 2018.
CS 1111 Introduction to Programming Spring 2019
CISC101 Reminders All assignments are now posted.
A LESSON IN LOOPING What is a loop?
12th Computer Science – Unit 5
Chapter 1: Programming Basics, Python History and Program Components
General Computer Science for Engineers CISC 106 Lecture 03
1.3.7 High- and low-level languages and their translators
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

A Python Tour: Just a Brief Introduction "The only way to learn a new programming language is by writing programs in it." -- B. Kernighan and D. Ritchie "Computers are good at following instructions, but not at reading your mind." -- D. Knuth Program: n. A magic spell cast over a computer allowing it to turn one's input into error messages. tr. v. To engage in a pastime similar to banging one's head against a wall, but with fewer opportunities for reward.

Useful Definitions Programming Language: A language that is designed to express instructions that can be carried out by a computer Program: A detailed set of instructions – written in a programming language – for a computer to perform Program Execution ("running" a program): The computer performs the program's instructions Machine language: The low-level language of a computer that consists of each computation's encoding as a sequence of 0s and 1s. In machine language, a program could look like this: 0110111110101110 1000010111010111 0000111101010111 ... A sequence of 0s and 1s (called bits) corresponds to operations such as Add, Store, Get Instruction, etc. Each computer (CPU) has its own machine language.

What is Python? A high-level language developed by Guido van Rossum in the Netherlands in the late 1980s. Released in 1991. Named after Monty Python Twice received recognition as the language with the largest growth in popularity for the year (2007, 2010) Clean, concise syntax Easy to learn Can create powerful programs quickly Design is compact Language constructs are easy to learn Easy to remember

Python is interpreted: How Does Python Work? Python is interpreted: Each command is translated to machine language and executed "on the fly" The other type is compiled: All commands are translated into machine language first The resulting program is stored in a new file Then the machine language program is executed

How Python Works Python uses an interpreter, which is a program that translates each command in your program into machine language and executes it. Here's what you do: Type your source code (Python) in a file (that ends in extension .py) Invoke the python interpreter (either from the command line or using IDLE) to translate your instructions into machine language and execute them Or you may also use Python interactively, and enter one command at a time: >> python Python 3.2.4 Type "help", "copyright", "credits" or "license" for more information >>>> print("hello, cosc 1301!") hello, cosc1301! >>>> for i in range(3): ... print(i) ... 1 2

Comments in a Python Program Explanations of code purpose in English Indicated by the # character Ignored by the interpreter The style guide for this class requires comments: At the beginning of a module At the beginning of a function Before a chunk of related statements that carry out some unified action # python program example def main(): # Say hello print("Hello!") main() # Execute the main function Question: What output do you expect from the above Python program? "Hello" Hello Say hello

Repetition in Python In programs, we may want the computer to repeat an instruction or set of instructions more than once To do that, we use loops, also known as repetition structures Examples with a loop: for i in [1, 2, 3]: print(i) # indentation is important for i in range(3): print(i) Output: 1 2 3 Output: 1 2

Decisions in Python We may want the computer to perform certain actions only under certain conditions. Test for the condition (to see if it's true) If condition is met, perform the action If condition is not met, skip the action Example: i = 5 if i < 10: print(i, "is less than 10")

Expected Output What is the output? 1 B. 2 C. i 2 4 i 3 6 i def main(): for i in [2, 4, 6]: print(i) main() What is the output? 1 B. 2 C. i 2 4 i 3 6 i

Python Program Structure A source file may contain function definitions, which are a collection of Python statements that will be executed together when the function is invoked. Function definitions start like this: def myFunctionName() : Statements that are part of the function (the loop body) must be indented(say 4 spaces) underneath the above line – this indentation tells the Python interpreter which statements are part of your function. Functions are not executed until you call them. It is common to include a function called main which is executed first A Python program can contain statements which are not part of any function. A comment is included after a # sign and is ignored by the Python interpreter.

An Example: Some Source Code in Python def main(): print("hello") print("world") print("hello", "world") myNum = 13 print(myNum * myNum) main() # call the main function Output: hello world hello world 169

Python Tour: Things to Remember Whitespace (blank spaces) matters Statements inside functions/loops/conditionals must be indented Tells interpreter which statements belong Two spaces too few, six probably too many – be consistent IDLE indents automatically Common to include a main function