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

Python Basics

Python Language Lab 1 provides an overview of Python including its history, installing and running Python, names and assignment, operations, and mutability. Python was invented in the 1990s in the Netherlands by Guido van Rossum and named after Monty Python. It is an open source, scalable, object-oriented and functional language used by Google from the beginning. The document discusses installing Python, running it interactively from the command line, variables, arithmetic operations, precedence, comments, printing, user input, comparison operators, assignment, and accessing names.

Uploaded by

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

Python Basics

Python Language Lab 1 provides an overview of Python including its history, installing and running Python, names and assignment, operations, and mutability. Python was invented in the 1990s in the Netherlands by Guido van Rossum and named after Monty Python. It is an open source, scalable, object-oriented and functional language used by Google from the beginning. The document discusses installing Python, running it interactively from the command line, variables, arithmetic operations, precedence, comments, printing, user input, comparison operators, assignment, and accessing names.

Uploaded by

mohammed sallam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Python Language Lab 1

Overview
• History
• Installing & Running Python
• Names & Assignment
• Python Operations
• Mutability
Brief History of Python
• Invented in the Netherlands, early 90s by Guido van Rossum
• Named after Monty Python
• Open sourced from the beginning
• Considered a scripting language, but is much more
• Scalable, object oriented and functional from the beginning
• Used by Google from the beginning
• Increasingly popular
Python’s Benevolent Dictator For Life

“Python is an experiment in
how much freedom program-
mers need. Too much freedom
and nobody can read another's
code; too little and expressive-
ness is endangered.”
- Guido van Rossum
http://docs.python.org/
The Python tutorial is good!
Running Python
Installing
• Python is pre-installed on most Unix systems,
including Linux and MAC OS X
• The pre-installed version may not be the most
recent one (2.6.2 and 3.1.1 as of Sept 09)
• Download from http://python.org/download/
• Python comes with a large library of standard
modules
• There are several options for an IDE
• IDLE – works well with Windows
• Eclipse with Pydev (http://pydev.sourceforge.net/)
IDE Integrated Development Environment

• IDLE is an Integrated DeveLopment Environ-ment for Python, typically


used on Windows
• Multi-window text editor with syntax highlighting, auto-completion,
smart indent and other.
• Python shell with syntax highlighting.
• Integrated debugger
with stepping, persis-
tent breakpoints,
and call stack visi-
bility
Running Interactively Command Line
On Unix/windows/Mac…
% python
>>> 3+3
6
• Python prompts with ‘>>>’.
• To exit Python (not Idle):
• In Windows, type CONTROL-Z + <Enter>
• Evaluate exit()
Example ‘script’: fileName.py
Compiled Languages Vs. Interpreted Languages
Variables or Identifiers
Naming Conventions
- Variables should be written in lowercase
- age = 42
- Multiple word names should be separated by underscore
- first_name = ‘Ahmed’
- Constants identifiers should be written in uppercase letters.
- PI = 3.14
- Do NOT use reserved words
- if = 3
Python- Basic Arithmetic Operations
Precedence
Precedence
Precedence
Precedence - Example
Comments – single Line Comment
Comments – multi Lines Comment
Printing to the screen – Quotations
Printing to the screen
Printing to the screen
Printing to the screen
Reading from user
Reading from user
Exercise
Write a python program to read your age and your
name then display them to user?
Exercise
Read two numbers and perform all the arithmetic
operations upon them?
Mixing Types and Conversion up
Mixing Types and Conversion up
Mixing Types and Conversion up
Comparison Operators
Comparison Operators
Assignment
• You can assign to multiple names at the same time
>>> x, y = 2, 3
>>> x
2
>>> y
3
This makes it easy to swap values
>>> x, y = y, x
• Assignments can be chained
>>> a = b = x = 2
Accessing Non-Existent Name
Accessing a name before it’s been properly created (by
placing it on the left side of an assignment), raises an
error

>>> y

Traceback (most recent call last):


File "<pyshell#16>", line 1, in -toplevel-
y
NameError: name ‘y' is not defined
>>> y = 3
>>> y
3

You might also like