A Lesson on Python Basics
By Yasmin Evans and Hongxi Li
Getting Started
As technology continues to creep ever more into our daily lives and workflows, learning to code has become a workplace and leisure goal for many. Coding allows those proficient in it to create scripts for anything from daily schedule rituals to controlling robots and taking heart monitor calculations, its versatility and variability is what generates so much value for its users and is what continues to drive people’s desire to learn the skill.
What is Python? And Why Should I Learn It?
Python is a general use coding language like C and Java. It has an extensive library of importable functions and scripts which have given it many development capabilities in things that would have otherwise required multiple languages to create. Unlike Java and C which also have extensive libraries, Python’s simplicity and learnability have catapulted it into being one of the most popular programming languages among beginners and fully fledged developers alike. By learning Python you will be grounding yourself in coding basics through the use of its simplistic coding syntax and rules, while also setting yourself up to learn more advanced coding languages through its transferable logic.Â
Using Python
To get started with Python, first you need to make sure you have the correct environment for the coding language. Python requires the Python.org application files to be present on a system before code can be written or run, although due to pythons versatility applications which host python and its many external libraries such as Anaconda can also be downloaded in lue of only the python application.
Python.org python Download page
Using the Python.org website, select the correct Python download based on your system.Â
or
Download Anaconda based on your computer specs
Anaconda Download page
Once python has been installed in one of the two above ways, you will need to download a text editor so you can write and save your code. Text editors that work with python include: VSStudio Code, Wing, Notepad++ and Atom.Â
Create documents using the text editors create text file option, usually located in the top left hand corner regardless of application. Always be sure to save python files with a .py at the end so they compile correctly.
Alt text: Image of file explorer on Windows 11 machine
Python does not require the use of a manual compiler command, using either the editors terminal or your computer’s CMD (while inside the file containing your .py file with the cd command) run your code using the command “python filename.py”.Â
Alt text: Image of python code being run in VSStudio Code
Hopefully, you’ve downloaded a suitable editor, so let’s try using it!
1. Open the editor (Wing as an example).
2. Click on the link below for some test code.
3. Type the test code into your text editor.
4. See if the output of your input code is the same as ours.
Alt text: Python Fiddle application with test code
Alt text: Wing Text Editor with test code
It’s perfectly ok if you don’t get the answer you were hoping for, relax, our learning has just begun.
Python Syntax
Python, like any other coding language, requires specific formatting and handling rules be followed in order for its code to run as intended, these formatting and handling rules are termed syntax in the coding world. Python’s coding syntax rules are generally straightforward and can be split into two categories, formatting and assignment.
Formatting:
- Indentation: Indentation is required to tell blocks of code apart from one another. If indentation is skipped the file will not compile properly, and will not run as a result. Use the tab function to create indentation for arguments inside of arguments.
Alt text: image showing an indented sentence
If, Else, While and For statements require that before an indented internal block of code is created below them they end in a “:”.
Alt text: image showing a while block example with an ending”:”
- Statements: Tying into indentation, when code statements in this language come to an end, a new coding statement must be created elsewhere on the document, single line programs are not feasible using python code.Â
Alt text: image showing several lines of independent code
- Commenting: The # sign is used for commenting code blocks. For any code you do not wish to run upon compilation, using the # sign before it will ensure it is ignored by the compiler. Normally, comments are placed in spots to define a function of concept to a developer who may not understand the flow of the code from a glance.
Alt text: image showing a comment with the use of #
Test your understanding of Python syntax!
Assignment:
The main form of Assignment in python is done through Pythonic variables using the “=” sign to assign a value of some sort to a variable (usually a single letter or word/words) to be utilized in the code blocks below it. Occasionally, assignments can also be done through the use of libraries and clever coding implementations, but we will not be going over such advanced topics in this lesson.
To start, we must first create the variables which we wish to assign values to. Variable names cannot contain any spaces, furthermore it is generally discouraged to include special characters such as !@#..ect, as they may confuse the compiler since these characters can also be used for calculation purposes. Naming conventions are a topic in of themselves as many programmers opt to name variables differently depending on their use in the program, for the uninitiated, as long as a single naming convention is used throughout for the base variable types there is no issue, such naming conventions include:
- snake_case: Where words are separated using _’s
Alt text: Snake Case example
- camelCase: Where the first word is lowercase and words after it are upper case
Alt text: Camel Case example
- PascalCase: (the most frequently used case) Where all new words are differentiated using uppercase letters.Â
Alt text: Pascal Case example
Test your understanding of the types of naming conventions!
There are MANY values that can be assigned to variables, including the natural pythonic variables, which are found in the basic python coding language, as well as variable types that can be imported using libraries. For the sake of this intro to python we will focus on the most basic natural pythonic variables that require no imported libraries or the use of classes to utilize as shown in the below infographic:
Alt-text Infographic about python primitive types: This infographic talks about the 4 key types of primitives used in python, from top to bottom integers, floats, strings and bools.
Test your understanding of the natural python types!
Common Python Functions and Operators
As mentioned in the intro paragraph, python is an incredibly versatile language that hosts many natural functions as well as functions that can be imported using libraries. The most utilized functions in all high level coding languages revolve around the core 4; Printing, If-Else logic, While Loops and For loops. For the sake of brevity, and because these functions are utilized in basically all code between beginner and advanced level understandings, we will be covering these core functions only. In general, before using one of Python’s natural functions, defining a function name to hold the natural function is preferred, to do this use”def functionName()”, in the below examples we use def code() when necessary.
Operators are required to perform operations so the code can evaluate outcomes, they follow the same rules as basic arithmetic. Such operators include the +, – and = signs which mean exactly the same as their arithmetic counterparts.
Print Function: The print function takes input directly from the coder and prints said input to the console (or terminal) after the program has been compiled and run, an example of the print statement and it’s terminal output can be seen in the below screenshot:
Alt text: image of a terminal running the print statement “Cats are my favorite animal”
If-Else Logic: If proceeds a statement that will only run its indented code if the statement is true, else can only be added at the end of an If statement to cover the cases that did not return true. An example of an If-Else statement can be seen in the below screenshot:
Alt text: image of an If Else logic code block
While Loops: While loops are snippets of code that run on repeat until a certain condition has been met, they require that the condition be worked towards each time the while loop repeats such that an infinite loop is not caused, below is a screenshot showing a basic While Loop structure.Â
Alt text: image of a While loop code block
For Loops: For loops work a lot like while loops, but rather then having their condition being updated using a variable that the loop is working with, external variables like i, j and k are used instead to dictate exactly how many repeats the loop has until it finishes, an example of a for loop can be seen in the below screenshot.Â
Alt text: image of a For loop code block
Basic Python Project
Now that you have a general base for how to code in python, below are a few coding exercises meant to strengthen your understanding of the content and test your learning thus far. Please complete one of the exercises using your text-editor and knowledge from this lesson, then submit your work to brightspace as a .py file.
Calculate the sum of numbers 1 to 100:Â Write and run a python script which calculates the sum of the values 1 to 100 like follows: 1+2+3+4+…+99+100
Or
The Iterative Stars Project:Â Using for loops, given a number of rows, create a pyramid of stars with that number of rows, but in each row an additional star appears, example output is shown below for the number 11 as input:Â
OUTPUT:
x
x x
x x x
x x x x
x x x x x
x x x x x x
x x x x x x x
x x x x x x x x
x x x x x x x x x
x x x x x x x x x x
Further learning
If you want to learn more about python, here are a few sites we recommend for beginners to continue on their python learning journey.
1. Python Tutorial (w3schools.com)
W3Schools offers an extensive Python tutorial tailored for beginners. It covers fundamental aspects such as syntax, data types, control flow, functions, modules, and object-oriented programming. The platform enhances learning through interactive exercises and examples, allowing beginners to grasp Python programming intuitively. We recommend it because its layout is clear and easy to understand, and the built-in Python editor is easy for newcomers to get started, also it is totally free. The only problem may be that it is only suitable for beginners, the content is relatively basic, and it is less helpful for further learning.
Alt text: W3 School Python Programming Data Types Page
2. Practice PythonÂ
Practice Python is also a Python learning platform that offers more than 40 different Python exercises, and each exercise includes a question and the solution. The advantage of this website is that it provides a large number of Python exercises to help users apply what they have learned to real-world scenarios, thereby deepening their understanding and memory of Python programming concepts. It’s also completely free! It should be noted that this website only provides practice questions and answers, and there is no tutorial on related knowledge. It is suitable for those who have learned part of Python and want to practice.
Alt text: Practice Python Exercise 7 page
3.Python Exercises – Python Tutorial (pythonbasics.org)
Python basics is more balanced than the previous two sites, it provides an explanation of Python knowledge while ensuring practice content. It also happens to be free.
These sites cover almost all the common Python knowledge and are perfect for beginners, so if you can’t wait, then start exploring them!
Methods for Successfully Learning More Python
1. Keep learning and practice in sync:Â It is not enough to just read the tutorial, what is important is a lot of practice and application. It is recommended that you start practicing a subject as soon as you have finished studying it.
2. Find yourself a hobby: We recommend you find a project that interests you so you can learn and solve problems by doing.
3. Join the community:Â Learning to program is not a lonely endeavor, and you can join Python-related forums or communities to exchange ideas with other learners.
4. Continuous learning: Programming is a continuous learning process, and continuous learning is very important as technology is updated and iterated quite quickly.
Reflection:
The general outline for this lesson plan as well as the content in the sections Getting Started, What is Python? And Why Should I Learn It?, Using Python (up until the sub-section describing using downloaded applications to ensure they work correctly), Python Syntax, Common Python Functions, Basic Python Project and the HP5 syntax quiz as well as the HP5 formatting quiz were created by Yasmin. The infographic of Python Primitive Types, quiz for the infographic made using H5P, Further learning section, methods for successfully learning more python section and the portion of Using Python content describing using the coding applications to ensure that they work correctly were created by Hnasey.
We created our lesson layout based on the learning objectives and constructive alignment lesson plan template found in Module 4, our lesson plan can be found in the chart below. Constructive alignment and backwards design greatly influenced our current lesson plan layout. We knew we wanted students to complete a coding assignment at the end of of the lesson as that would be the best method of evaluating whether students had the base we had envisioned for them in Python, which was our ultimate learning goal. Constructive alignment for us meant making sure each learning activity was made to move our students towards understanding all the smaller concepts in Python so they could ultimately create what was assigned at the end and prove their knowledge.
Big Idea What is the big idea that the learner will walk away with at the end of the lesson that is critical for learners at this stage of their learning path? | Learning Outcome(s) What specific things will the learner know or be able to do by the end of the lesson? | Evidence of Learning What does learning look like for this objective? (e.g., accurate performance of a task, correct use of terminology) | Assessments What will learners do to provide evidence of their learning? (e.g., a presentation, a test, a project) | Learning Activities What learning activities will allow learners to acquire and practice the skills necessary to demonstrate their learning and complete the assessment successfully? |
Since this is a total beginners intro to Python, we want the learner to walk away with a strong core foundation in the Python coding language, such that they will be able to build on this information easily if they decide to further their knowledge of the language. | Know why learning python is valuable as a skill Understand basic pythonic syntax Understand basic pythonic variables and variable allocation Be able to create simple programs to solve entry level coding problems | Correct understanding of syntax and variable allocation in assigned python coding task Accurate understanding of how functions in assigned work. | Create and submit a .py document with their solutions to the assignment questions. Get a score of 5 or higher across the HP5 quiz’s. |  Write code in to solve the provided assignment questions. Test their understanding of using the HP5 quizzes provided. |
We used multiple types of media when creating this lesson plan, we had WordPress HP5 content to test students in multiple areas including HP5 tests for Variables, Syntax and Naming Conventions. The layout of this HP5 content varied from drag and drop to matching words to their definitions as we didn’t want the material to become repetitive for students. We included a Canva made infographic to teach students the different types of variables prior to the related HP5 testing material, when creating this infographic we focused on alignment, proximity and color so as to best hold the attention of our students. Regarding external media, we decided to incorporate Python-Fiddle into the lesson. This site allowed us to create practice pieces of code without making students download .py files to run, rather all they needed to do was open the web to view the code and create based on our test code. Finally, but still importantly, we included many screenshots and images to hopefully assist the students in following along with the content.Â
In general, during the creation of our lesson plan we made sure to focus on including the following principles, theories, and techniques such that students would be able to get the most out of learning the basics of Python! We attempted to stay on topic and generate a cohesive flow throughout the lesson in an attempt to halt our students from getting cognitively overloaded with unnecessary information. This meant using signaling and segmenting on content as well as consistently placing like content next to like content for the sake of overall contiguity. We found that our lesson content was very writing heavy which made the intrinsic cognitive load hard to balance at first, we settled on breaking up the concepts with related images and activities as recommended by the dual coding theory in order to soften the intrinsic load on students. Dual coding also allowed us to pre-train students for upcoming related HP5 activities which were not only used to segment content, but were also aimed at creating an active learning environment for students. Extraneous load was avoided by actively avoiding repetition in text and images to the best of our ability. We’re hoping that a balanced germane load was created through our efforts to incorporate these theories and principles.
For the sake of accessibility and representation we ensured that all images had an alt text. After completing our lesson plan we ran a WAVE Accessibility check on the content and found that it passed the check after minor alterations.
For the most part the content in this lesson plan comes from our collective knowledge as computer science students about the subject of coding in Python, however the following sites and sources were used to enrich our understanding of the topic, were mentioned during the lesson or were used as external media:Â
Written Content Sources:
Rossum, G. V., Warsaw, B., & Coghlan, A. (2013, August 1). PEP 8 – style guide for python code. Python Enhancement Proposals (PEPs). https://peps.python.org/pep-0008/#naming-conventions
Websites mentioned in images/content not otherwise hyperlinked:
Recent Comments