Class3 (BSD)
Class3 (BSD)
accuracy
o Learn what Python syntax is and how to use it to write basic instructions for
the computer.
o Explore the concept of the interactive shell (REPL) and how it helps you test
and experiment with Python commands in real-time.
o Understand what expressions are and how they combine values and
operators to produce a single result.
o Explore common operators (+, -, *, /, **, %, and //) and learn how to use
them in Python programs.
o Use the + operator for string concatenation to combine multiple strings into
one.
o Use the * operator for string replication to repeat strings multiple times.
o Recognize and handle errors when using operators with incompatible data
types.
By the end of this class, you’ll have practical skills to write basic Python commands,
experiment with data types, and manipulate strings effectively.
Before moving ahead, let’s review the key topics we studied in the last class to solidify our
understanding:
1. What Is Programming?
• Key Ideas:
2. What Is Python?
• Definition: Python is a programming language with simple syntax (rules) that makes
it easy to learn and use.
• Why Python?
o It’s widely used for web development, data analysis, AI, and more.
• Interesting Fact: Python is named after the comedy group Monty Python, not the
snake.
3. Installing Python and Mu Editor
• Python Installation:
o Python’s software (the interpreter) translates your instructions into tasks the
computer understands.
• Mu Editor:
o Why Mu? It simplifies coding and debugging, making it ideal for learners.
• Key Point: Programming is more about solving problems and logic than doing
complex math.
• What We Learned:
o Share your code and error messages using tools like Pastebin.
Python Basics
The Python programming language has a wide range of syntactical constructions, standard
library functions, and interactive development environment features. Fortunately, you can
ignore most of that; you just need to learn enough to write some handy little programs
Okay, imagine you want to teach a pet dog some cool tricks. You can't just shout random
things; you need to use specific commands they understand, right? Like 'sit,' 'stay,' 'fetch.'
Python, the programming language, is like that. It has its own special set of commands and
ways of writing things – we call that syntax (like the rules for writing commands).
Python has many cool features, but you don't need to know everything to start doing fun
stuff. Think of it like you don't need to know everything about football to have a good game
with your friends in the park. You just need to know the basic rules.
So, we'll learn some of these basic programming rules and ideas. These can be a bit tricky at
first, like learning a new game, but once you get the hang of it, you can make the computer
do amazing things – almost like magic!
We're going to use something called the interactive shell. It's like a little playground where
you type in commands, and Python immediately does them and shows you the result. It’s
very helpful for experimenting with these basic commands and observing what happens. So
instead of just reading about it, we are actually going to do it to learn faster.
So, remember, we're going to learn just enough basic Python to build cool stuff. It's going to
be like learning a new skill slowly, step by step and we are going to be experimenting with
Python so we learn faster than reading about it only
REPL (Read-Evaluate-Print Loop) has the interactive shell, which lets you run (or execute)
Python instructions one at a time and instantly shows you the results. Using the interactive
shell is great for learning what basic Python instructions do, so give it a try as you follow
along. You’ll remember the things you do much better than the things you only read.
Entering Expressions into the Interactive Shell
The interactive shell is a tool that allows you to type Python instructions one at a time and
see the results instantly. This is an excellent way to learn Python and experiment with its
features.
o Windows: Open the Start menu, type “Mu,” and select the app.
o Click the New button and save the empty file as blank.py.
o The interactive shell will appear as a new pane at the bottom of the Mu
editor window with a >>> prompt.
Enter 2 + 2 at the prompt to have Python do some simple math. The Mu window should now
look like this:
>>> 2 + 2
4 >>>
Expressions
In Python, 2 + 2 is called an expression, which is the most basic kind of programming
instruction in the language. Expressions consist of values (such as 2) and operators (such as
+), and they can always evaluate (that is, reduce) down to a single value. That means you can
use expressions any where in Python code that you could also use a value.
In the previous example, 2 + 2 is evaluated down to a single value, 4. A single value with no
operators is also considered an expression, though it evaluates only to itself, as shown here:
>>> 2
It consists of:
>>>
You can use plenty of other operators in Python expressions, too. For example
>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30
28093077826734
>>> 2 ** 8
256
>>> 23 / 7
3.2857142857142856
>>> 23 // 7
>>> 23 % 7
>>> 2 + 2
16.0
In each case, you as the programmer must enter the expression, but Python does the hard
part of evaluating it down to a single value. Python will keep evaluating parts of the
expression until it becomes a single value, as shown here:
These rules for putting operators and values together to form expressions are a fundamental
part of Python as a programming language, just like the grammar rules that help us
communicate. Here’s an example:
The second line is difficult to parse because it doesn’t follow the rules of English. Similarly, if
you enter a bad Python instruction, Python won’t be able to understand it and will display a
SyntaxError error message, as shown here
You can always test to see whether an instruction works by entering it into the interactive
shell. Don’t worry about breaking the computer: the worst that could happen is that Python
responds with an error message. Professional software developers get error messages while
writing code all the time.
Key Takeaway
• Errors like SyntaxError are common and part of the coding process.
• Testing expressions in the interactive shell allows you to identify mistakes instantly.
• Mistakes won't harm your computer; at most, you'll receive an error message to help
you debug.
If you ever see the error message SyntaxError: EOL while scanning string literal, you
probably forgot the final single quote character at the end of the string,
String Concatenation
• What is it? Concatenation combines two strings into one continuous string.
• Operator: +
• Example:
If you try to concatenate a string with a non-string (like an integer), Python raises an error:
To fix this, you can explicitly convert the integer to a string using str():
String Replication
• What is it? Replication repeats a string multiple times.
• Operator: *
• Example:
Rules:
The expression evaluates down to a single, new string value that combines the text of the
two strings. However, if you try to use the + operator on a string and an integer value,
Python will not know how to handle this, and it will display an error message.
>>> 'Alice' + 42
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
'Alice' + 42
TypeError: can only concatenate str (not "int") to str
The error message can only concatenate(link) str (not "int") to str means that Python
thought you were trying to concatenate an integer to the string 'Alice'. If you want it to run,
Your code will have to explicitly convert the integer to a string because Python cannot do
this automatically. (Converting data types is an awesome thing and we will learn about it in
our next classes when we talk about the str(), int(), and float() functions.)
The * operator multiplies two integer or floating-point values. But when the * operator is
used on one string value and one integer value, it becomes the string replication operator.
Enter a string multiplied by a number into the interactive shell to see this in action.
>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
The expression evaluates down to a single string value that repeats the original string a
number of times equal to the integer value. String replication is a useful trick, but it’s not
used as often as string concatenation. The * operator can be used with only two numeric
values (for multiplication), or one string value and one integer value (for string replication).
Otherwise, Python will just display an error message, like the following:
It makes sense that Python wouldn’t understand these expressions: you can’t multiply two
words, and it’s hard to replicate an arbitrary string a fractional number of times.
1. Instant Feedback
o The interactive shell allows you to type Python instructions and see the
results immediately.
o This instant feedback helps you quickly understand how Python works.
2. Experimentation
o It’s perfect for trying out small pieces of code or testing how a specific
function or operation behaves.
o You don’t need to write an entire program—just type a command and see the
output.
o For beginners, the shell is like a playground where you can learn Python’s
syntax and features step by step.
o It helps you memorize concepts better because you’re actively doing, not just
reading.
o When you encounter an error in your code, you can test individual lines or
commands in the shell to pinpoint where the issue lies.
6. Improved Workflow
o For larger programs, you can use the interactive shell to test small
components before adding them to your script, saving time and effort.
In the next class, we'll explore how to store values in variables and use
assignment statements. You'll learn how variables can hold different types of
data and how to update or overwrite values in them. We'll also cover the rules
for naming variables, ensuring your code stays organized and readable. Finally,
we'll go over the basics of writing and running your first Python program, with
an emphasis on using functions like print() and input() for interaction. You'll get
hands-on experience working with variables, expressions, and simple user
input!