You are on page 1of 30

CSC 101 – Introduction to

Computing
Outline
 Introduction to Python
 Indentation
 First Python Program
 Variables: Integer Values
 Variables & Assignment
 Identifiers
 Floating-Point Values
 User Input
 eval Function
 print Function
 Difference between Expressions & Statements in Python.
What Is a Computer Program?

 Computer programs
 Also called software
 Are a list of instructions
▪ Instructions are called code
 CPU performs the instructions
 Three types
 System software: Operating system, Utility and
others
 Application software

13A-3
Hardware/Software Interaction

 Code
 Statements written in a programming language
 Writing code can be tedious
▪ Code must be perfect
▪ Order of steps must be exact
 Writing code is quite exciting
▪ Problems are solved
▪ New ideas are formed

13A-4
Hardware/Software Interaction

 Machine code
 Recall that computers think in binary
 Code is translated into machine code
▪ CPU executes the machine code
 CPUs have a unique machine code

13A-5
Hardware/Software Interaction

 Programming languages
 Simplifies the writing of code
▪ English is used to describe the binary
 Original code is called source code
 Several hundred languages exist

13A-6
Programming languages

13A-7
Hardware/Software Interaction

 Compilers and interpreters


 Converts source code into binary
▪ Allows code to execute
 Checks source code for correctness

13A-8
Hardware/Software Interaction

 Compiler
 Creates an executable file
▪ Contents are called object code
 Executable can run on its own
 Each language has its own compiler
 C++ and Java are compiled languages

13A-9
Hardware/Software Interaction

 Interpreter
 Runs program one line at a time
 More flexible than compilers
 Slower than compilers
 Always needed to execute program
 Visual Basic and Perl are interpreted

13A-10
Interpreter vs. Compiler

13A-11
Introduction to Python

 general-purpose
 interpreted
 object-oriented programming language
 created by Guido van Rossum in the
Netherlands in 1990
 British comedy troupe Monty Python’s Flying
Circus.
Launching Python
python filename.py
Programming Errors

 Syntax Errors
 Print(“Programming is fun)
 Runtime Errors
 1/0
 Logical Errors
Variables

 Names that can be assigned a value and then


used to refer to that value throughout your
code
 Variables keep values accessible
 Variables give values context
▪ Operator ( +,-,= etc.)
▪ Assignment operator
Rules for Valid Variable Names
 Variable names may contain
 uppercase and lowercase letters (A–Z, a–z),
 digits (0–9), and
 underscores (_),
 but they cannot begin with a digit.
▪ string1
▪ _a1p4a
▪ list_of_names
▪ 9lives
▪ 99_balloons
▪ 2beOrNot2Be
▪ Descriptive Names Are Better Than Short Names
▪ lower_case_with_underscores.
Comments
Integers and Floating-Point Numbers

 Integers
 int()
 int("25")
 integer literal
 1 or int("1")
 1000000, 1_000_000 >>> ?
 floating-point numbers
 float("1.25")
 Floating point literal ??
 1000000.0, 1_000_000.0, 1e6
 2e+17, 1e-4 ??
Input and eval

 You can use the input function to ask the


user to input a value for the radius.
 variable = input("Enter a value: ")
 You can use the function eval to evaluate
and convert it to a numeric value
 eval("34.5“)
 eval("3 + 4")
 eval("51 + (54 * (3 + 2))") returns 321.
Statements and Expressions

 The statement for assigning a value to a


variable is called an assignment statement.
 (=) is used as the assignment operator.
 variable = expression
 An expression represents a computation
involving values, variables, and operators that,
taken together, evaluate to a value.
 1 = x ??
First Python Program
 Python statement prompt >>>
 Case sensitive
 Indentation matters
 Don’t put any punctuation at the end of a statement
 A function performs actions. In the case of the print
function, it displays a message to the console.
print("Python is fun")
 In programming terminology, when you use a
function, you are said to be “invoking a function” or
“calling a function.”
Writing a Simple Program
 Let’s first consider the simple problem of computing the area of a circle.
 How do we write a program for solving this problem?
 Writing a program involves designing algorithms and then translating
them into programming instructions, or code.
 An algorithm describes how a problem is solved by listing the actions
that need to be taken and the order of their execution.
 The algorithm for calculating the area of a circle can be described as
follows:
Writing a program involves
designing a strategy for solving
the problem and then using a
programming language to
implement that strategy.

23
Writing a Simple Program
 The value for the radius is stored in the computer’s memory.
 In order to access it, the program needs to use a variable.
 A variable is a name that references a value stored in the
computer’s memory.
 Rather than using x and y as variable names, choose descriptive
names:
▪ in this case, for example, you can use the name radius for the variable that
references a value for radius and area for the variable that references a
value for area.
Writing a program involves
designing a strategy for solving
the problem and then using a
programming language to
implement that strategy.

24
Writing a Simple Program

 The second step is to compute area by assigning the result


of the expression
 radius * radius * 3.14159 to area.
 In the final step, the program will display the value of area
on the console by using Python’s print function.
Writing a program involves
designing a strategy for solving
the problem and then using a
programming language to
implement that strategy.

25
Writing a Simple Program

File Name: ComputeArea.py

26
Reading Input from the
Console
Reading input from the console
enables the program to accept input
from the user.
🞆 The value entered is a string.
🞆 You can use the function eval to evaluate and convert it to
a numeric value.
🞆 For example,
🞆 eval("34.5") returns 34.5,
🞆 eval("345") returns 345,
🞆 eval("3 + 4") returns 7, and
🞆 eval("51 + (54 * (3 + 2))") returns 321.
27
Reading Input from the Console:
Example

File Name: ComputeAreaWithConsoleInput.py


28
Reading Input from the
Console: Example

File Name: 29

ComputeAverage.py
Summary
 Introduction to Python
 Indentation
 First Python Program
 Variables: Integer Values
 Variables & Assignment
 Identifiers
 Floating-Point Values
 User Input
 eval Function
 print Function
 Difference between Expressions & Statements in Python.

You might also like