Download presentation
Presentation is loading. Please wait.
Published byDarren Willis Modified over 9 years ago
1
Computer Science 101 Introduction to Programming
2
From Algorithms to Programs Algorithms are abstract things that can have –Linguistic realizations (in pseudocode or a programming language) –Hardware realizations (in digital circuitry)
3
What Is a Program? One or more algorithms written in a programming language that can be run on hardware Sometimes called software
4
What Is a Programming Language? A bit like pseudocode, but very strict syntax rules Examples: FORTRAN, COBOL, Lisp, Basic, C, C++, Java, Python, …
5
From Algorithms to Hardware Algorithm Program Circuitry Translate by human being Translate by compiler
6
The Program Development Process Editor Compiler Circuitry Program in programming language Program in machine language InputOutput Syntax errors
7
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
8
Pattern of Simple Programs Get inputs from the user Run an algorithm on these inputs Print the results as outputs
9
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
10
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 = = means set
11
Basic Elements: Keywords Keywords are special names reserved for special purposes print, if, else, while, import, … Color coded in orange in IDLE
12
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 (, …, )
13
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
14
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 int s, or a float when at least one operand is a float
15
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: ") = input( ) input is a function
16
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: ")
17
Output Statements print outputs numbers or strings Outputs a newline by default print "My name is Ken" print "I have 15 students" print
18
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"
19
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: ") = raw_input( ) raw_input is a function
20
For Monday Homework due!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.