Table of Contents
Click on the content icon to the right to scroll down to the content you want.
We need an Integrated Development Environment (IDE) – place to write and execute the code for us. But…
IDE
What is an IDE?
To recap:
An IDE is a program that helps programmers increase productivity. It allows us to write programs in different programming languages with ease.
How does it help to write programs?
IDEs that support coding in Python have a Python interpreter and compilers for other programming languages it supports. When we write a Python program, the IDE compiles and interprets our code and produces output. Not only does it allow us to write programs, but we can also use the IDE to modify already existing programs, debug (fix syntax or logical errors) programs all in one environment.
Most modern IDEs have more tools. One tool is autofill, which completes a word after you type enough unique keys corresponding to that word in order to increase typing speed and save time.
What does it look like?
Since IDEs are applications, different IDEs have a few differences. However, most functionalities are the same:
- IDEs have a workspace to allow us to write code.
- To run our programs, IDEs usually have either a Build&Run or Run button.
- There’s also a command line or shell to display output whenever we Run our program.
Inside most IDEs there is an “engine” that executes the code – this either an Interpreter or a Compiler. But what is the difference between the two?
Interpreter VS. Compiler
How IDEs execute code
Variables
What is a variable?
To recap:
When programming, we’ll often need to work with text or numbers a lot. The text or numbers we are working with needs to be stored somewhere so that it can be retrieved later if we want to change it or if we want to use it somewhere. This is where variables come in. Variables help us store our data safely on unique memory locations of the computer.
Variables work as containers for data. A single variable can only store certain types of data. If we choose to store an integer number (1,2,3) in a variable, we cannot replace that number with a letter (a,b,c) later on. However, we can replace it with another integer number later on. The same is true for variables that were storing characters or strings of characters.
Variable naming conventions:
Every word, except the first one, starts with an uppercase letter.
- Example: myFavouriteProgrammingLanguage
Every word in the variable name starts with an uppercase letter.
- Example: MyFavouriteProgrammingLanguage
The variable name is written in small caps, and if needed to separate words in the variable name, an underscore ( _ ) is used.
- Example: my_favourite_programming_language
Every letter is capitalised, and words are separated by an underscore
- Example: MY_FAVOURITE_PROGRAMMING_LANGUAGE
There are a few rules to keep in mind when naming variables:
- The first character must be an alphabet or an underscore;
- The remaining characters must be alphabets, digits or underscores;
- We are not allowed to use reserved Python keywords when naming variables; and
- We are not allowed to use special characters.
Primitive Data Types
Deep dive
A data type is a classification method to make the Python interpreter aware of the kind of data it is working with. It is a way of helping Python know what our input looks like, so that it can process the right kind of output when running our code.
Python has five basic classes of data types as outlined in the Figure 1 alongside.
Numeric: Integer (int) Data Type
We are in the Grade 7 Mathematics class and our teacher is introducing us to the concept of integers.
What are integers?
Integers in mathematics, referred to as “int” in programming, are any whole numbers we can represent on the number line. These include all negative whole numbers(-1,-2,-3), zero, and all positive whole numbers(1,2,3).
When working with integers, we cannot represent decimal values and fractions.
Numeric: Float Data Type
Numbers span more than just whole numbers. We have numbers that need to be represented using point decimals – such as the number 23.0 Some are fractions – such as 0.25.
To represent such values, computers use float and double data types.
Float vs Double data types:
- Floats are 4 bytes = 32 bits in size
- Doubles are 8 bytes = 64 bits in size
What this basically means is that you can fit more data, in this case numbers, in a double than in a float.
In Python, however, only float is valid for representing decimal values.
Numeric: Complex Data Type
It is important to understand that we have both Real and Imaginary Numbers. We only learn about Real Numbers in high school. Complex numbers, are numbers that contain two parts: a real part and an imaginary part.
This is how they are typically represented in mathematics:
z = 2 + 3i (formula is written as z = a + bi)
- z represents the number,
- 2 is the real part of the imaginary number z and
- 3i is the imaginary part of the imaginary number z.
Complex numbers are represented on a 2 dimensional cartesian plane (like a graph), and not on a 1 dimensional number line.
None Data Type
None Data Type is not so much a Data Type by when you define a variable (next lesson) and don’t assign a Data Type to it.
Bool Data Type
Data Type bool – which is just True and False (notice 1st letters are CAPS). True & False are some of a list special words – keywords. This just mean if you write this words, Python will read is you meaning a specific thing. In this case Booleans Data Type.
Strings Data Type
A more detailed look
A string is an array, or collection, of text in programming. Basically any collection of characters can be a string in programming. But, since some words (like print for example) mean something specific to the computer, for the computer to distinguish between strings and other specified words, the strings are enclosed within quotation marks.
Anything and everything can be a string if it is inside quotations, even numbers. You will recall that you have already come across strings when you wrote your first Python programme earlier.
String Built-in Functions/Methods
These functions/methods are not only used for strings. Because a string is a sequence of characters, we can use some of these functions/methods for Arrays, Lists, Tuples, all the ordered sequences (i.e. indexed).
Give example for each and then toggle
Functions
- len(varName)
- “char to add”.join(varName) Converts the elements of an iterable into a string.
Methods
- varName.capitalize() – Converts the first character to upper case.
- varName.center(length) Returns a centered string
- varName.count(“?”) Returns the number of times a specified value occurs in a string.
- varName.endswith(“?”) – Returns true if the string ends with the specified value.
- varName.find(“?”) – Searches the string for a specified value and returns the position of where it was found.
- varName.format(placeholder = value) – Formats specified values in a string.
- varName.index(“?”) – Searches the string for a specified value and returns the position of where it was found.
- varName.isalnum() – Returns True if all characters in the string are alphanumeric.
- varName.isalpha() – Returns True if all characters in the string are in the alphabet.
- varName.isascii() – Returns True if all characters in the string are ascii characters.
- varName.isdecimal() – Returns True if all characters in the string are decimals.
- varName.isdigit() – Returns True if all characters in the string are digits.
- varName.isidentifier() – Returns True if the string is an identifier.
- varName.islower() – Returns True if all characters in the string are lower case.
- varName.isupper() – Returns True if all characters in the string are upper case.
- varName.isnumeric() – Returns True if all characters in the string are numeric.
- varName.isprintable() – Returns True if all characters in the string are printable.
- varName.isspace() – Returns True if all characters in the string are whitespaces.
- varName.istitle() – Returns True if the string follows the rules of a title.
- varName.ljust(value) – Returns a left justified version of the string.
- varName.rjust() Returns a right justified version of the string.
- varName.lower() – Converts a string into lower case.
- varName.upper() – Converts a string into upper case.
- varName.strip() – Returns a trimmed version of the string.
- varName.lstrip() – Returns a left trim version of the string.
- varName.rstrip() Returns a right trim version of the string.
- varName.partition() – Returns a tuple where the string is parted into three parts.
- varName.replace(“old”, “new”) – Returns a string where a specified value is replaced with a specified value.
- varName.rfind() – Searches the string for a specified value and returns the last position of where it was found.
- varName.rindex() – Searches the string for a specified value and returns the last position of where it was found.
- varName.rpartition() – Returns a tuple where the string is parted into three parts.
- varName.rsplit() – Splits the string at the specified separator, and returns a list.
- varName.split() – Splits the string at the specified separator, and returns a list.
- varName.splitlines() – Splits the string at line breaks and returns a list.
- varName.startswith() – Returns true if the string starts with the specified value.
- varName.swapcase() – Swaps cases, lower case becomes upper case and vice versa.
Text manipulation
String Built-in Functions
We have already covered string built-in functions when we did the Mad libs game exercise. Remember that the course manual has a list of all the string built-in functions.