unit 1 python
unit 1 python
Python was designed to be highly readable which uses English keywords frequently
where as other languages use punctuation and it has fewer syntactical constructions
than other languages
Python is Interactive: This means that you can actually sit at a Python prompt and
interact with the interpreter directly to write your programs
A broad standard library: One of Python's greatest strengths is the bulk of the library is
very portable and cross-platform compatible on UNIX, Windows.
Interactive Mode: Support for an interactive mode in which you can enter results from
a terminal right to the language, allowing interactive testing and debugging of
snippets of code.
Multi-window text editor with syntax highlighting, auto-completion, smart indent and other.
Hello, Python!
>>> 3+4*5;
23
print("Hello, Python!”);
Hello, Python!
I love Python
Introduction to Python Programming 8
Keywords/Reserved Words:
Keywords/Reserved words contain lowercase letters only.
and exec not
assert finally or
def if return
elif in while
else is with
• The triple quotes can be used to span the string across multiple lines.
word = 'word'
– Class names start with an uppercase letter and all other identifiers with a lowercase
letter.
– Starting an identifier with a single leading underscore indicates by convention that
the identifier is meant to be private.
a = [1, 2, 3] a 1 2 3
a
b=a 1 2 3
b
a
a.append(4) 1 2 3 4
b
Introduction to Python Programming 21
Python - Variable Types
• Variables are nothing but reserved memory locations to store values.
This means that when you create a variable you reserve some space in
memory.
a = b = c = 1
a, b, c = 1, 2, "john"
10 / 3 produces 3,
while 10 % 3 produces 1
operand1 + operand2, or 3 + 5
Others are followed by one or more operands until the end of the line,
When operators are evaluated, they perform action on their operands, and produce a
new value.
Note that python supports two different types of numbers, Integers (int) and
Floating point numbers (float). Floating Point numbers have a fractional part
(digits after the decimal place), while Integers do not!
This is especially important for division, as integer division produces a different result
from floating point division:
10 / 3 produces 3.3333333
Other operators work differently on different data types: + (addition) will add two
numbers, but concatenate strings.
Numbers
Strings are a more complicated data type (called Sequences) that we will
discuss more later. They are made up of individual letters (strings of
length 1)
Functions exist which will take data in one type and return data in
another type.
myName = input()
The input() function waits for the user to type some text on the keyboard. This function call evaluates to a
string equal to the user’s text, and it assigns the myName variable to this string value.
Example:
>>> 42 == 42
True
>>> 42 == 99
False
>>> 2 != 3
True
>>> 2 != 2
False
false true
Grade >= 60
Example:
spam = 0
while spam < 5:
print('Hello, world.’)
spam = spam + 1
>>>total=0
for i in range(5):
total=total+i
print(i)
print('Sum of the values',total)
Output
0
1
2
3
4
Sum of the values 10
Output
5
4
3
2
1
The max function tells us the “largest character” in the string (which turns out to be
the letter “w”)
The min function shows us the smallest character which turns out to be a space.
These functions are not limited to looking at strings, they can operate on any
set of values.
You should treat the names of built-in functions as reserved words (i.e. avoid
using “max” as a variable name).
This statement creates a module object named math. If you print the module object,
you get some information about it:
The module object contains the functions and variables defined in the module.
To access one of the functions, you have to specify the name of the module and the name
of the function, separated by a dot (also known as a period).
This format is called dot notation.
The first example computes the logarithm base 10 of the signal-to-noise ratio.
The math module also provides a function called log that computes logarithms base e.
The second example finds the sine of radians . The name of the variable is a hint that
sin and the other trigonometric functions ( cos , tan , etc.) take arguments in radians.
To convert from degrees to radians, divide by 360 and multiply by 2π:
The expression math.pi gets the variable pi from the math module.
The value of this variable is an approximation of π, accurate to about 15
digits.
If you know your trigonometry, you can check the previous result by
comparing it to the square root of two divided by two:
To end the function, you have to enter an empty line (this is not necessary in a
script or python file).
Once you have defined a function, you can use it inside another function.
For example, to repeat the previous refrain, we could write a function called
repeat_lyrics
When this script executes, the print statement will print out “8” because the addtwo
function was called with 3 and 5 as arguments.
Within the function the parameters a and b were 3 and 5 respectively.
The function computed the sum of the two numbers and placed it in the local function
variable named added and used the return statement to send the computed value
back to the calling code as the function result which was assigned to the variable x and
printed out.
Introduction to Python Programming
109
Return Values
• Often a function will take its arguments, do some computation and return a
value to be used as the value of the function call in the calling expression. The
return keyword is used for this.
def greet():
return “Hello”
But in a script, if you call a fruitful function and do not store the result of the function in a variable, the
return value vanishes
This script computes the square root of 5, but since it doesn’t store the result in a variable or display
the result, it is not very useful.
Void functions might display something on the screen or have some other effect, but they don’t have a
return value.
If you try to assign the result to a variable, you get a special value called None