0% found this document useful (0 votes)
13 views36 pages

CH 02

This document serves as an introduction to Python programming, covering its history, features, and differences between Python 2 and Python 3. It provides guidance on getting started with Python, including installation, common IDEs, and running code in both interactive and script modes. Additionally, it emphasizes the importance of programming style and documentation for effective coding practices.

Uploaded by

AKRM YOUSSEF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views36 pages

CH 02

This document serves as an introduction to Python programming, covering its history, features, and differences between Python 2 and Python 3. It provides guidance on getting started with Python, including installation, common IDEs, and running code in both interactive and script modes. Additionally, it emphasizes the importance of programming style and documentation for effective coding practices.

Uploaded by

AKRM YOUSSEF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to Python and its development environment.

Computer Programming 1
Program: Intermediate Diploma in Programming and Computer Science

Course Code: APPC121

Amira Alotaibi

College: Applied College

Institution: University Of Jeddah


Outline
❑ What is Python?
❑ Python 2 vs. Python 3
❑ Getting Started with Python
❑ Anatomy of a Python Program
❑ Programming Style and Documentation
What is Python?

• Python is a general-purpose, interpreted, object-oriented programming language.

• Created by Guido van Rossum in 1990.

• Named after the comedy show Monty Python’s Flying Circus.


• Known for simple, concise, and intuitive syntax and extensive library.
General Purpose

• Python is a general-purpose programming language.


• That means you can use Python to write code for any
programming tasks.
• Python are now used in Google search engine, in mission
critical projects in NASA, in processing financial transactions at
New York Stock Exchange.
Interpreted
• Python is interpreted.
• Which means that python code is translated and executed one statement
at a time by an interpreter.
• In a compiled language, the entire source code is compiled and then
executed altogether.
Object Oriented
• Python is an object-oriented programming language.
• Data in Python are objects created from classes.
• A class is essentially a type that defines the objects of the same kind with
properties and methods for manipulating objects.
• Object-oriented programming is a powerful tool for developing reusable
software.
Python 2 vs. Python 3

• Python 3 is the modern version (not backward-compatible with Python 2).

• Python 2 is deprecated, and Python 3 is recommended for new projects.

• Key Difference: Syntax changes (e.g., print statement → print() function).


Getting Started with Python
❑ Launching Python
❑ Common Python IDEs
❑ Running Python Code
❑ Interactive vs Script Mode
❑ First Python Program
❑ Welcome With Three Messages
❑ Compute an Expression
❑ Check points
Launching Python
➢ Install Python
• Go to https://www.python.org/downloads/ and then download and install the last
version of Python 3.13.x for your operating system
• See Lab 1 for more details
Launching Python
➢ You can start Python in a command window by typing python at the command prompt

➢ You can use IDLE


Common Python IDEs:

• IDLE (built-in) – Simple, lightweight

• PyCharm – Best for professional projects

• VS Code – Lightweight, customizable

• Jupyter Notebook – Best for data science


PyCharm IDE

• An integrated development environment IDE ) is an application that provides


comprehensive facilities to programmers for software development.
• IDEs are large size programs , and many of them are not free .
• For Python programmers, PyCharm is one of the best IDE for Python .
• Also, it has a free version called Community Edition ””.
• In general, using IDEs are the best way to develop programs especially mid large
programs.
Install PyCharm
• Go to https://www.jetbrains.com/pycharm/download/ and then download and install “Community”
version.
• See Lab1 for more details.
Running Python Code
• Interactive Mode: Run commands directly in the Python shell (>>>).

• Script Mode: Save Python code in a file (.py) and run it.
Interactive Mode
• Interactive mode provides us with a quick way of running blocks or a
single line of Python code
• The code executes via the Python Shell (also known as Python Interactive
Shell), which comes with Python installation
• The >>> indicates that the Python shell is ready to execute and send your
commands to the Python interpreter
• The result is immediately displayed on the Python shell as soon as the
Python interpreter interprets the command
Script Mode

• This is the normal mode where a python code is written in a text file with a (.py ) extension,
and Python interpreter executes the file
• The result of the code will be displayed after the Python interpreter runs the file
Interactive vs Script Mode
The key differences between programming in interactive mode and
programming in script mode:
1- In script mode, a file must be created and saved before executing the
code to get results. In interactive mode, the result is returned immediately
after pressing the <enter>key from the keyboard.
2- In script mode, you are provided with a direct way of editing your code.
This is not possible in interactive mode.
First Python Program
Example Program:

# This program prints a welcome message


print("Welcome to Python!")
print("Python is fun!")

• print() function outputs text to the console.


• Comments start with # (ignored by Python).
Welcome With Three Messages

Write a program that displays Welcome to Python, Programming is fun, and Problem Driven. The
output should be as the following:

Welcome to Python
Python is fun
Problem Driven

➢ The Solution:

# Display three messages


print("Welcome to Python")
print("Python is fun")
print("Problem Driven")
Compute an Expression
10.5+2×3
Write a program that evaluates and print its result.
45−3.5

➢ The Solution:

# Compute expression
print((10.5 + 2 * 3) / (45 -3.5))

➢ The output:
0.39759036144578314
Check Point #1

Identify and fix the errors in the following code:

# Display two messages


print("Welcome to Python")
print("Python is fun").

➢Solution: The errors are the incorrect indentation in line 2, and the punctuation (.) in line 3.

# Display two messages


print("Welcome to Python")
print("Python is fun")
Check Point #2

Show the output of the following code:

print("3.5 * 4 / 2 -2.5 is")


print(3.5 * 4 / 2 -2.5)

Solution:

3.5 * 4 / 2 -2.5 is
4.5
Anatomy of a Python Program

❑ Statement
❑ Indentation
❑ Comment
❑ Special Symbols
Statement
• A statement represents an action or a sequence of actions

• The statement print("Welcome to Python") in the program is a statement to display the


greeting
“ Welcome to Python”

# Display two messages


print("Welcome to Python") Statement
print("Python is fun")
Statement
Indentation Block1
The indentation matters in Python. Block2
The following figure is a block structure visualizing indentation. Block3

Note that the statements are entered from the first column in Block2,Continuation
the new line It would cause an error if the program is typed as Block1, Continuation
Follows:

Bad code
Caution
• Don’t put any punctuation at the end of a statement.
For example, the Python interpreter will report errors for the following code:

Bad code
Note
• Python programs are case sensitive.
• It would be wrong, for example, to replace print in the program with Print.

# Display two messages


print("Welcome to Python")
Print("Python is fun")
Comments
Comment that documents what the program is and how it is constructed.
▪ Comments help programmers communicate and understand a program.
▪ They are not programming statements and thus are ignored by the interpreter.
➢ Line comment
comments are preceded by a pound sign (#) on a line.
➢ Paragraph comment
comments are enclosed between three consecutive single quotation marks (''') on one or several
lines.
examples of comments:
# This program displays Welcome to Python
''' This program displays Welcome to Python and
Python is fun
'''
Special characters
Programming Style and Documentation
❑ Programming Style
❑ Documentation
❑ Appropriate Comments and Comment Styles
❑ Proper Indentation and Spacing
Programming Style
• Programming style deals with what programs look like
• When you create programs with a professional programming style
they not only execute properly but are easy for people to read and
understand
• This is very important if other programmers will access or modify
your programs
Documentation
• Documentation is the body of explanatory remarks and comments pertaining to a program
• These remarks and comments explain various parts of the program and help others
understand its structure and function
• As you saw earlier in the chapter, remarks and comments are embedded within the
program itself, Python’s interpreter simply ignores them when the program is executed
• Good programming style and proper documentation make a program easy to read and
prevents errors
• Programming style and documentation are as important as coding In the following slides,
there are a few guidelines
Appropriate Comments and Comment Styles
• Include a summary comment at the beginning of the program to explain what the
program does, its key features and any unique techniques it uses
• In a long program, you should also include comments that introduce each major
step and explain anything that is difficult to read
• It is important to make comments concise so that they do not crowd the program
or make it difficult to read
• In homework and exams, Include your name, class section, instructor, date, and a
brief description at the beginning of the program
Proper Spacing
• A consistent spacing style makes programs clear and easy to read, debug and maintain .
• Use blank line to separate segments of the code.

print(20+50) print(20 + 50)


print(20- 10) print(20 - 10)
print(60*5+30) print(60 * 5 + 30)
print("A","B") print("A" , "B")

You might also like