Python Programming Expressions and Arithmetic Operators Overview

Slide Note
Embed
Share

Learn about expressions, arithmetic operators, value combinations with operators, operator precedence and associativity in Python programming. Understand arithmetic operations, variable assignments, common errors, and examples highlighting key concepts such as unary and binary operators. Enhance your knowledge of syntax errors, runtime errors, and logic errors to improve your Python programming skills.


Uploaded on Sep 14, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Python Programming UNIT II 14 September 2024 III B.Sc IT 1

  2. Expressions and Arithmetic Expressions Operators 14 September 2024 III B.Sc IT 2

  3. Expressions Values and variables can be combined with operators to form more complex expressions >>>sum = value1 + value2; Assignment operator is an arithmetic expression involving two variables and the addition operator. The expression is evaluated by adding together the values bound to the two variables 14 September 2024 III B.Sc IT 3

  4. Arithmetic Operators The common arithmetic operations are Addition Subtraction Multiplication Division and Power behave in the expected way 14 September 2024 III B.Sc IT 4

  5. Example x, y, z = 3, -4, 0 x = -x y = -y z = -z print(x, y, z) OUTPUT -3 4 0 14 September 2024 III B.Sc IT 5

  6. Operator Precedence and Associativity All Python operators have a precedence and associativity: Precedence when an expression contains two different kinds of operators, which should be applied first? Associativity when an expression contains two operators with the same precedence, which should be applied first? 14 September 2024 III B.Sc IT 6

  7. Example print(2 + 3 * 4) RESULT_____________ Arity Operators Associativity Unary +, - Binary *, /, % Left Binary +, - Left Binary = Right 14 September 2024 III B.Sc IT 7

  8. Errors In Python, there are three general kinds of errors: Syntax errors Run-time errors Logic errors. 14 September 2024 III B.Sc IT 8

  9. Syntax errors A syntax error is a common error that the interpreter can detect when translate a python program into machine language >>>x = y + 2 #produce output >>>y+2=x #Error statement 14 September 2024 III B.Sc IT 9

  10. Run-time Errors Errors depend on the context of the program s execution. Such errors are called run-time errors or exceptions. Run-time errors arise after the interpreter s translation phase and during its execution phase. >>>x = y + 2 14 September 2024 III B.Sc IT 10

  11. Logic Errors The program contains an error, but the interpreter is unable detect the problem. An error of this type is known as a logic error For example >>>dividend, divisor = eval(input('Please enter two numbers to divide: ')) >>>print(dividend, '/', divisor, "=", dividend/divisor) When the condition changes to divisor/dividend; The program runs, and unless a value of zero is entered for the dividend, the interpreter will report no errors. However, the answer it computes is not correct in general. 14 September 2024 III B.Sc IT 11

  12. 14 September 2024 III B.Sc IT 12

  13. Question 1 Given the following assignments: i1 = 2 i2 = 5 i3 = -3 d1 = 2.0 d2 = 5.0 (a) i1 + i2 (b) i1 / i2 (c) i1 - i2 (d) i2 % i1 (e) d1*i1/i2 (f) d1/d2*i1 (g) i1 * i3 Evaluate each of the following Python expressions. 14 September 2024 III B.Sc IT 13

  14. Question 2 If x = 2 Indicate what each of the following Python statements would print. (a) print("x") (b) print( x ) (c) print(x) (d) print("x + 1") (e) print( x + 1) (f) print(x + 1) 14 September 2024 III B.Sc IT 14

  15. Program to check Armstrong numbers in certain interval 14 September 2024 III B.Sc IT 15

Related


More Related Content