ITP313 115s
ITP313 115s
*rga/2020
Python Fundamentals
Program structure
Comments and Whitespace
Statements and Expressions
Variables
Data Types
Operators
Math functions
Python Fundamentals
Program structure
The complete classic “Hello World” program, Pythonic style.
print(“Hello World!”) #That’s it!
No header!
No semi-colon!
No braces!
One line!
Simple!
Python Fundamentals
Whitespace
whitespace is meaningful in python:
especially indentation and placement of newlines.
• Use a newline to end a line of code (press ENTER)
• Use \ when you must go to next line prematurely.
• No braces { } to mark blocks of code in python…
use consistent indentation instead.
• The first line with less indentation is outside of the block.
• The first line with more indentation starts a nested block
• a colon (:) appears at the start of a new block.
Python Fundamentals
Comments
• Start comments with # – the rest of line is ignored.
• Can include a “documentation string” as the first line of
any new function or class that you define.
In addition to int and float, Python supports other types of numbers, such as
Decimal and Fraction. Python also has built-in support for complex numbers,
and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).
Primitive Data Types
STRING
Besides numbers, Python can also manipulate strings, which can be expressed in
several ways. They can be enclosed in single quotes ('...') or double quotes ("...") with the same
result [2]. \ can be used to escape quotes:
For instance, suppose your program needs to store the age of a user. To do that, we
can name this data userAge and define the variable age using the following
statement.
age = 0
We can also define multiple variables at one go. To do that simply write
1. thisIsAVariableName
2. this_is_a_variable_name
Python Fundamentals
Variables
follows same naming convention as C, C++, Java
Primitive Data Types
bool, int, long, float, complex, string
Variable Declaration
Data typing is implicit and automatic upon
assignment
a=0 integer
b = 123L long integer
x = 1.23 float
s = “abc” string
s = ‘abc’ string
isEven = (n % 2 == 0) Boolean
The Assignment Sign
Note that the = sign in the statement userAge = 0 has a different
meaning from the = sign we learned in Math. In programming, the = sign
is known as an assignment sign. It means we are assigning the value on
the right side of the = sign to the variable on the left. A good way to
understand the statement userAge = 0 is to think of it as userAge <-
0.
The statements x = y and y = x have very different meanings in
programming.
Assignment Operator
a=b=c=0 multiple assignments
j, k = 0, 1 multiple assignments
x, y = y, x one-step variable swap
Type the following code into your IDLE
editor.
?
x = 5
y = 10
x = y
print ("x = ", x)
print ("y = ", y)
Integer: + - * // %
2%3 = 2
2//3 = 0
Float: + - * / **
2/3 = 0.6666 as expected
2**3 = 8 means 2^3
Relational Operators
<, >, <=, >=, ==, != <> not supported in ver. 3.x
x % 2 <> 0 test for odd
x % 2 !== 0 test for odd
1<x<5 x is between 1 and 5
1<=x<=5 x is within 1 through 5
Logical Operators
not, and, or
not(1<x and x<5) same as not(1<x<5)
ans ==‘y’ or ans==‘n’
More Assignment Operators
Besides the = sign, there are a few more assignment operators in Python
(and most programming languages).
There are (at least) two distinguishable kinds of errors: syntax errors and exceptions.
Syntax Errors
Syntax errors, also known as parsing errors, are perhaps the most common kind of
complaint you get while you are still learning Python:
>>> print('Hello")
SyntaxError: EOL while scanning string literal
The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest
point in the line where the error was detected. The error is caused by (or at least
detected at) the token preceding the arrow: in the example, the error is detected at the
function print(), since a colon (') is missing before it. File name and line number are
printed so you know where to look in case the input came from a script.
Exceptions
Even if a statement or expression is syntactically correct, it may cause an error when an attempt
is made to execute it. Errors detected during execution are called exceptions and are not
unconditionally fatal: you will soon learn how to handle them in Python programs. Most
exceptions are not handled by programs, however, and result in error messages as shown here:
>>> 10 * (1/0)
>>> 4 + spam*3
>>> '2' + 2
References:
Copyright
Python and this documentation is:
Copyright © 2001-2019 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives.
All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights
reserved.