0% found this document useful (0 votes)
6 views

Class3 (BSD)

This document introduces the basics of programming in Python, covering syntax, expressions, operators, data types, and string manipulation. It emphasizes the use of the interactive shell for real-time experimentation and learning, while also reviewing previous lessons on programming concepts. By the end of the class, learners will have practical skills to write basic Python commands and manipulate data effectively.

Uploaded by

uzairznkhattak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Class3 (BSD)

This document introduces the basics of programming in Python, covering syntax, expressions, operators, data types, and string manipulation. It emphasizes the use of the interactive shell for real-time experimentation and learning, while also reviewing previous lessons on programming concepts. By the end of the class, learners will have practical skills to write basic Python commands and manipulate data effectively.

Uploaded by

uzairznkhattak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Class 3 introduction to Programming in Python And a little touch to typing

accuracy

What You’ll Learn After Reading This

After completing this lesson, you will:

1. Understand Python Basics

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.

2. Work with Expressions and Operators

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.

3. Discover the Order of Operations

o Learn how Python evaluates complex mathematical expressions using


standard order of operations rules.

4. Learn About Data Types

o Differentiate between the three most common data types in Python:

▪ Integers (int) for whole numbers.

▪ Floating-point numbers (float) for numbers with decimal points.

▪ Strings (str) for text values.

5. Explore String Manipulation

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.

Class 3: Review of Previous Lessons

Before moving ahead, let’s review the key topics we studied in the last class to solidify our
understanding:

1. What Is Programming?

• Definition: Programming is about giving instructions to a computer to perform tasks,


like solving problems, finding information, or making games.

• Key Ideas:

o Computers follow instructions step by step.

o Programming involves creating logical flows like:

▪ "Do this, then do that."

▪ "If this happens, do this; otherwise, do something else."

▪ "Repeat this task X number of times."

o Python is a beginner-friendly language for writing such instructions.

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.

o It’s accessible on any operating system: Windows, macOS, or Linux.

• Interesting Fact: Python is named after the comedy group Monty Python, not the
snake.
3. Installing Python and Mu Editor

• Python Installation:

o We installed Python 3.13.1 to set up the programming environment.

o Python’s software (the interpreter) translates your instructions into tasks the
computer understands.

• Mu Editor:

o A beginner-friendly tool for writing and running Python programs.

o Why Mu? It simplifies coding and debugging, making it ideal for learners.

4. Programming Doesn’t Require Much Math

• Key Point: Programming is more about solving problems and logic than doing
complex math.

• Example: Solving a Sudoku puzzle is like programming—breaking down a problem


into small steps to find a solution.

5. Debugging Errors. (finding and fixing errors or bugs in source code )

• What We Learned:

o Errors are normal in programming.

o Understanding error messages helps fix problems.

o Searching online for solutions to error messages is a valuable skill.

o You can search at Github, Reddit.com/r/programming


6. How to Ask Smart Programming Questions

• Tips for Effective Questions:

o Clearly explain your goal and problem.

o Share your code and error messages using tools like Pastebin.

o Mention your Python version and operating system.

o Be polite and respectful when seeking help.

Starting with our 3rd Class

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

Syntactical constructions = Syntactic construction in programming refers to the specific way


you arrange keywords, symbols, and identifiers (like variable names) to form valid code that
the computer can understand.

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.

How to Run the Interactive Shell

To start the interactive shell, follow these steps:

1. Open the Mu Editor:

o Windows: Open the Start menu, type “Mu,” and select the app.

o macOS: Go to the Applications folder and double-click the Mu app.

2. Create a New File:

o Click the New button and save the empty file as blank.py.

3. Run the File:

o Click the Run button or press F5.

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

An expression is the simplest type of instruction in Python.

It consists of:

• Values: Numbers, text, or other data (e.g., 2, 4, 'Hello').

• Operators: Symbols that perform operations (e.g., +, -, *, /).

Expressions evaluate to a single value.

>>>
You can use plenty of other operators in Python expressions, too. For example

Operator Operation Example Evaluates to . . .


** Exponent (power 2) 2 ** 3 8
% Modulus/remainder 22 % 8 6
// Integer division 22 // 8 2
/floored quotient
/ Division 22 / 8 2.75
* Multiplication 3*5 15
- Subtraction 5-2 3
+ Addition 2+2 4
The order of operations (also called precedence) of Python math operators is similar to that
of mathematics. The **(exponent) operator is evaluated first; the *, /, //, and % operators
are evaluated next, from left to right; and the + and - operators are evaluated last (also from
left to right). You can use parentheses() to override the usual precedence if you need to.
Whitespace in between the operators and values doesn’t matter for Python (except for the
indentation at the beginning of the line), but a single space is convention.

Enter the following expressions into the interactive shell:

>>> 2 + 3 * 6

20

>>> (2 + 3) * 6

30

>>> 48565878 * 578453

28093077826734

>>> 2 ** 8

256

>>> 23 / 7

3.2857142857142856

>>> 23 // 7

>>> 23 % 7

>>> 2 + 2

>>> (5 - 1) * ((7 + 1) / (3 - 1))

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:

This is a grammatically correct English sentence.

This grammatically is sentence not English correct a.

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.

The Integer, Floating-Point, and String Data Types


Remember that expressions are just values combined with operators, and they always
evaluate down to a single value. A data type is a category for values, and every value belongs
to exactly one data type. The most common data types in Python are listed in Table table
following. The values -2 and 30, for example, are said to be integer values. The integer (or
int) data type indicates values that are whole numbers. Numbers with a decimal point, such
as 3.14, are called floating-point numbers (or floats). Note that even though the value 42 is
an integer, the value 42.0 would be a floating-point number.

Data type Examples


Integers (int) -2, -1, 0, 1, 2, 3, 4, 5
Floating-point numbers (floats) -1.25, -1.0, -0.5, 0.0, 0.5, 1.0, 1.25
Strings 'a', 'aa', 'aaa', 'Hello!', '11 cats'
Python programs can also have text values called strings, or strs (pro nounced “stirs”).
Always surround your string in single quote (') characters (as in 'Hello' or 'Goodbye cruel
world!').. so Python knows where the string begins and ends. You can even have a string
with no characters in it, ' ', called a blank string or an empty string. We are going to study
about String more in later classes as its a little controversial topic.

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,

such as in this example:

String Concatenation
• What is it? Concatenation combines two strings into one continuous string.

• Operator: +

• Example:

The + operator joins the two strings.

Error with Different Data Types:

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:

The string 'Alice' is repeated 5 times.

Rules:

• The * operator works only with:

1. A string and an integer (e.g., 'Alice' * 5).

2. Two numeric values (for multiplication).

Python doesn’t allow:

▪ Multiplying two strings (e.g., 'Alice' * 'Bob').

▪ Replicating a string a fractional number of times (e.g., 'Alice' * 5.0).

String Concatenation and Replication ( in general)


The meaning of an operator may change based on the data types of the values next to it. For
example, + is the addition operator when it operates on two integers or floating-point
values. However, when + is used on two string values, it joins the strings as the string
concatenation operator.

Enter the following into the interactive shell:

>>> 'Alice' + 'Bob'


'AliceBob'

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.

Some thing to know about interactive shell:


Why Use the Interactive Shell?

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.

3. Learning and Practice

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.

4. Debugging Made Simple

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.

o This makes fixing errors faster and easier.

5. Quick Calculations and Functions


o You can use the shell as a calculator or for running small snippets of Python
code, like checking logic, formulas, or expressions.

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!

You might also like