Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University 2009-2010.

Slides:



Advertisements
Similar presentations
Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CMT Programming Software Applications
Python November 14, Unit 7. Python Hello world, in class.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
JavaScript, Fourth Edition
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
JavaScript, Third Edition
Introduction to C Programming
Hello, world! Dissect HelloWorld.java Compile it Run it.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Python.
Programming for Linguists An Introduction to Python.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Programming for Linguists An Introduction to Python.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
PhD, Senior Lecturer, Baimuratov Olimzhon A LGORITHMS & P ROGRAMMING (P YTHON ) Lecture 2 From SDU:
Input, Output, and Processing
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Computer Science 101 Introduction to Programming.
Variables and Expressions CMSC 201 Chang (rev )
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
Variables, Expressions and Statements
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
A Sample Program #include using namespace std; int main(void) { cout
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
C++ First Steps.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
BASIC ELEMENTS OF A COMPUTER PROGRAM
Completing the Problem-Solving Process
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Variables, Data Types & Math
elementary programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
General Computer Science for Engineers CISC 106 Lecture 03
Presentation transcript:

Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University

The Python Programming Language High-level language (like C, C++, Perl, Java, etc.) Interpreted language Python programs executed by an interpreter Two ways to use the interpreter command-line mode script mode

The Python Programming Language In Command-line Mode type Python programs and the interpreter prints the result The first line of this example is the command that starts the Python interpreter. The next two lines are messages from the interpreter The third line starts with >>>, prompt the interpreter uses to indicate it is ready Type print 1+1 program, and interpreter replied 2

The Python Programming Language In Script Mode write a program in a file and use the interpreter to execute the contents of the file. The file is called a script file name ends with “.py” Ex: use text editor to create a file named “ hello.py”. Then tell interpreter the name of the script $ python hello.py

First Program: “Hello World” in the Python print "Hello, World!" in the C #include int main(void) { printf("hello, world\n"); return 0; } in the C++ #include void main(){ cout << "Hello, world." << endl; } in the Java public class HelloWorld{ public static void main(String args[]){ System.out.println(“Hello, World!”); }

Values and Types A value is one of the fundamental things— like a letter or a number—that a program manipulates Ex: 2 “Hello, World!” These values are belongs to different types 2: integer “Hello, World!”: string

Values and Types If you are not sure what type a value has, the interpreter can tell you: >>> type(’Hello, World!’) >>> type(17) >>> type(3.2) str ->String, int ->Integer, float ->floating-point

Exercise 1 What about values like ’17’ and ’3.2’? >>> type(’17’) >>> type(’3.2’) They’re strings.

Exercise 2 What is the output? >>> print 1,000, ? NO ? YES a semantic error: the code runs without producing an error message, but it doesn’t do the “right” thing.

Variables A variable is a name that refers to a value The assignment statement creates new variables and gives them values >>> message = ’Hello, World!’ >>> n = 17 >>> pi =

Variables (cont.) The print statement also works with variables >>> print message Hello, World! >>> print n 17 >>> print pi

Variables (cont.) Variables also have types again, we can ask the interpreter what they are >>> type(message) >>> type(n) >>> type(pi) The type of a variable is the type of the value it refers to.

Variable names and keywords choose meaningful names both letters and numbers, but begin with a letter Message and message are different (use lowercase by convention) use underscore character (_) in names with multiple words person_name

Variable names and keywords If you give a variable an illegal name, you get a syntax error: >>> 76tables = ‘seventy six tables’ SyntaxError: invalid syntax >>> more$ = SyntaxError: invalid syntax >>> class = ’COMPE 111’ SyntaxError: invalid syntax 76trombones is illegal because it does not begin with a letter. more$ is illegal because it contains an illegal character, the dollar sign But what’s wrong with class ? It turns out that class is one of the Python keywords.

Variable names and keywords Keywords define the language’s rules and structure Keywords cannot be used as variable names Python has twenty-nine keywords:

Statements A statement is an instruction that the Python interpreter can execute print and assignment The result of a print statement is a value. Assignment statements don’t produce a result. A script usually contains a sequence of statements. Ex: the script print 1 x = 2 print x produces the output 1 2

Operators and Operands Operators are special symbols that represent computations like addition and multiplication The values the operator uses are called operands hour-1 hour*60+minute minute/60 5**2 (5+9)*(15-7) The symbols +, -, and /, and the use of parenthesis for grouping, mean in Python what they mean in mathematics The asterisk (*) is the symbol for multiplication ** is the symbol for exponentiation % modulo

Operators and Operands(cont.) When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed Addition, subtraction, multiplication, and exponentiation all do what you expect, but division! >>> minute = 59 >>> minute/60 0 >>> float(minute)/ The value of minute is 59, and in conventional arithmetic 59 divided by 60 is , not 0. The reason for the discrepancy is that Python is performing integer division. use floating-point division

Order of Operations When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. Python follows the same precedence rules for its mathematical operators that mathematics does. The acronym PEMDAS is a useful way to remember the order of operations:

Order of Operations PEMDAS Parentheses have the highest precedence 2 * (3-1) is 4, and (1+1)**(5-2) is 8 Exponentiation has the next highest precedence, 2**1+1 is 3 and not 4, and 3*1**3 is 3 and not 27 Multiplication and Division have the same precedence, which is higher than Addition and Subtraction 2*3-1yields 5 rather than 4, and 2/3-1 is -1, not 1 Operators with the same precedence are evaluated from left to right. 6*100/60 yields 10

Operations on Strings In general, you cannot perform mathematical operations on strings, even if the strings look like numbers The following are illegal: message-1 ’Hello’/123 message*’Hello’ ’15’+2 + operator work with strings. It does concatenation, means joining the two operands by linking them end-to-end fruit = ’banana’ bakedGood = ’ nut bread’ print fruit + bakedGood Output: banana nut bread * operator also works on strings; it performs repetition. ’Fun’*3 is ’FunFunFun’

Warning! There are limits on where you can use certain expressions. For example, the left-hand side of an assignment statement has to be a variable name, not an expression. The following is illegal: minute+1 = hour

Comments Notes to your programs to explain in natural language what the program is doing, called comments, and they are marked with the # symbol Everything from the # to the end of the line is ignored—it has no effect on the program # compute the percentage of the hour that has elapsed percentage = (minute*100)/60 # caution:integer division