Hvordan kører kurset? Jeg taler en smule I programmerer en masse Læs stoffet før undervisningen Løs opgaver under og efter undervisningen Kurset afsluttes.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Structured programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C 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.
Introduction to C Programming
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
Hello, world! Dissect HelloWorld.java Compile it Run it.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
The University of Texas – Pan American
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Fundamentals of Programming I Overview of Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Introduction to Java Applications
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to Python
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
Variables, Expressions, and IO
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
MSIS 655 Advanced Business Applications Programming
Chapter 3 – Control Structures
Chapter 2 - Introduction to C Programming
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Unit 3: Variables in Java
Introduction to C Programming
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Hvordan kører kurset? Jeg taler en smule I programmerer en masse Læs stoffet før undervisningen Løs opgaver under og efter undervisningen Kurset afsluttes med et obligatorisk projekt

2 Intro: Java/Python Differences Dynamically typed: a = 2 s = “Julius Caesar” s = 4 Statically typed: int a; String s; a = 2; s = “Julius Caesar” Blocks indented: if a==1: b=5 Blocks surrounded by { }: if (a==1) { b=5; } Interpreted: python MyProgram.py Compiled: javac MyClass.java java MyClass PythonJava - see

3 First Program in Python: Printing a Line of Text Python code – # symbol: a single line comment (like // in java) – print function: sends a stream of text to be output Executing code – Saved in a file Type code into a.py file and save it (e.g. someName.py ) Type python someName.py – Interactively Type python in the command line: runs the python interpreter Type in code statements one by one

# Fig. 2.1: fig02_01.py # Printing a line of text in Python. print "Welcome to Python!" Welcome to Python! Comments Prints out the line of text Fig. 2.2Code saved in a file 1 # Fig. 2.5: fig02_05.py 2 # Printing multiple lines with a single statement. 3 4 print "Welcome\nto\n\nPython!" Welcome to Python! The \n is used to make the text appear on the next line

5 Python 2.2b2 (#26, Nov , 11:44:11) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information­. >>> print "Welcome to Python!" Welcome to Python! >>> ^D Fig. 2.2Interactive mode. (Python interpreter software Copyright ©2001 Python Software Foundation.)

6 Functions, variables The raw_input function – Used to retrieve data from the user The int function – Used to convert strings to integers The float function – Used to convert strings to floating point numbers

1 # Fig. 2.7: fig02_07.py 2 # Simple addition program. 3 4 # prompt user for input 5 integer1 = raw_input( "Enter first integer:\n" ) # read string 6 integer1 = int( integer1 ) # convert string to integer 7 8 integer2 = raw_input( "Enter second integer:\n" ) # read string 9 integer2 = int( integer2 ) # convert string to integer sum = integer1 + integer2 # compute and assign sum print "Sum is", sum # print sum Enter first integer: 45 Enter second integer: 72 Sum is 117 Prompts the user to enter an integer value Converts the string value into an integer value Adds up and then prints out the sum of the two numbers

8 More on variables "45"integer1 "45" 45 Memory location showing value of the variable (after integer1 = raw_input(..) ) and the name bound to the value. After integer1 = int( integer1 )

integer1 = raw_input( "Enter first integer:\n" ) # read a string print "integer1: ", id( integer1 ), type( integer1 ), integer1 integer1 = int( integer1 ) # convert the string to an integer print "integer1: ", id( integer1 ), type( integer1 ), integer1 Enter first integer: 5 integer1: integer1: Prints the id, type and value before and after the variable is converted into an integer Notice in the output that after the conversion the value is the same but the type and id have changed

10 Accidentally adding strings Python 2.2b2 (#26, Nov , 11:44:11) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> value1 = raw_input( "Enter an integer: " ) Enter an integer: 2 >>> value2 = raw_input( "Enter an integer: " ) Enter an integer: 4 >>> print value1 + value2 24 Fig. 2.8Adding values from raw_input (incorrectly) without converting to integers (the result should be 6).

Arithmetic Symbols – * = multiply – / = divide – % = modulus – ** = exponential ( a**4 means a*a*a*a ) – // = floor division Order – blahblah.. Just put in the extra parentheses. (1+3**3+10/5)*2-5 = 55 ((1+(3**3)+(10/5))*2)-5 = 55

# Fig. 2.19: fig02_19.py # String formatting. integerValue = 4237 print "Integer ", integerValue print "Decimal integer %d" % integerValue print "Hexadecimal integer %x\n" % integerValue floatValue = print "Float", floatValue print "Default float %f" % floatValue print "Default exponential %e\n" % floatValue print "Right justify integer (%8d)" % integerValue print "Left justify integer (%-8d)\n" % integerValue print "Force eight digits in integer %.8d" % integerValue print "Five digits after decimal in float %.5f" % floatValue stringValue = "String formatting" print "Fifteen and five characters allowed in string:" print "(%.15s) (%.5s)" % ( stringValue, stringValue ) Integer 4237 Decimal integer 4237 Hexadecimal integer 108d Float Default float Default exponential e+05 Right justify integer ( 4237) Left justify integer (4237 ) Force eight digits in integer Five digits after decimal in float Fifteen and five characters allowed in string: (String formatti) (Strin) The % formatting operator

# String formatting. name = “Arthur” grade = 11 print “Dear %s, your grade is %d, congratulations!” %(name, grade) Dear Arthur, your grade is 11, congratulations! Easy to put variable values inside strings

14 i_guess_your_number.py Note % operator while 1 / break Indentation if statement

15 i_guess_your_number.py output

16 Indentation – Used to delimit blocks of code (instead of {} ) – The indentation must be the same in each block – There is no rule for the number of spaces but 3 or 4 gives good readability Wrong indentation

17 Expression values >>> if 0:... print "0 is true"... else:... print "0 is false"... 0 is false >>> if 1:... print "non-zero is true"... non-zero is true >>> if -1:... print "non-zero is true"... non-zero is true >>> print 2 < 3 False Expressions may have integer values 0 is false, non-0 is true.

18 Example programs Programs from book, e.g. chapter 2, can be found here: DAIMI Unix system: ~chili/PBI/Programs_from_book/ch02/ Web: Other example programs can be found here: DAIMI Unix system: ~chili/PBI/ExamplePrograms/ Web: