This document provides an overview of core Python concepts including numbers, variables, operators, functions, and errors. It discusses numeric data types like integers and floats, arithmetic operators, variables and assignments, built-in functions like print(), and different types of errors like syntax errors, runtime errors, and logic errors. Example programs are provided to demonstrate various concepts. The homework assigns Python exercises from the chapter on numbers, variables, operators, and functions.
This document provides an overview of core Python concepts including numbers, variables, operators, functions, and errors. It discusses numeric data types like integers and floats, arithmetic operators, variables and assignments, built-in functions like print(), and different types of errors like syntax errors, runtime errors, and logic errors. Example programs are provided to demonstrate various concepts. The homework assigns Python exercises from the chapter on numbers, variables, operators, and functions.
This document provides an overview of core Python concepts including numbers, variables, operators, functions, and errors. It discusses numeric data types like integers and floats, arithmetic operators, variables and assignments, built-in functions like print(), and different types of errors like syntax errors, runtime errors, and logic errors. Example programs are provided to demonstrate various concepts. The homework assigns Python exercises from the chapter on numbers, variables, operators, and functions.
This document provides an overview of core Python concepts including numbers, variables, operators, functions, and errors. It discusses numeric data types like integers and floats, arithmetic operators, variables and assignments, built-in functions like print(), and different types of errors like syntax errors, runtime errors, and logic errors. Example programs are provided to demonstrate various concepts. The homework assigns Python exercises from the chapter on numbers, variables, operators, and functions.
Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 28
Core Objects, Variables, In-
put, and Output
Section 1 Chapter 2 Numbers
• Numbers are referred to as numeric lit-
erals • Two Types of numbers: ints and floats – Whole number written without a decimal point called an int : 34, -34 – Number written with a decimal point called a float : 23.45, 34. Numbers • Arithmetic Operators – Addition(+), subtraction(-), multiplication(*), division(/), and exponentiation(**). • Result of a division is always a float • Result of the other operations is a float if … – Either of the numbers is a float – Otherwise is an int. The print Function • Used to display numbers on the monitor – If n is a number, print(n) displays number n. – The print function can display the result of evaluated expressions – A single print function can display several val- ues: print(m, n, r, …) Example 1: Program demonstrates operations Variables • In mathematics problems, quantities are referred to by names • Names given to the values are called variables Example 2: Program uses speed, time … calculates distance Variables • Assignment statements
• Expression on right evaluated
– That value assigned to variable Variables • The art of programming is – Getting the right values in the right variables at the right time • Rules – It may consist only of the letters a-z, the digits 0-9, the underscore(_). – It must start with a letter or underscore(_) – Python only remembers the first 35 characters. • Examples – R2d2, pay_day, _2a – Pay-day, 2a, name$ • Case sensitivity – Balance, BALANCE, BaLance, balance – camelCaps, milleniumBug, dayOfTheWeek – Initial_value, x_average, interest_rate Variables • Names in Python are case-sensitive • There are thirty-three words, called re- served words (or keywords) – Have special meanings in Python – Cannot be used as variable names – See Appendix B The abs Function The int Function
The round Function
abs, int, and round Example • Example 3: Program evaluates func- tions, prints results Augmented Assignments • Remember: expression on right side of assignment statement evaluated before assignment is made var = var + 1 • Python has special operator to accom- plish same thing var += 1 Augmented Assignments • Example 4: Program illustrates different augmented assignment operators. Two Other Integer Operators • Integer division operator – Written // • Modulus operator – Written % Two Other Integer Operators • Example 5: converts 41 inches to 3 feet, 5 inches: Parentheses, Order of Precedence
• Parentheses used to clarify meaning of
an expression • Order of precedence 1. Terms inside parentheses (inner to outer) 2. Exponentiation 3. Multiplication, division (ordinary and inte- ger), modulus 4. Addition and subtraction Example: Bank interests • $1000 has been deposited in your bank account with annual in- terest rate of 9%. What is the total money you get after 1 year ? • Rough structure plan – Get the data (initial balance & interest rate) into python – Calculate the interest (9% of $1000, i.e. $90) – Add the interest to the balance ($90+$1000, i.e. $1090) – Display the new balance Example: Bank interests • $1000 has been deposited in your bank account with annual in- terest rate of 9%. What is the total money you get after 1 year ? • Rough structure plan – Get the data (initial balance & interest rate) into MATLAB – Calculate the interest (9% of $1000, i.e. $90) – Add the interest to the balance ($90+$1000, i.e. $1090) – Display the new balance • Program balance=1000 rate=0.09 interest= rate*balance balance=balance+interest print(balance) Syntax Errors • Grammatical and punctuation errors are called syntax errors.
Table 2.1 Three Syntax Errors
Runtime Errors • Errors discovered while program is run- ning called runtime errors or exceptions
Table 2.2 Three Runtime Errors
Runtime Errors • When Python encounters exception – Terminates execution of program, displays message
FIGURE 2.2 An Error Message for an Exception Error
Logic Error • Occurs when a program does not per- form the way it was intended • Example average = firstNum + secondNum / 2 • Syntax correct, logic wrong: should be average = (firstNum + secondNum) / 2 • Logic errors are most difficult type of er- ror to locate Numeric Objects in Memory • Consider this code:
• This is what happens:
FIGURE 2.3 Numeric objects in memory.
Homework ch2-1 • Solve the exercise. – Use the Python program to solve it. – Print screen the Editor (program) and Figure, and hard copy it. – Submit it by next next class. – 2-1 과제 : 4, 6, 8, 10, 19, 24, 27, 28, 30, 34, 38, 49, 50, 51, 61, 63, 66, 67, 70, 73, 75, 76, 77 – 과제 실습일 : 다음 수업시간 조교 2 명 . – 제출 : icampus 탑재 . Homework ch2-1_ 온라인용 • Solve the exercise. – 실습시간 조교에게 질문 & 답변 (icampus 문의게시판 또는 email) – 2-1 과제 : 4, 6, 8, 10, 19, 24, 27, 28, 30, 34, 38, 49, 50, 51, 61, 63, 66, 67, 70, 73, 75, 76, 77 Homework ch2-1 • Solve the exercise. – 실습시간 조교에게 질문 & 답변 – 조교가 돌아다니며 질문 못하고 못 풀고 있는 학생 직접 지도 – 문제별 시간분배 : 4~51 20 분 , 61~67 20 분 , 70~77 30 분 – 2-1 과제 : 4, 6, 8, 10, 19, 24, 27, 28, 30, 34, 38, 49, 50, 51, 61, 63, 66, 67, 70, 73, 75, 76, 77