Download Lect 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Python Conversion
Course
Cathal Hoare
Website - http://www.cs.ucc.ie/~hoare
Why Python?
•
Well established, mature language, with a large
community of developers
•
Lots of resources to help developers
•
Lots of libraries of code to support everything
from web development (Django, Webpy) to
machine vision (opencv)
Value of Python to you
source Morgan McKinley 2014 Irish Salary and Benefits Guide
Python in the Realworld
•
Google, Yahoo, Instagram (Django Framework)
•
Battlefield 2, Civilization 4
•
Also used in science, finance and many other
domains
Python vs. Java
•
Problem solving approach is similar - in fact
Python is a great language for research and
prototyping
•
Python is concise compared to Java
•
Syntactic differences but recognisable non the
less
•
Subtle implementation differences (e.g. dynamic
typing, conversions between types etc)
Python vs Java
Java
class helloworld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Python
print(“Hello World!”)
More Complex Example
LIMIT = 1000
MAXPERLINE = 10
num_primes = 0
for n in range(2, LIMIT+1):
possible_prime = True
# set possible prime to false if n composite
for c in range(2, n):
if n%c==0:
possible_prime = False
break
# print n if it is prime
if possible prime :
print (”%6d” % n, end=””)
num_primes = num primes + 1
if num_primes % MAXPERLINE == 0 :
print ()
print()
How Python Executes
•
We use a tool called IDLE. It has two forms of input:
• Python Shell where short sequences of Python can
be interpreted and executed
• Interpretation and Execution of Python code files
(usually ending in .py)
•
Python Interpreter analyses each instruction for
validity and converts it to machine code
•
This contrasts to Java with its compilation/
interpretation model
IDLE and Python Environment
Jump to the console here!
Python’s Numeric Data Types
We focus initially on two simple numeric data types:
int
• Integers (whole numbers, positive, negative or zero)
• Supports standard arithmetic operations such as
addition, multiplication, etc.
float
• Real numbers
• Supports arithmetic operations and mathematical
functions such as square root etc
Many other data types exist.
integer values
•
•
•
Integers in Python are written in the standard way:
-12345 0 67890
optionally signed, decimal digits, no space or internal
punctuation
No limit on size
Can combine using standard operators such as +, -, *
and ()
e.g. 1 + 2 * 3
integer division
•
Python has two operators for division of integers
//
Integer division, fractional part discarded
Expression Result
8 // 3
2
8 // 4
2
8 // 5
1
%
Gives remainder
Expression
Result
8%3
2
8%4
0
•
Both return integer results for integer values
Division of negative integers
•
Python’s // rounds towards -∞ (not towards zero)
>>> 5 // 2
2
>>> -5 // 2
-3
In each case, its the largest integer value less than or
equal to the precise real result.
•
Precedence follows BODMAS rules (Brackets, Orders,
Division, Multiplication, Addition, Subtraction)
•
Don’t forget - division by zero is just plain wrong!
float values
•
Floats may be written
• as decimals (e.g. 2.718)
• as scientific notation (e.g. 6.02E + 23)
•
Floats are an approximation of the true value, and they
have limits on scale and precision
• e.g. math.pi is 3.141592653589793 which has only
15 places of precision
float division
•
The basic arithmetic operators work with floats
•
Float division / is an extra operator available to floats
>>> 8 / 2
4.0
>>> 10 / 2.5
4.0
>>> 8.5 / 2.7
3.148148148148148
•
The operator produces a float result even when its
parameters are integers
•
Be careful! Don’t confuse / and //
Python’s Mathematical Functions
•
Python has a rich mathematical library
>>> import math
>>> math.sqrt(5)
2.23606797749979
•
We will see an example of this in the lab session
Python’s Simple Output
•
Output can be achieved using the print() command
>>> print(2)
2
>>> print(“Hello World”)
Hello World
•
We will see how this output can be extensively
formatted in later lectures
Variables in Python
•
•
Variables names must consist of letters, digits and
underscores
Must not begin with a digit
•
Avoid using reserved keywords - these are often
highlighted in development environments
•
Python is case sensitive - myVar is different to myvar
Assignments in Python
•
E.g.
x=1+2*3
x is the variable
= is the operator
1+2*3 is the expression
We say x is assigned the value of the expression
More Assignments in Python
•
Assume a and b are previously unseen:
a=b+1
This line will cause an error
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
a=b+1
NameError: name ’b’ is not defined
a’s use is legal. However, as b’s use is illegal.
We can assign a previously unseen variable (i.e. use it
on the LHS (Left Hand Side))
But we cannot use a previously unseen variable on
the RHS