Outline  Why should we use Python?  How to start the interpreter on a Mac?  Working with Strings.  Receiving parameters from the command line.  Receiving.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

Introduction to Python
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Structured programming
 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.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Introduction to scripting
Game Programming © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line L.
Chapter 1 Beginnings.
Python Programming Fundamentals
The University of Texas – Pan American
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.
Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Input, Output, and Processing
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CSE 131 Computer Science 1 Module 1: (basics of Java)
Python: Print Damian Gordon. Your first Python program When learning a new computer programming language, the first thing typically taught is how to write.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Conditionals chapter 5
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Strings in Python. Computers store text as strings GATTACA >>> s = "GATTACA" s Each of these are characters.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Variables, Types, Expressions Intro2CS – week 1b 1.
SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:
Input, Output and Variables GCSE Computer Science – Python.
Fundamentals of Programming I Overview of Programming
Basic concepts of C++ Presented by Prof. Satyajit De
C++ First Steps.
Topics Designing a Program Input, Processing, and Output
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Introduction to Python
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Introduction to Python
Variables, Expressions, and IO
Introduction to Scripting
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Introduction to Python
Introduction to Python
An Introduction to Java – Part I, language basics
Introduction to Primitive Data types
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Python Primer 1: Types and Operators
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
General Computer Science for Engineers CISC 106 Lecture 03
Introduction to Primitive Data types
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Outline  Why should we use Python?  How to start the interpreter on a Mac?  Working with Strings.  Receiving parameters from the command line.  Receiving input from the user.  Importing libraries.  Working with math.

Why Python?Why Python?  Python is  easy to learn,  relatively fast,  object-oriented,  strongly typed,  widely used, and  portable.  C is much faster but much harder to use.  Java is about as fast and slightly harder to use.  Perl is slower, is as easy to use, but is not strongly typed.

Getting started on the MacGetting started on the Mac  Start a terminal session.  Type “ python ”  This should start the Python interpreter > python Python (#2, Apr , 16:28:28) [GCC (Red Hat Linux )] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print “ hello, world! ” hello, world!

Working with StringsWorking with Strings  A string in python has a type str. It consists of a collection of characters in a sequence (the order does matter), delimited by single quotes (‘ ‘) or by double quotes (“ “).  “This is a string”  ‘ this is another string’  “x”  Some languages like C consider strings and characters of different types, in Python they are all the same.

Working with StringsWorking with Strings  x = “1” : variable x is assigned a string 1. Note this is not a number it is a string.  You can convert strings to integers or floats using special functions or constructors.  Number= int (x) : this functions casts or converts the string in x to an integer.  Print “Hello Word” : here we print to the default ouput (screen) the string hello world using the reserved function print

Important Note with Variables  Python Tokens: Keywords: You cannot use (are prevented from using) them in a variable name

Python TokensPython Tokens anddelfromnotwhile aselifglobalorwith assertelseifpassyield breakexceptimportprint classexecinraise continuefinallyisreturn defforlambdatry

Working with StringsWorking with Strings  Assign a string to a variable  Hw= “hello world”  hw.title()  hw.upper()  hw.isdigit()  hw.islower()  hw.isupper()

Working with StringsWorking with Strings  Examples:

Working with StringsWorking with Strings  String literals can span multiple lines in several ways. Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line:  hello = "This is a rather long string containing \n\ several lines of text just as you would do in C. \n\ Note that whitespace at the beginning of the line is \ significant.”  print hello

Working with StringsWorking with Strings  Python does not support a character type, to access individual characters we have to view them as “substrings”.  aString = ‘Hello World!’  aString[0]  ‘H’  aString[1:5]  ‘ello ‘  aString[6:]  ‘World!’

Working with StringsWorking with Strings  Memberships:  ‘bc’ in ‘abcd’  True  ‘n’ in ‘abcd’  False  ‘nm’ not in ‘abcd’  True

Concatenation  We can use the concatenation operator + to create new strings from existing ones or from substrings.  ‘Hello’ + ‘World’  ‘HelloWorld’  “Hello” + “ “ + “World”  ‘Hello World’  a=“Welcome to our Class”  b =a[1:3] + ‘ ‘ + a[8]+a[1]+a[5]+a[6]  What would be the output?

Concatenation  Python allows programmers a simpler way to concatenate adjacent strings, this is not the normal way but it’s a “convenient glitch”  foo = "Hello" 'World'  print foo  ‘HelloWorld’

Receiving Integers from the User  To receive an integer from the user you can use the “input” command.  >>> x = input (“Give me a number: “)  Give me a number: 5  >>> type (x) 

Receiving parameters from the Command Line  To get information into a program, we will typically use the command line.  The command line is the text you enter after the word “ python ” when you run a program.  import sys print "hello, world!" print sys.argv[1] print sys.argv[2]  The zeroth argument is the name of the program file.  Arguments larger than zero are subsequent elements of the command line.

Receiving parameters from the Command Line  DEMO: #!/usr/bin/env python import sys #libreria importada print "hola mundo voy a recibir algo de la linea de comandos y es: " print sys.argv[1] print sys.argv[2]

Receiving parameters from the Command Line  To run the program we do the following:  Assuming its saved as: cline.py  Python cline.py param1 param2  How could we run it directly?

Executing Python ScriptsExecuting Python Scripts  To execute a python script you have to do the following: 1.Insert at the top of the script the following: 1.#! /usr/bin/env python 2.Save the script 3.Modify permissions 1.Chmod 700 myscript.py

Executing Python ScriptsExecuting Python Scripts 4.- Run the script: 1../script_name optional_parameters

Receiving parameters from the Command Line  Modify the program cline.py to receive two inputs, number A and Number B and make the program multiply both numbers:

Receiving parameters from the Command Line  Proposed Solution:  We must convert the input received from the command line to integer.  To do so we use a built in function or constructor named: int ()  int (argv[1]) this line will convert the input received as a string to an integer.

Solution  #!/usr/bin/env python  # programa que recibe de la linea de comandos 2 parametros y los multiplica  #hecho con entusiasmo por Ivan Escobar  import sys  print "La multiplicacion de los dos parametros de entrada es: "  a= int(sys.argv[1])  b = int (sys.argv[2])  print a * b

Importing LibrariesImporting Libraries  Many python functions are only available via “ packages ” that must be imported. >>> print log(10) Traceback (most recent call last): File " ", line 1, in ? NameError: name 'log' is not defined >>> import math >>> print math.log(10)

Receiving input from the User The function: raw_input(“Give me a value”)  prints “Give me a value” on the python screen and waits till the user types something (anything), ending with Enter  Warning, it returns a string (sequence of characters), no matter what is given, even a number (‘1’ is not the same as 1, different types)

Working with MathWorking with Math import math radiusString = raw_input("Enter the radius of your circle:") radiusFloat = float(radiusString) circumference = 2 * math.pi * radiusFloat area = math.pi * radiusFloat * radiusFloat print print "The cirumference of your circle is:",circumference,\ ", and the area is:",area