CS 110: Computer Science Fundamentals Expressions and Arithmetics Lec06_expressions01 PDF
Document Details
Uploaded by TantalizingPine
Emory University
Tags
Summary
This document covers expressions and arithmetics in Python programming. It provides examples of arithmetic expressions involving variables, operators like addition, subtraction, multiplication, division, and modulus; explains operator precedence and associativity. It intends to help students understand the different types of expressions used in Python.
Full Transcript
CS 110: Computer Science Fundamentals Expressions and Arithmetics 1 Expressions and Arithmetics 1.1 Expression value1 = int( input (’Please enter a number : ’)) value2 = int( input (’Please enter another number : ’)) sum12 = value1 + value2 print (val...
CS 110: Computer Science Fundamentals Expressions and Arithmetics 1 Expressions and Arithmetics 1.1 Expression value1 = int( input (’Please enter a number : ’)) value2 = int( input (’Please enter another number : ’)) sum12 = value1 + value2 print (value1 , ’+’, value2 , ’=’, sum12) value1 = int(input(’Please enter a number: ’)) stores the value in the memory location value1. bound to sum12 = value1 + value2: This assignment statement contains the assignment operator (=). The variable sum12 left to the assignment operator receives a value when this statement executes. value1 + value2 is an arithmetic expression involving two variables and the addition operator. An arithmetic expression evaluates by adding two values. The evaluated value gets assigned to the sum12 variable. Here are common binary operators. x+y x added to y, if x and y are numbers. x concetened to y, if x and y are strings. x−y x take away y, if x and y are numbers. x∗y x times y, if x and y are numbers. x concatenated with itself y times, if x is a string and y is an integer. y concatenated with itself x times, if y is a string and x is an integer. x/y x divided by y, if x and y are numbers. x//y integer division: Floor of x divided by y, if x and y are numbers. x%y modulus or remainder: Remainder of x divided by y, if x and y are numbers. x ∗ ∗y x raised to y power, if x and y are numbers. Two operators, + and −, are unary operators. – The − unary operator expects a single numeric expression (literal number, variable, and or more com- plicated numeric expression within parenthesis) immediately to its right. – The + unary operator is present only for completeness; when applied to a numeric value, variable, or expression, the resulting value is no different from the original value of its operand. Omitting the unary + operator does not change its behavior. The // and % operators produce an integer when used with integers. But, the / operator always produces a floating-point number. – Floating-point numbers are not real numbers, so the result of 1.0/10.0 cannot be represented exactly without infinite precision. – In the decimal (base 10) number system, one-third is a repeating fraction, so it has an infinite number of digits. – Since numbers within computers are stored in binary (base 2) form, even one-tenth cannot be represented exactly with floating-point numbers 1 one = 1.0 one tenth = 1.0/10.0 zero = one − one tenth − one tenth − one tenth \ − one tenth − one tenth − one tenth \ − one tenth − one tenth − one tenth \ − one tenth print (’one =’, one , ’ one tenth =’, one tenth , ’ zero =’, zero) – Any binary fractional power works precisely. For example, 1 4 = 2−2. Try this. The python automatically joins the following line if the \ is used at the end of an incomplete line. The Python interpreter also automatically joins long statements spread over multiple lines in the source code if it detects an opening parenthesis. x = (int( input (’Please enter an integer ’)) + (y − 2) + 16) ∗ 2 x = (int( input (’Please enter an integer ’)) + (y − 2) + 16) ∗ 2 1.2 Operator Precedence and Associativity When different operators appear in the same expression, the normal rules of arithmetic apply. 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? The multiplicative operators (*, /, //, %) have equal precedence with each other, and the additive operators (binary + or −) have equal precedence with each other. Multiplicative operators have precedence over additive operators. Python accepts parentheses to override the precedence rules. Arity Operators Associativity Bianry ** Right Unary +, - Binary *, /, //, % Left Bianry +, - Left Bianry = Right The assignment operator is used only to build assignment statements. The precedence and associativity do not apply in the context of the assignment operator. Python supports a special assignment statement called the chained assignment. 2