Learn Python quick. Jens Braun
Suppose x = 5, y = 2
Addition:
x + y = 7
Subtraction:
x - y = 3
Multiplication:
x*y = 10
Division:
x/y = 2.5
Floor Division:
x//y = 2 (rounds down the answer to the nearest whole number)
Modulus:
x%y = 1 (gives the remainder when 5 is divided by 2)
Exponent:
x**y = 25 (5 to the power of 2)
More Assignment Operators
Besides the = sign, there are a few more assignment operators in Python (and most programming languages). These include operators like +=, -= and *=.
Suppose we have the variable x, with an initial value of 10. If we want to increment x by 2, we can write
x = x + 2
The program will first evaluate the expression on the right (x + 2) and assign the answer to the left. So eventually the statement above becomes x <- 12.
Instead of writing x = x + 2, we can also write x += 2 to express the same meaning. The += sign is actually a shorthand that combines the assignment sign with the addition operator. Hence, x += 2 simply means x = x + 2.
Similarly, if we want to do a subtraction, we can write x = x - 2 or x -= 2. The same works for all the 7 operators mentioned in the section above.
Chapter 4: Data Types in Python
In this chapter, we’ll first look at some basic data types in Python, specifically the integer, float and string. Next, we’ll explore the concept of type casting. Finally, we’ll discuss three more advanced data types in Python: the list, tuple and dictionary.
Integers
Integers are numbers with no decimal parts, such as -5, -4, -3, 0, 5, 7 etc.
To declare an integer in Python, simply write variableName = initial value
Example:
userAge = 20, mobileNumber = 12398724
Float
Float refers to numbers that have decimal parts, such as 1.234, -0.023, 12.01.
To declare a float in Python, we write variableName = initial value
Example:
userHeight = 1.82, userWeight = 67.2
String
String refers to text.
To declare a string, you can either use variableName = ‘initial value’ (single quotes) or variableName = “initial value” (double quotes)
Example:
userName = ‘Peter’, userSpouseName = “Janet”, userAge = ‘30’
In the last example, because we wrote userAge = ‘30’, userAge is a string. In contrast, if you wrote userAge = 30 (without quotes), userAge is an integer.
We can combine multiple substrings by using the concatenate sign (+). For instance, “Peter” + “Lee” is equivalent to the string “PeterLee”.
Built-In String Functions
Python includes a number of built-in functions to manipulate strings. A function is simply a block of reusable code that performs a certain task. We’ll discuss functions in greater depth in Chapter 7.
An example of a function available in Python is the upper() method for strings. You use it to capitalize all the letters in a string. For instance, ‘Peter’.upper() will give us the string “PETER”. You can refer to Appendix A for more examples and sample codes on how to use Python’s built-in string methods.
Formatting Strings using the % Operator
Strings can also be formatted using the % operator. This gives you greater control over how you want your string to be displayed and stored. The syntax for using the % operator is
“string to be formatted” %(values or variables to be inserted into string, separated by commas)
There are three parts to this syntax. First we write the string to be formatted in quotes. Next we write the % symbol. Finally, we have a pair of round brackets ( ) within which we write the values or variables to be inserted into the string. This round brackets with values inside is actually known as a tuple, a data type that we’ll cover in the chapter later.
Type the following code in IDLE and run it.
brand = ‘Apple’
exchangeRate = 1.235235245
message = ‘The price of this %s laptop is %d USD and the exchange rate is %4.2f USD to 1 EUR’ %(brand, 1299, exchangeRate)
print (message)
In the example above, the string ‘The price of this %s laptop is %d USD and the exchange rate is %4.2f USD to 1 EUR’ is the string that we want to format. We use the %s, %d and %4.2f formatters as placeholders in the string.
These placeholders will be replaced with the variable brand, the value 1299 and the variable exchangeRate respectively, as indicated in the round brackets. If we run the code, we’ll get the output below.
The price of this Apple laptop is 1299 USD and the exchange rate is 1.24 USD to 1 EUR
The %s formatter is used to represent a string (“Apple” in this case) while the %d formatter represents an integer (1299). If we want to add spaces before an integer, we can add a number between % and d to indicate the desired length of the string. For instance “%5d” %(123) will give us “ 123” (with 2 spaces in front and a total length of 5).
The %f formatter is used to format floats (numbers with decimals). Here we format it as %4.2f where 4 refers to the total length and 2 refers to 2 decimal places. If we want to add spaces before the number, we can format is as %7.2f, which will give us “ 1.24” (with 2 decimal places, 3 spaces in front and a total length of 7).
Formatting Strings using the format() method
In addition to using the % operator to format strings, Python also provides us with the format() method to format strings. The syntax is
“string to be formatted”.format(values or variables to be inserted into string, separated by commas)
When we use the format method, we do not use %s, %f or %d as placeholders. Instead we use curly brackets, like this:
message = ‘The price of this {0:s} laptop is {1:d} USD and the exchange rate is {2:4.2f} USD to 1 EUR’.format(‘Apple’, 1299, 1.235235245)
Inside the curly bracket, we first write the position of the parameter to use, followed by a colon. After the colon, we write the formatter. There should not be any spaces within the curly brackets.
When we write format(‘Apple’, 1299, 1.235235245), we are passing in three parameters to the format() method. Parameters are data that the method needs in order to perform its task. The parameters are ‘Apple’, 1299 and 1.235235245.
The