Mooc,fi Flashcards
Computer programs consist of
commands, each command instructing the computer to take some action. A computer executes these commands one by one. Among other things, commands can be used to perform calculations, compare things in the computer’s memory, cause changes in how the program functions, relay messages or ask for information from the program’s user.
Arithmetic operations
You can also put arithmetic operations inside a print command. print(2 + 5), Notice the lack of quotation marks around the arithmetic operations above. Quotation marks are used to signify strings.
strings
are sequences of characters. They can consist of letters, numbers and any other types of characters, such as punctuation. Strings aren’t just words as we commonly understand them, but instead a single string can be as long as multiple complete sentences. Strings are usually printed out exactly as they are written.
Commenting
Any line beginning with the pound sign #, also known as a hash or a number sign, is a comment. This means that any text on that line following the # symbol will not in any way affect how the program functions. Python will simply ignore it.
Comments are used for explaining how a program works, to both the programmer themselves, and others reading the program code.
When the program is run, the comment will not be visible to the user
Input
refers to any information a user gives to the program. Specifically, the Python command input reads in a line of input typed in by the user. It may also be used to display a message to the user, to prompt for specific input.
variable
a variable is a location for storing some value, such as a string or a number. This value can be used later, and it can also be changed.
the value stored in a variable can change. In the previous section we noticed that the new value replaces the old one.
Referencing a variable
A single variable can be referred to many times in a program
Choosing a good name for a variable
It is often useful to name variables according to what they are used for. For example, if the variable contains a word, the name word is a better choice than, say, a.
There is no set limit to the length of a variable name in Python, but there are some other limitations. A variable name should begin with a letter, and it can only contain letters, numbers and underscores _.
Lowercase and uppercase letters are different characters. The variables name, Name and NAME are all different variables. While this rule has a few exceptions, we will ignore those for now.
It is a common programming practice in Python to use only lowercase characters in variable names. If the variable name consists of multiple words, use an underscore between the words. While this rule also has a few exceptions, we will ignore those for now.
Integers
Integers are numbers that do not have a decimal or fractional part, such as -15, 0 and 1.
Notice the lack of quotation marks here. In fact, if we were to add quotation marks around the number, this would mean our variable would no longer be an integer, but a string instead. A string can contain numbers, but it is processed differently.
For integer values the + operator means addition, but for string values it means concatenation, or “stringing together”.
If we do want to print out a string and an integer in a single command, the integer can be cast as a string with the str function, and the two strings can then be combined normally.
The print command also has built-in functionalities that support combining different types of values. The simplest way is to add a comma between the values. All the values will be printed out regardless of their type
Printing with f-strings
So called f-strings are another way of formatting printouts in Python. The syntax can initially look a bit confusing, but in the end f-strings are often the simplest way of formatting text.
With f-strings the previous example would look like this:
result = 10 * 25
print(f”The result is {result}”)
A single f-string can contain multiple variables.
Floating point numbers
Floating point number or float is a term you will come across often in programming. It refers to numbers with a decimal point. They can be used much in the same way as integer values.
end = “”
For example:
print(“Hi “, end=””)
print(“there!”)
Sample output
Hi there!
Arithmetic operations
Operator Purpose Example Result
+ Addition 2 + 4 6
- Subtraction 10 - 2.5 7.5
* Multiplication -2 * 123 -246
/ Division (floating point result) 9 / 2 4.5
// Division (integer result) 9 // 2 4
% Modulo 9 % 2 1
** Exponentiation 2 ** 3 8
The order of operations is familiar from mathematics
first calculate the exponents, then multiplication and division, and finally addition and subtraction. The order can be changed with parentheses.
Operands, operators and data types
A calculation usually consists of operands and operators.
The data type of an operand usually determines the data type of the result: if two integers are added together, the result will also be an integer. If a floating point number is subtracted from another floating point number, the result is a floating point number. In fact, if a single one of the operands in an expression is a floating point number, the result will also be a floating point number, regardless of the other operands.
Division / is an exception to this rule. Its result is a floating point number, even if the operands are integers. For example 1 / 5 will result in the floating point number 0.2.