200 likes | 335 Views
Computer Science 101. Introduction to Programming. From Algorithms to Programs. Algorithms are abstract things that can have Linguistic realizations (in pseudocode or a programming language) Hardware realizations (in digital circuitry). What Is a Program?.
E N D
Computer Science 101 Introduction to Programming
From Algorithms to Programs Algorithms are abstract things that can have • Linguistic realizations (in pseudocode or a programming language) • Hardware realizations (in digital circuitry)
What Is a Program? • One or more algorithms written in a programming language that can be run on hardware • Sometimes called software
What Is a Programming Language? • A bit like pseudocode, but very strict syntax rules • Examples: FORTRAN, COBOL, Lisp, Basic, C, C++, Java, Python, …
From Algorithms to Hardware Algorithm Translate by human being Program Translate by compiler Circuitry
The Program Development Process Editor Program in programming language Compiler Syntax errors Program in machine language Circuitry Input Output
Python • Free, general purpose programming language • Can run code interactively in a shell • Can also edit and run longer code segments called scripts or programs
Pattern of Simple Programs • Get inputs from the user • Run an algorithm on these inputs • Print the results as outputs
Basic Elements: Variables • Variable names - a sequence of letters and/or digits, can include _ (the underscore) • Case sensitive - radius and Radius name two distinct variables • Variables are usually spelled with lowercase letters, constants with uppercase letters
Assignment Statement • Gives a variable a value • Variables must have values before they are used in expressions PI = 3.14 area = PI * 6 ** 2 print area <variable> = <expression> = means set
Basic Elements: Keywords • Keywords are special names reserved for special purposes • print, if, else, while, import, … • Color coded in orange in IDLE
Functions • Run a computation on a given value to produce a new value • The given values are called arguments >>> abs(-45) 45 >>> round(3.1416, 3) 3.142 <function>(<arg1>, …, <argn>)
Numeric Data Types • int - whole numbers, such as 45, 100, -20 • float - numbers with a decimal point, such as 3.14, 66.222, -0.33
Arithmetic Operators • +, -, *, /, %, ** • Evaluate from left to right, but do ** first, the *, /, %, then +, - • Use parentheses to override the standard order of evaluation • / and % produce integer quotient and remainder with two ints, or a float when at least one operand is a float
Numerical Input • The user is prompted with a text message • The sequence of keystrokes is converted to an int or a float length = input("Enter the length: ") width = input("Enter the width: ") <variable> = input(<a string>) input is a function
String Data Type • Use " or ' as delimiters for simple strings • Use """ as delimiters for multiline strings or comments """ File: rectangle.py Author: Ken """ length = input("Enter the length: ")
Output Statements • print outputs numbers or strings • Outputs a newline by default print "My name is Ken" print "I have 15 students" print <expression>
Output Statements • Use a comma to print several data values on the same line, separated by a space name = "Ken" number = 15 print "My name is", name print "I have", number, "students"
Input of Text (Strings) • The user is prompted with a text message • The sequence of keystrokes is converted to a single string name = raw_input("Enter your name: ") color = raw_input("Enter your hair color: ") <variable> = raw_input(<a string>) raw_input is a function
For Monday Homework due!