0% found this document useful (0 votes)
3 views12 pages

Python Programming 1

Python is a high-level, interpreted, interactive, and object-oriented programming language known for its readability and ease of use for beginners. It supports various applications, and its history dates back to the late 1980s with its creator Guido van Rossum. The document covers Python's features, installation, variable rules, basic operations, comments, strings, arithmetic and comparison operators, and decision-making structures.

Uploaded by

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

Python Programming 1

Python is a high-level, interpreted, interactive, and object-oriented programming language known for its readability and ease of use for beginners. It supports various applications, and its history dates back to the late 1980s with its creator Guido van Rossum. The document covers Python's features, installation, variable rules, basic operations, comments, strings, arithmetic and comparison operators, and decision-making structures.

Uploaded by

Achintha Pradeep
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

Python Programming
Python is a high-level, interpreted, interactive and object-oriented
scripting language. Python is designed to be highly readable. It uses
English keywords frequently where as other languages use punctuation,
and it has fewer syntactical constructions than other languages.

• Python is Interpreted: Python is processed at runtime by the interpreter.


You do not need to compile your program before executing it.

Python Source Code Process/Run

By the Interpreter

• Python is Interactive: You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.

• Python is Object-Oriented: Python supports Object-Oriented style or


technique of programming that encapsulates code within objects.

• Python is a Beginner's Language: Python is a great language for the


beginner-level programmers and supports the development of a wide range of
applications from simple text processing to WWW browsers to games.

History of Python
Python was developed by Guido van Rossum in the late eighties and
early nineties in the Netherlands.

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


2

Apps made using Python

Examples:
• Youtube
• Dropbox
• Google
• Netflix

Variables

Variable is a memory location in the computer memory.

There are rules in Python about variables. A variable name must;


• Be one word only-no spaces
• Use only letters, numbers and the underscore character.
• Start with a letter.

Ex 1: Password=”diamond7”

Ex 2: Name=input (“What is your name ?”)

Create an algorithm

The algorithm works as a plan when you make a program.

Ex:
Set total to 0
Input a number
Add number to total to give the new total
Output the total

Python Installation

There are 2 methods to obtain Python.

1. [Link]

2. Use of an online compiler


Ex: [Link]

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


3

Display a Simple Text

>>> "Hello Python" Correct output with “ “

'Hello Python'

>>> 'Hello Python' Correct output with ‘ ‘

'Hello Python'

>>> Hello Python Compilation error ! (No ‘ or “)

SyntaxError: invalid syntax

Display a Simple Text with “print”

>>> print ("hello") Correct output. Python allows for


either pairs of single or double
hello quotes.

>>> print ('hello')

hello

>>> print ("hello"); Print with ;

hello

>>> print ("hello");

hello

>>> print (hello) Compilation error (No ‘ or “)

Traceback (most recent call last):


File "<pyshell#9>", line 1, in <module>
print (hello)
NameError: name 'hello' is not defined. Did you mean: 'help'?

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


4

>>> print ("Achintha Pradeep-for computer classes...");

Achintha Pradeep-for computer classes...


Several “print” statements…

>>> print ("Achintha Pradeep"); print ("-for computer classes...");

Achintha Pradeep
- for computer classes...

Display a Number
>>> 100

100

>>> "100"

'100'

>>> '100'

'100'

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


5

Performing a Calculations (without using variables)


>>> 100+100 Correct way

200

>>> "100"+"100" Run time error (No compilation error)

'100100'

>>> '100'+'100' Run time error (No compilation error)

'100100'

Run time errors mean output errors.

Single Assignments
>>> a=10

>>> a

10

>>> a=10

>>> print (a)

10

>>> a=10

>>> "a"

'a' Run time error (No compilation error)

>>> 'a'

'a' Run time error (No compilation error)

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


6

Performing a Calculation (with using variables)


Variable is a memory location created in the main memory (RAM). You can use any
character or name for a variable. (Ex: a, age, price1, My_choice etc ….)

>>> a=10

>>> print (a+10)

20

>>> print (a*10)

100

>>> a=10

>>> b=20

>>> print (a+b)

30

>>> print (a+b)+(b-a)

40

>>> print ((a+b)+(b-a))

40

>>> price1=25.00

>>> price2=1500.25

>>> Total_price=price1+price2

>>> print (Total_price)

1525.25

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


7

Multiple Assignments

>>> a=b=c=1

>>> a

>>> b

>>> c

>>> print (a)

>>> print (b)

>>> print (c)

>>> a, b, c =1,2,"Achintha"

>>> a

>>> b

>>> c

'Achintha'

>>> print (a)

>>> print (b)

>>> print (c) → Achintha

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


8

Python Comments and Multiline


Comments in Python are used to explain what the code does.

print("hi 1") # This is single line comment

print("hi 2")
""" This
is
a multi line
comment """

Python Strings
Strings in Python are identified as a contiguous set of characters
represented in the quotation marks. Python allows for either pairs of single
or double quotes. Subsets of strings can be taken using the slice operator
([ ] and [:] ) with indexes starting at 0 in the beginning of the string.

The plus (+) sign is the string concatenation operator and the asterisk (*)
is the repetition operator. For example −

str='Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5thprint
str[2:] # Prints string starting from 3rd character
print str*2 # Printsstring two times
print str+"TEST"# Prints concatenated string

This will produce the following result

Hello World!
H
llo

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


9

llo World!
Hello World!Hello
World!
Hello World!TEST

Python Arithmetic Operators


Assume variable a holds 10 and variable b holds 20, then

Operator Description Example

+ Addition Adds values on either side of the operator. a + b = 30

- Subtraction Subtracts right hand operand from left hand a – b = -10


operand.

* Multiplies values on either side of the operator a * b = 200


Multiplication

/ Division Divides left hand operand by right hand operand b/a=2

% Modulus Divides left hand operand by right hand operand b%a=0


and returns remainder

** Exponent Performs exponential (power) calculation on a**b =10 to


operators the power 20

// Floor Division - The division of operands where 9//2 = 4 and


the result is the quotient in which the digits after 9.0//2.0 =
the decimal point are removed. 4.0

a=10

b=20

print a+b

print a*b

print a+10

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


10

print 2**3

print a**b

print b/a

print b%a

Python Comparison Operators


These operators compare the values on either sides of them and decide
the relation among them. They are also called Relational operators.

Assume variable a holds 10 and variable b holds 20, then −

Operator Description Example

== If the values of two operands are equal, then the (a == b) is not


condition becomes true. true.

!= If values of two operands are not equal, then


condition becomes true.

<> If values of two operands are not equal, then (a <> b) is true.
condition becomes true. This is similar to
!= operator.

> If the value of left operand is greater than the (a > b) is not true.
value of right operand, then condition becomes
true.

< If the value of left operand is less than the value (a < b) is true.
of right operand, then condition becomes true.

>= If the value of left operand is greater than or (a >= b) is not


equal to the value of right operand, then true.
condition becomes true.

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


11

<= If the value of left operand is less than or equal (a <= b) is true.
to the value of right operand, then condition
becomes true.

a=10

b=20

print a==b

print a!=b

print a<>b

print a>b

Python Decision Making


Decision making is anticipation of conditions occurring while execution of
the program and specifying actions taken according to the conditions.

Decision structures evaluate multiple expressions which produce TRUE or


FALSE as outcome. You need to determine which action to take and
which statements to execute if outcome is TRUE or FALSE otherwise.

Following is the general form of a typical decision making structure found


in most of the programming languages −

Achintha Pradeep - BSc(Hons), MSc(IT) P.G. EDUCATION Computer Science


12

Python programming language assumes any non-zero and non-


null values as TRUE, and if it is either zero or null, then it is assumed
as FALSE value.

Python programming language provides following types of decision


making statements. Click the following links to check their detail.

Statement Description

if statements An if statement consists of a boolean expression


followed by one or more statements.

An if statement can be followed by an optional else


if...else statements
statement, which executes when the boolean
expression is FALSE.

You can use one if or else if statement inside


nested if statements
another if or else if statement(s).

weight = float(input("How many pounds does your suitcase

weigh? "))if weight > 50: If [Link]

print("There is a $25 charge for luggage that heavy.")

print("Thank you for your business.")

temp=float(input('What is the temp? ')) If [Link]

if temp>70:

print('Wear shorts')

else:

print('Wear long')

print('Get excercises')

print (temp)

P.G. EDUCATION
Achintha Pradeep - BSc(Hons), MSc(IT) Computer Science

You might also like