Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Introduction To Pyhton Programming

Uploaded by

frzerkebamo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Introduction To Pyhton Programming

Uploaded by

frzerkebamo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Introduction to Programming

with Python

By Tariku A
Introduction to Programming with Python

• code or source code: The sequence of instructions in a


program.
• syntax: The set of legal structures and commands that
can be used in a particular programming language.
• output: The messages printed to the user by a
program.
• console: The text box onto which output is printed.
• Some source code editors pop up the console as an
external window, and others contain their own
console window.
Compiling and interpreting
• Many languages require you to compile
(translate) your program into a form that the
machine understands.
Cont……
• Python is instead directly interpreted into
machine instructions.
The Python Interpreter
• • Python is an interpreted • >>> 3 + 7
• language • 10
• • The interpreter provides • >>> 3 < 15
• an interactive environment
• True
• to play with the language
• >>> 'print me'
• • Results of expressions
are • 'print me'
• printed on the screen • >>> print 'print me'
• print me
• >>>
Expressions
• expression: A data value or set of operations
to compute a value.
• Examples: 1 + 4 * 3 = 42
• Arithmetic operators we will use: + - * /
addition, subtraction/negation, multiplication,
division
• % modulus, a.k.a. remainder
• ** exponentiation
Cont…..
• precedence: Order in which operations are
computed.
• * / % ** have a higher precedence than + -
• 1 + 3 * 4 is 13
• Parentheses can be used to force a certain
order of evaluation.
• (1 + 3) * 4 is 16
Math commands
• Python has useful commands (or called
functions) for performing calculations.
Math's Commands
Cont…..
• To use many of these commands, you must
write the following at the top of your Python
program:
• from math import *
Cont…..
• int(x) converts x to • >>> 1.23232
• 1.2323200000000001
• an integer • >>> print 1.23232
• float(x) converts x • 1.23232
• >>> 1.3E7
• to a floating point
• 13000000.0
• The interpreter • >>> int(2.0)
• shows • 2
• >>> float(2)
• a lot of digits • 2.0
Variables
• variable: A named piece of memory that can
store a value.
Usage:
• Compute an expression's result,
• store that result into a variable,
• and use that variable later in the program.
Cont….
• assignment statement: Stores a value into a variable.
• Syntax:
• name = value
• Examples: x = 5
• gpa = 3.14
• x5 gpa 3.14
• A variable that has been given a value can be used in
expressions.
• x + 4 is 9
Example
• >>> x = 7
• >>> x
• 7
• >>> x+7
• 14
• >>> x = 'hello'
• >>> x
• 'hello'
• >>>
print
• print : Produces text output on the console.
• Syntax:
• print "Message"
• print Expression
• Prints the given text message or expression value on
the console, and
• moves the cursor down to the next line.
• print Item1, Item2, ..., ItemN
• Prints several messages and/or expressions on the same
line.
Examples:
• print "Hello, world!"
• age = 45
• print "You have", 65 - age, "years until
retirement"
• Output:
Hello, world!
You have 20 years until retirement
Example: print Statement
• Elements separated by • >>> print 'hello'
• commas print with a • hello
space between them • >>> print 'hello', 'there'
• A comma at the end of • hello there
the statement (print
‘hello’,)
• will not print a newline
character
Input
• input : Reads a number from user input.
• You can assign (store) the result of input into a variable.
Example:
• age = input("How old are you? ")
• print "Your age is", age
• print "You have", 65 - age, "years until retirement“
• Output:
• How old are you? 53
• Your age is 53
• You have 12 years until retirement
The for loop
• for loop: Repeats a set of statements over a group of
values.
• Syntax:
• for variable Name in group Of Values:
• statements
• We indent the statements to be repeated with tabs or
spaces.
• variable Name gives a name to each value, so you can refer
to it in the statements.
• group Of Values can be a range of integers, specified with
the range function.
Cont…..
• Example:
• for x in range(1, 6):
• print x, "squared is", x * x
Output:
• 1 squared is 1
• 2 squared is 4
• 3 squared is 9
• 4 squared is 16
• 5 squared is 25
Range
• The range function specifies a range of
integers:
• range(start, stop) - the integers between start
(inclusive) and stop (exclusive)
• It can also accept a third value specifying the
change between values.
• range(start, stop, step) - the integers between
start (inclusive) and stop (exclusive) by step
Example
• Example:
• for x in range(5, 0, -1):
• print x
• print "Blastoff!"
Output:
5
4
3
2
1
Blastoff!
If
• if statement: Executes a group
of statements only if a certain
• condition is true. Otherwise,
the statements are skipped.
• Syntax:
• if condition:
• Statements
Example:
• gpa = 3.4
• if gpa > 2.0:
• print "Your application is
accepted."
If/else
• if/else statement: Executes one block
of statements if a certain
• condition is True, and a second block
of statements if it is False.
• Syntax:
• if condition:
statements
else:
statements
Example:
gpa = 1.4
if gpa > 2.0:
print "Welcome to Mars University!"
else:
print "Your application is denied."
Cont……
• Multiple conditions can be chained
with elif ("else if"):
• else :
• if condition: • y=x
statements
• elif condition: • print ‘y = ‘,
statements • print math.sin(y)
• else:
statements • >>> import
• Example
ifstatement
• import math
• x = 30 • y = 0.999911860107
• if x <= 15 :
• y = x + 15
• >>>
• elif x <= 30 :
• y = x + 30
while
• while loop: Executes a
group of statements as
long as a condition is
True.
• good for indefinite loops
(repeat an unknown
number of times)
• Syntax:
• while condition:
statements
Cont…..
• Example:
• number = 1
• while number < 200:
• print number, number = number * 2
Output:
• 1 2 4 8 16 32 64 128
While Loops
• x=1 • >>> import whileloop
• 1
• while x < 10 : • 2
• print x • 3
• 4
• x=x+1 • 5
• In whileloop.py • 6
• 7
• 8
• 9
• >>>
• In whileloop.py
• In interpreter
Logic
• Many logical expressions use relational
operators:
Cont…
• Logical expressions can be combined with
logical operators:
More Data /Everything is an object
• Everything means • >>> x = 7
• everything, including • >>> x
functions and classes • 7
(more • >>> x = 'hello'
• on this later!) • >>> x
• Data type is a property • 'hello'
of the object and not of
• >>>
the variable
Numbers: Integers
• Integer – the equivalent • >>> 132224
of a C long • 132224
• Long Integer – an • >>> 132323 **
unbounded integer • 2
value.
• 17509376329L
• >>>
Numbers: Floating Point
• int(x) converts x to an • >>> 1.23232
integer • 1.2323200000000001
• float(x) converts x to a • >>> print 1.23232
floating point • 1.23232
• The interpreter shows a • >>> 1.3E7
lot of digits • 13000000.0
• >>> int(2.0)
• 2
• >>> float(2)
• 2.0
Numbers: Complex
• Built into Python Same • >>> x = 3 + 2j
operations are • >>> y = -1j
• supported as integer • >>> x + y
and float • (3+1j)
• >>> x * y
• (2-3j)
String Literals
• + is overloaded to do • >>> x = 'hello'
concatenation • >>> x = x + ' there'
• >>> x
• 'hello there'
Substrings and Methods
• >>> s = '012345' • len(String) – returns the
• >>> s[3] • number of characters in
• '3' • the String
• >>> s[1:4] • str(Object) – returns a
• '123' • String representation of the
• >>> s[2:] Object
• '2345' • >>> len(x)
• >>> s[:4] • 6
• '0123' • >>>
• >>> s[-2] • str(10.3)
• '4' • '10.3'
Chapter - Two
• Methods Of Data Fitting

You might also like