Python First Program
Python First Program
IDLE
Interactive Script
Python shell Window
Interactive Python Shell
The first line tells you what version of Python is running. In this case, IDLE is running
Python 3.9.13. The second and third lines give some information about the operating
system and some commands you can use to get more information about Python. The
>>> symbol in the last line is called the prompt.
Script Window
Interactive window - Sequence of events
1. First, Python reads the code entered at the prompt.
2. Then the code is evaluated.
3. The output is printed in the window and a new prompt is
displayed.
• For information:
• This loop is commonly referred to as a Read-Evaluate-Print Loop, or
REPL. Python programmers sometimes refer the Python shell as a
“Python REPL”, or just “the REPL” for short.
The “Hello World”
• Run the following command : Print (“Hello World”)
• To print text to the screen in Python, you use the print( ) function. A
function is a bit of code that typically takes some input, called an
argument, does something with that input, and produces some
output, called the return value.
Syntax Run-time
Error Error
Syntax Errors
• In loose terms, a syntax error occurs when you write some code that isn’t allowed in
the Python language. You can create a syntax error by changing the contents of the
hello_world.py script from the last section to the following :
• In this example, the double quotation mark at the end of "Hello, world“ has been
removed. Python won’t be able to tell where the string of text ends. Save the altered
script and then try to run it. What happens? The code won’t run! IDLE displays an alert
box with the following message:
• EOL stands for End Of Line, so this message tells you that Python read all the way to the
end of the line without finding the end of something called a string literal. A string
literal is text contained in-between two double quotation marks. The text "Hello,
world" is an example of a string literal.
• Back in the script window, notice that the line containing with "Hello, world is
highlighted in red. This handy features helps you quickly find which line of code caused
the syntax error
Run-time Errors
• IDLE catches syntax errors before a program starts running, but some
errors can’t be caught until a program is executed. These errors are known
as run-time errors because they only occur at the time that
a program is run.
• Let us generate a run-time error - change the code in hello_world.py to the
following: print(Hello, world)
Error?
• Arithmetic + - * // / % **
• Comparison == != > < >= <=
• Assignment = += -= *= //= /= %= **=
• Logical and or not
• Bitwise
& | ^ ~ >> <<
• Membership
in not in
• Identity
is is not
Variables
• Variables are names that can be assigned a value and used to reference that
value throughout your code. Variables are fundamental to programming for
two reasons
• Variables keep values accessible: the result of some time-consuming
operation can be assigned to a variable so that the operation does not need to
be performed each time you need to use the result.
• Variables give values context: The number 28 could mean lots of different
things, such as the number of students in a class, or the number of times a user
has accessed a website, and so on. Naming the value 28 something like
num_students makes the meaning of the value clear.
• Values are assigned to a variable using a special symbol = called the
assignment operator
• Variable names are case-sensitive, so a variable named phrase is distinct from
a variable named Phrase
Variables continued
• Rules for Valid Variable Names :
Variable names can be as long or as short as you like, but there are few rules. Variable names
can only contain uppercase and lowercase letters (A–Z, a–z), digits (0–9), and underscores (_).
However, variable names cannot begin with a digit .
• For example, phrase, string1, _a1p4a, and list_of_names are all valid variable names, but 9lives
is not.
• Descriptive Names Are Better Than Short Names For Clarity in Code
e.g. seconds_per_hour = 3600 is better than seconds = 3600 or s = 3600
• Python Variable Naming Conventions
In many programming languages, it is common to write variable names in camelCase like
numStudents and listOfNames
In Python, however, it is more common to write variable names in snake case like
num_students and list_of_names. Every letter is owercase, and each word is separated by an
underscore
• Note: The Style Guide for Python Code – PEP8 (Python Enhancement Proposals 8)
Assignment Statement
+ Addition 9+2 is 11
9.1+2.0 is 11.1
- Subtraction 9-2 is 7
9.1-2.0 is 7.1
* Multiplication 9*2 is 18
9.1*2.0 is 18.2
/ Division 9/2 is 4.25 In Python3
9.1/2.0 is 4.55 Real div.
// Integer 9//2 is 4
Division
% Remainder 9%2 is 1
The // operator
• Also referred to as “integer division”
• Result is a whole integer (floor of real division)
• But the type need not be int
• the integral part of the real division
• rounded towards minus infinity
• Examples
9//4 is 2 (-1)//2 is -1 (-1)//(-2) is 0
1//2 is 0 1//(-2) is -1 9//4.5 is 2.0
The % operator
• The remainder operator % returns the
remainder of the result of dividing its first
operand by its second.
Ideally: x == (x//y)*y + x %y
Quick Recap – Hands on Exercises