Академический Документы
Профессиональный Документы
Культура Документы
Example:
# From here on, the $ sign denotes the start of a terminal command line,
and the # sign denotes a comment. Note: the # sign denotes a comment
in Python. Python ignores anything written to the right of a # sign on a
given line
$ python # Type python into your terminal's command line
>>> # After a short message, the >>> symbol will appear. This
signals
# the start of a Python interpreter's command line
Running Python (2)
Examples:
>>> print (“Hello world”)
Hello world
# Relevant output is displayed on subsequent lines without the >>> symbol
>>> x = [0,1,2]
# Quantities stored in memory are not displayed by default
>>> x
# If a quantity is stored in memory, typing its name will display it
[0,1,2]
>>> 2+3
5
>>> # Type ctrl-D to exit the interpreter
$
Running Python (3)
Python scripts can be written in text files with the suffix .py. The scripts
can be read into the interpreter in several ways:
Examples:
$ python script.py
# This will simply execute the script and return to the terminal afterwards
$ python -i script.py
# The -i flag keeps the interpreter open after the script is finished running
$ python
>>> execfile('script.py')
# The execfile command reads in scripts and executes them immediately,
as though they had been typed into the interpreter directly
$ python
>>> import script # DO NOT add the .py suffix. Script is a module
here
# The import command runs the script, displays any unstored outputs, and
creates a lower level (or context) within the program. More on contexts
later.
Running Python (4)
Suppose the file script.py contains the following lines:
print 'Hello world'
x = [0,1,2]
Let's run this script in each of the ways described on the last slide:
Examples:
$ python script.py
Hello world
$
# The script is executed and the interpreter is immediately closed. x is
lost.
$ python -i script.py
Hello world
>>> x
[0,1,2]
>>>
# “Hello world” is printed, x is stored and can be called later, and the
interpreter is left open
Running Python (5)
Examples: (continued from previous slide)
$ python
>>> execfile('script.py')
Hello world
>>> x
[0,1,2]
>>>
# For our current purposes, this is identical to calling the script from the
terminal with the command python -i script.py
$ python
>>> import script
Hello world
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
>>>
# When script.py is loaded in this way, x is not defined on the top level
Running Python (6)
Examples: (continued from previous slide)
# to make use of x, we need to let Python know which module it came
from, i.e. give Python its context
>>> script.x
[0,1,2]
>>>
# Pretend that script.py contains multiple stored quantities. To promote x
(and only x) to the top level context, type the following:
$ python
>>> from script import x
Hello world
>>> x
[0,1,2]
>>>
# To promote all quantities in script.py to the top level context, type
from script import * into the interpreter. Of course, if that's what
you want, you might as well type python -i script.py into the
terminal.
In Python 3.3
• Open the code in Idle IDE
• You can run the program using two options
run module
use python shell
If you are using python shell the use the
following command
>>>exec(open("first.py").read())
if the file is in first.py file
Accessing individual elements of file
• Consider To execute:(In python shell)
first.py >>> exec(open("first.py").read())
• print("Hello World") To get individual item
• x=[5,6,7] >>> from first import x
• a=10 >>>x
• b=2 [5,6,7]
• c1=a**b >>> from first import c1
Error….
>>> from first import *
>>>c1
100
Python Build-in types
Numbers 3.1415
Strings “Hello World”
Lists [1,2,3,4]
Dictionaries {‘test’: ‘yum’}
Files input=open(‘file.txt’, ‘r’)
Variables and Types
Objects always have a type
>>> a = 1
>>> type(a)
<type 'int'>
<class 'int'> //In python 3
>>> a = "Hello"
>>> type(a)
<type 'string'>
<class ‘str’> //In python 3
>>> type(1.0)
<type 'float'>
<class ‘float’> //In python 3
Types and Operators: Types of Numbers (1)
Python supports several different numeric types
Integers
Examples: 0, 1, 1234, -56
Integers are implemented as C longs
Note: dividing an integer by another integer will return only the integer
part of the quotient, e.g. typing 7/2 will yield 3
Long integers
Example: 999999999999999999999L
Must end in either l or L
Can be arbitrarily long
Floating point numbers
Examples: 0., 1.0, 1e10, 3.14e-2, 6.99E4
Implemented as C doubles
Division works normally for floating point numbers: 7./2. = 3.5
Operations involving both floats and integers will yield floats:
6.4 – 2 = 4.4
Types and Operators: Types of Numbers (2)
Other numeric types:
Octal constants
Examples: 0177, -01234
Must start with a leading 0
Hex constants
Examples: 0x9ff, 0X7AE
Must start with a leading 0x or 0X
Complex numbers
Examples: 3+4j, 3.0+4.0j, 2J
Must end in j or J
Typing in the imaginary part first will return the complex number in the
order Re+ImJ
In Python 3
• int (signed integers) − They are often called
just integers or ints. They are positive or
negative whole numbers with no decimal
point. Integers in Python 3 are of unlimited
size. Python 2 has two integer types - int and
long. There is no 'long integer' in Python 3
anymore.
• hexa-decimal or octal form
• >>> number = 0xA0F #Hexa-decimal
• >>> number
2575
• >>> number = 0o37 #Octal
• >>> number
31
Types and Operators: Operations on Numbers
Basic algebraic operations
Four arithmetic operations: a+b, a-b, a*b, a/b
Exponentiation: a**b
Other elementary functions are not part of standard Python, but included
in packages like NumPy and SciPy
Comparison operators
Greater than, less than, etc.: a < b, a > b, a <= b, a >= b
Identity tests: a == b, a != b
Bitwise operators
Bitwise or: a | b
Bitwise exclusive or: a ^ b # Don't confuse this with exponentiation
Bitwise and: a & b
Shift a left or right by b bits: a << b, a >> b
Number Type Conversion
• If you have >>> x=3.3
• int(x) to convert x to a plain integer.
• long(x) to convert x to a long integer.// not there
in python 3
• float(x) to convert x to a floating-point number.
• complex(x) to convert x to a complex number
with real part x and imaginary part zero.
• complex(x, y) to convert x and y to a complex
number with real part x and imaginary part y. x
and y are numeric expressions
Mathematical Functions
• 1 abs(x) The absolute value of x: the (positive) distance between x
and zero.
• 3 cmp(x, y)
• -1 if x < y, 0 if x == y, or 1 if x > y. Deprecated in Python 3. Instead
use return (x>y)-(x<y).
• x=3.7
• y=4.4
• print("the absolute value of x is : " + str(abs(x)))
•
• print("the log value of x is : " + str(math.log(x)))
• pi
The mathematical constant pi.
• e
The mathematical constant e.