# Date: Sometime in the past
# Description: A program
# explores arithmetic operators
print(16+3)
print(16-3)
print(16*3)
print(16**3)
print(16/3)
print(16//3)
print(16.0/3)
print(16.0//3)
print(16%3)
Critical Thinking Questions:
1. Draw a line between each flowchart symbol and its corresponding line of Python code. Make note of any problems.
2. Execute the print statements in the previous Python program
a. Next to each print statement above, write the output. b. What is the value of the following line of code?
c. Predict the values of 17%3 and 18%3 without using your computer.
3. Explain the purpose of each arithmetic operation:
a. + ____________________________
b. - ____________________________
c. * ____________________________
d. ** ____________________________
e. / ____________________________
f. // ____________________________
g. % ____________________________
An assignment statement is a line of code that uses a "=" sign. The statement stores the result of an operation performed on the right-hand side of the sign into the variable memory location on the left-hand side.
4. Enter and execute the following lines of Python code in the editor window of your IDE (e.g. Thonny):
Python Program 1 |
---|
|
a. What are the variables in the above python program? b. What does the assignment statement : MethaneMolMs = 16 do? c. What happens if you replace the comma (,) in the print statements with a plus sign (+) and execute the code again? Why does this happen?
5. What is stored in memory after each assignment statement is executed?
Note: Concatenating Strings in python
The "+" concatenates the two strings stored in the variables into one string. "+" can only be used when both operators are strings.
6. Run the following program in the editor window of your IDE (e.g. Thonny) to see what happens if you try to use the "+" with strings instead of numbers?
Python Program 2 |
---|
lastName ="Python" fullName = firstName + lastName print(fullName) print(firstName,lastName) |
a. The third line of code contains an assignment statement. What is stored in fullName when the line is executed? b. What is the difference between the two output lines? c. How could you alter your assignment statements so that print(fullName) gives the same output as print(firstName,lastName) d. Only one of the following programs will work. Which one will work, and why doesn’t the other work? Try doing this without running the programs!
Python Program 3 | Python Program 4 |
---|---|
streetName = "Pennsylvania Ave" streetAddress= addressNumber + streetName print(streetAddress) | streetName = "Pennsylvania Ave" streetAddress= addressNumber + streetName print(streetAddress) |
e. Run the programs above and see if you were correct. f. The program that worked above results in no space between the number and the street name. How can you alter the code so that it prints properly while using a concatenation operator?
7. Before entering the following code into the Python interpreter (Thonny IDE editor window), predict the output of this program.
Python Program 5 | Predicted Output |
---|---|
* 10 print(myNumber) myWord = "Cool!" + 10 print(myWord) |
|
Now execute it. What is the actual output? Is this what you thought it would do? Explain.
8. Let’s take a look at a python program that prompts the user for two numbers and subtracts them.
Execute the following code by entering it in the editor window of Thonny.
Python Program 6 |
---|
("Enter a number: ") secondNumber = input("Enter another number: ") difference= firstNumber - secondNumber print("*" * 10) print("Difference = ", difference) |
a. What output do you expect? b. What is the actual output c. Revise the program in the following manner:
d. Explain the purpose of the function int(). e. Explain how the changes in the program produced the desired output.
There are multiple ways to format output in python. The old way is to use the string modulo %, and the new way is with a format method function.
Python Program 7 | Output |
# format with string modulo ( % (number)) ( "% (number)) ( % (number)) ( , (number, )) ( , (number, )) ( , (number, )) ( , (number, )) ( , (number, )) ( , (number, )) |
9. Look closely at the output for python program 7.
a. How do you indicate the number of decimals to display using
the string modulo (%) ______________________________________________________
the format function ________________________________________________________
b. What happens to the number if you tell it to display less decimals than are in the number, regardless of formatting method used?
c. What type of code allows you to right justify your numbers?
10. Execute the following code by entering it in the editor window of Thonny.
Python Program 7 |
---|
laptopCost = price = numLaptops* laptopCost ( , price) |
a. Does the output look like standard output for something that has dollars and cents associated with it?
b. Replace the last line of code with the following:
print("Total cost of laptops: $%.2f" % price)
print("Total cost of laptops:" ,format(price, '.2f.))
Discuss the change in the output.
c. Replace the last line of code with the following:
print("Total cost of laptops: $", format(price,'.2f') print("Total cost of laptops: $" ,format(price, '.2f.))
Discuss the change in the output.
d. Experiment with the number ".2" in the ‘0.2f’ of the print above statement by substituting the following numbers and explain the results.
.4 ___________________________________________________
.0 ___________________________________________________
.1 ___________________________________________________
.8 ___________________________________________________
e. Now try the following numbers in the same print statement. These numbers contain a whole number and a decimal. Explain the output for each number.
02.5 ___________________________________________________
08.2 ___________________________________________________
03.1 ___________________________________________________
f. Explain what each part of the format function: format(variable, "%n.nf") does in a print statement where n.n represents a number.
variable ____________________________ First n _________________________
Second n_______________________ f _________________________
g. Revise the print statement by changing the "f" to "d" and laptopCost = 600 . Execute the statements and explain the output format.
print("Total cost of laptops: %2d" % price) print("Total cost of laptops: %10d" % price)
h. Explain how the function format(var,'10d') formats numeric data. var represents a whole number.
REMINDER: Computers perform four main operations on data: data into a computer data to a screen or file data using arithmetic, logical, searching or sorting operations data |
11. Use the following program and output to answer the questions below.
Program | Sample Output |
---|---|
( ) numItems = ( ( )) itemCost = ( ( )) #Calculate price totalCost=numItems*itemCost #Printing results ( , itemName) ( , itemCost) ( , numItems) ( % totalCost) |
a. From the code and comments in the previous program, explain how the four main operations are implemented in this program. b. There is one new function in this sample program. What is it? From the corresponding output, determine what it does.
Application Questions: Use the Python Interpreter to check your work
courseLabel = "CHEM" courseNumber = "3350"
Write a line of Python code that concatenates the label with the number and stores the result in the variable courseName . Be sure that there is a space between the course label and the course number when they are concatenated.
height1 = 67850 height2 = 456
Use Python formatting to write two print statements that will produce the following output exactly at it appears below:
Homework Assignment: s2020py03
Download the assignment from the website, fill out the word document, and upload to your Google Drive folder the completed assignment along with the two python files.
1. (5 pts) Write a Python program that prompts the user for two numbers, and then gives the sum and product of those two numbers. Your sample output should look like this:
Enter your first number:10 Enter your second number:2 The sum of these numbers is: 12 The product of these two numbers is: 20
2. (10 pts) Write a program that calculates the molarity of a solution. Molarity is defined as numbers of moles per liter solvent. Your program will calculate molarity and must ask for the substance name, its molecular weight, how many grams of substance you are putting in solution, and the total volume of the solution. Report your calculated value of molarity to 3 decimal places. Your output should also be separated from the input with a line containing 80 asterixis.
Assuming you are using sodium chloride, your input and output should look like:
3. (4 pts) Make two hypothes.is annotations dealing with external open access resources on formatting with the format function method of formatting. These need the tag of s20iostpy03ualr .
C operators.
Assignment operators are used for assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
1. “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example:
2. “+=” : This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. Example:
If initially value stored in a is 5. Then (a += 6) = 11.
3. “-=” This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. Example:
If initially value stored in a is 8. Then (a -= 6) = 2.
4. “*=” This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. Example:
If initially value stored in a is 5. Then (a *= 6) = 30.
5. “/=” This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. Example:
If initially value stored in a is 6. Then (a /= 2) = 3.
Below example illustrates the various Assignment Operators:
Similar reads.
Learning the syntax of a programming language is key if you want to use that language effectively. This is true for both new and experienced developers.
And one of the most important things to pay attention to while learning a programming language is whether the code you're dealing with is a statement or an expression.
It can sometimes be confusing to differentiate between statements and expressions in programming. So this article is meant to simplify the differences so that you can improve your programming skills and become a better developer.
An expression is any word or group of words or symbols that is a value. In programming, an expression is a value, or anything that executes and ends up being a value.
It is necessary to understand that a value is unique. For example, const , let , 2 , 4 , s , a , true , false , and world are values because each of them is unique in meaning or character.
Let's look at some code as an example:
Judging from the code above, const , price , = , and 500 are expressions because each of them has a definite and unique meaning or value. But if we take all of them together const price = 500 - then we have a statement.
Let's look at another example:
Looking at the code above, you can see an anonymous function is assigned to a variable. Oh, wait! You might know that any function is a statement. Can it also be an expression?
Yes! A "function" and a "class" are both statements and expressions because they can perform actions (do or not do tasks) and still execute to a value.
This brings us to statements – so what are they?
A statement is a group of expressions and/or statements that you design to carry out a task or an action.
Statements are two-sided – that is, they either do tasks or don't do them. Any statement that can return a value is automatically qualified to be used as an expression. That is why a function or class is a statement and also an expression in JavaScript.
If you look at the example of the function under the section on expressions, you can see it is assigned and execute to a value passed to a variable. That is why it is an expression in that case.
Inline statements.
The whole of the code above is a statement because it carries out the task of assigning $2000 to amount . It is safe to say a line of code is a statement because most compilers or interpreters don't execute any standalone expression.
Look at the below if statement:
The if statement is a statement because it helps us check whether I love you or not. As I have said before, it is two-sided: this code finds out whether "I love you" or not, and that is why it is a statement. Also, it doesn't return any value but it can create side effects.
Here's a loop statement:
In short, any loop is a statement because if it can only do the tasks it is meant to do or not – does loop and doesn't loop. But a loop can't execute to a value in the end. They can only have side effects in JavaScript. Once they can execute to a value in a programming language, then they can also be used as an expression.
For example, you can use forloop and if statement as expressions in Python.
There is also an "IF" expression in Python. That means that something that is a statement in one language can be an expression (or both statement and expression) in another.
Look at the below function statement:
We declare the function add(firstNumber, secondNumber) and it returns a value. The function is called with two arguments as in add(2, 3) by declaration and so it is a statement. If you pay close attention, you will realize that calling the function as a statement is useless since it has no side effect.
Hey, stop! How can we turn it into an expression? Oh yeah, we can do it like this:
Though the function is now an expression the way it is called above, the whole of the code is still a statement.
Check out this class statement:
You can see that we declare the class "Person" and instantiate and assign it to "User" immediately. So, it is used as an expression.
Now, let's use it as a statement:
A class is similar to a function in the sense that it can be declared, assigned, or used as an operand just like a class. So, a class is a statement and/or an expression.
Expressions can be assigned or used as operands, while statements can only be declared.
Statements create side effects to be useful, while expressions are values or execute to values.
Expressions are unique in meaning, while statements are two-sided in execution. For example, 1 has a certain value while go( ) may be executed or not.
Statements are the whole structure, while expressions are the building blocks. For example, a line or a block of code is a statement.
First of all, understanding the difference between statements and expressions should make learning new programming languages less surprising. If you're used to JavaScript, you may be surprised by Python's ability to assign an if statement as a variable which is not possible in JavaScript.
Second, it makes it easy to use programming paradigms across different programming languages.
For example, a JavaScript "if statement" cannot be used as an expression because it can't execute to a value – it can only create side effects. Yet, you can use the ternary operator if you want to avoid the side effects of using an if statement in JavaScript.
For this reason, you can understand why some programmers avoid if statements by using the ternary operator in JavaScript. It is because they want to avoid side effects .
It also makes your realize why you have to be always careful about the scope of your variables whenever you use a statement. This is true because statements mostly have side effects to be useful, and it is reasonable to understand the scope of your variables and operations. For example,
Hey wait! What would be logged in the console if you ran the code above?
Tell yourself the answer first and then paste the code in the console to confirm. If you you're wrong, you need to learn more about scope and side effects. But if you're right, try to make those functions a bit better to avoid the confusion they may generate.
Knowing the difference also helps you to easily identify non-composable and composable syntaxes (functions, classes, modules, and so on) of a programming language. This makes porting your experience from one programming language to another more interesting and direct.
Now that you understand the difference between expressions and statements in programming, and you know why understanding the differences is important, you can identify pieces of code as expressions or statements while coding.
Next time, we'll go even further and help make learning a second programming language easier.
Go and get things done now! See you soon.
I am planning to share a lot about programming tips and tutorials in 2023. If you're struggling to build projects or you want to stay connected with my write-ups and videos, please join my list at YouTooCanCode or subscribe to my YouTube channel at You Too Can Code on YouTube .
Ayobami loves writing history with JavaScript(React) and PHP(Laravel). He has been making programming fun to learn for learners. Check him out on YouTube: https://bit.ly/3usOu3s
If you read this far, thank the author to show them you care. Say Thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
IMAGES
VIDEO
COMMENTS
Assignment (computer science) In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location (s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement (or expression) is a fundamental construct.
An Assignment statement is a statement that is used to set a value to the variable name in a program. Assignment statement allows a variable to hold different types of values during its program lifespan. Another way of understanding an assignment statement is, it stores a value in the memory location which is denoted.
Assignment: An assignment is a statement in computer programming that is used to set a value to a variable name. The operator used to do assignment is denoted with an equal sign (=). This operand works by assigning the value on the right-hand side of the operand to the operand on the left-hand side. It is possible for the same variable to hold ...
Assignment Kenneth Leroy Busbee. Overview. An assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. [1] Discussion. The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable).
The assignment statement. The assignment statement is used to store a value in a variable. As in most programming languages these days, the assignment statement has the form: <variable>= <expression>; For example, once we have an int variable j, we can assign it the value of expression 4 + 6: int j; j= 4+6; As a convention, we always place a ...
The meaning of the first assignment is computing the sum of the value in Counter and 1, and saves it back to Counter. Since Counter 's current value is zero, Counter + 1 is 1+0 = 1 and hence 1 is saved into Counter. Therefore, the new value of Counter becomes 1 and its original value 0 disappears. The second assignment statement computes the ...
Assignment Statements ¶. Assignment statements initialize or change the value stored in a variable using the assignment operator =. An assignment statement always has a single variable on the left hand side. The value of the expression (which can contain math operators and other variables) on the right of the = sign is stored in the variable ...
To create a new variable or to update the value of an existing one in Python, you'll use an assignment statement. This statement has the following three components: A left operand, which must be a variable. The assignment operator ( =) A right operand, which can be a concrete value, an object, or an expression.
In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location (s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement (or expression) is a fundamental construct. Today, the most commonly used notation ...
That's how assignment works. But in algebra, the equal sign means that the values on both sides are the same. So the way you know computing isn't algebra is that x = x + 1 is meaningless in algebra. No number equals itself plus one. That is a contradiction. So, when you see assignment statements in programming, realize that they mean to ...
Assignment Statements. One of the most common statements (instructions) in C++ is the assignment statement, which has the form: destination = expression ; = is the assignment operator. This statement means that the expression on the right hand side should be evaluated, and the resulting value stored at the desitnation named on the left.
An assignment statement changes the value that is held in a variable. The program uses an assignment statement. The assignment statement puts the value 123 into the variable. In other words, while the program is executing there will be a 64 bit section of memory that holds the value 123. Remember that the word "execute" is often used to mean "run".
Assignment means: "storing a value (of a particular type) under a variable name". Think of each assignment as copying the value of the righthand side of the expression into a "bucket" associated with the left hand side name! Read this as, the variable called "name" is "assigned" the value computed by the expression to the right of the ...
Rule 1. Name must be comprised of digits, upper case letters, lower case letters, and the underscore character "_". Rule 2. Must begin with a letter or underscore. A good name for a variable is short but suggestive of its role: Circle_Area.
The assignment operator = is used to associate a variable name with a given value. For example, type the command: a=3.45. in the command line window. This command assigns the value 3.45 to the variable named a. Next, type the command: a. in the command window and hit the enter key. You should see the value contained in the variable a echoed to ...
Assignment operators are used in programming to assign values to variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...
An assignment statement is a line of code that uses a "=" sign. The statement stores the result of an operation performed on the right-hand side of the sign into the variable memory location on the left-hand side. 4. Enter and execute the following lines of Python code in the editor window of your IDE (e.g. Thonny):
Multiple- target assignment: x = y = 75. print(x, y) In this form, Python assigns a reference to the same object (the object which is rightmost) to all the target on the left. OUTPUT. 75 75. 7. Augmented assignment : The augmented assignment is a shorthand assignment that combines an expression and an assignment.
Learn the basics of assignment statements in Python in this tutorial. We'll cover the syntax and usage of the assignment operator, including multiple assignm...
1. "=": This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10; b = 20; ch = 'y'; 2. "+=": This operator is combination of '+' and '=' operators. This operator first adds the current value of the variable on left to the value on the right and ...
In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. ... The meaning of a statement is determined by its semantics. Simple statements ... this is simply a special case of the assignment statement built into the language grammar rather than a true expression.
Statements create side effects to be useful, while expressions are values or execute to values. Expressions are unique in meaning, while statements are two-sided in execution. For example, 1 has a certain value while go( ) may be executed or not. Statements are the whole structure, while expressions are the building blocks.
Assigning variables. Here's how we create a variable named score in JavaScript: var score = 0; That line of code is called a statement. All programs are made up of statements, and each statement is an instruction to the computer about something we need it to do. Let's add the lives variable: var score = 0; var lives = 3;