Binary Tree
Digital Literacy
Week 4
Basic Programming Fundamentals (Python)
Basic Fundamentals
In Python we use many different methodologies and logic to make things happen. First we can start out with one of the most useful features of Python: printing code to the console.
Try it for yourself!
Booleans
Booleans are simple and easy to use concepts in programming. A boolean represents an idea of “true” or “false.” While writing any program, we often want to execute different code in different situations. Booleans help our code to do just that easy and effective. More often, a boolean value is returned as a result of some kind of comparison operations.
There are 2 important keywords: True and False
Booleans In Action
Variables
Variables are an important part of coding. Variables allow us to store both individual data or large amounts of it. Variables also have different types. For instance for words are strings, numbers are integers and finally come lists which have more than one value.
Operators
Operators are special symbols in Python that are used to perform arithmetic or logical computations. The values on which operation is to be done are called operands, while the operation is denoted by its operator (eg. +, -, /, *, %, etc.) There is a list of some common operators:
Repeated Actions
We can perform actions over and over again in Python. To do this we can use loops. Some common loops are for loops and while.
Conditionals in Python
Conditionals in Python
The code above sets x as a random integer between 0 and 10. If x is equal to 7 when the code is run, then “hi!” will be printed in the console. Otherwise, nothing will happen.
In this case, x = 7, so ‘hi!’ was printed.
In this case, x was not equal to �7, so nothing happened.
Conditionals in Python
In this case, x was not equal to �7, meaning the first condition was false. However, x was equal to 4, indicating that the elif condition was true and so it ran.
Conditionals in Python
Conditionals in Python
In the code above, the program first checks if x is equal to 7. If it is, then it will “print” hello and stop. However, if x is not equal to 7, then the program will check the else condition and print “?”
As we can see, the randomly selected integer x was not 7, because the output of this program was “?”
Mini Activity
Mini Activity
Search for Relevant Code
Identify the Purpose
Break Down the Logic
Evaluate the Operators
Navigating Github!
(This is an in-lesson activity)
Share screen and explore code snippets on Github!