Python1_Papia
Python1_Papia
Introducing Python
Python is a popular programming language. It was
created by Guido van Rossum, and released in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
1
5/10/2023
Introducing Python
Python is a high-level programming language
Open source and community driven
Dynamic typed
Source can be compiled or run just-in-time
Similar to C, R, Pearl, Tcl, Ruby, etc.
2
5/10/2023
Why Python?
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping can be
very quick.
Python can be treated in a procedural way, an object-oriented way or a
functional way.
Can interface with the Component Object Model (COM) used by
Windows
Can interface with Open Source GIS toolsets.
3
5/10/2023
Basic Syntax
The print statement:
print(“Hello, world!”)
4
5/10/2023
5
5/10/2023
Activity
Using print() function write a program describing
yourself.
Sample output:
Name: Papia Sultana
Job: Teaching
Institution: University of Rajshahi
Hobby: Programming
6
5/10/2023
Hello!
I have started learning Python
7
5/10/2023
Types of operators
Python language supports the following types of operators.
Arithmetic operators
Assignment operators
Comparison (relational) operators
Logical operators bitwise
Operators membership
Operators identity
Bitwise operators
Arithmetic operators
Operator Symbol a = 7.5 b=2
Addition + a+b 9.5
Subtraction - a-b 5.5
Multiplication * a*b 15
Division (floating) / a/b 3.75
Division (integer) // a // b 3
Power or Exponent ^ or ** a^b 56.25
8
5/10/2023
Assignment operators
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Relational operators
Descriptions Operator a = 7.5, b = 2 Results Value
less than < a<b FALSE 0
greater than > a>b TRUE 1
equal == a == b FALSE 0
9
5/10/2023
Logical operators
Logical operators are used to combine conditional statements:
Identity operators
Identity operators are used to compare the objects, not if
they are equal, but if they are actually the same object,
with the same memory location:
Operator Description Example
is Returns True if both variables are x is y
the same object
is not Returns True if both variables are x is not y
not the same object
10
5/10/2023
Membership operators
Membership operators are used to test if a sequence is
presented in an object:
Operator Description Example
in Returns True if a sequence with the specified x in y
value is present in the object
not in Returns True if a sequence with the specified x not in y
value is not present in the object
Bitwise operators
Bitwise operators are used to compare (binary) numbers:
operator Name Description Example
& AND Sets each bit to 1 if both bits are 1 x&y
| OR Sets each bit to 1 if one of two bits is 1 x|y
^ XOR Sets each bit to 1 if only one of two bits is 1 x^y
11
5/10/2023
Variables
A variable is something that stores information in a
program so that it can be used later.
12
5/10/2023
Declaring variables
A variable is created and updated in Python using an
assignment statement (=).
name=“Papia”
Print(name)
13
5/10/2023
Continue….
Variable names are case sensitive. For example, Name
and name are two different variable.
Can be of any length.
Don’t begin with a number.
It can not be only digits or numbers.
It can start with an underscore.
Example Output
x=40
y=5 40
5
print(x) 45
print(y)
print(x+y)
14
5/10/2023
15
5/10/2023
Unpack a Collection
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Output Variables
The Python print() function is often used to output
variables.
For example:
x = "Python is awesome"
print(x)
16
5/10/2023
Output Variables
The Python print() function is often used to output
variables.
For example:
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Output Variables
The Python print() function is often used to output
variables.
For example:
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
17
5/10/2023
Output Variables
The Python print() function is often used to output
variables.
For example:
x=5
y = 10
print(x + y)
Output Variables
The Python print() function is often used to output
variables.
For example:
x=5
y = "John"
print(x + y)
Python will give you an error
18
5/10/2023
Output Variables
The Python print() function is often used to output
variables.
For example:
x=5
y = "John"
print(x, y)
Data Types
Python has the following data types built-in by default,
in these categories:
Text Type: str
Numeric Types: int, float, complex
19
5/10/2023
Data types
String: A string (str) is a series of characters and
anything inside quotes is considered as string in
Python. You can use single or double quotes around
your string.
Example
website=“www.ru.ac.bd”
print(website)
20
5/10/2023
Data types
Integer: Integer (int) is a whole number, positive or
negative, without decimals of unlimited length in
Python.
Example:
age=25
print(age)
Data types
Float: Float or floating are the numbers with decimal
point.
Example:
pi=3.14
print(pi)
21
5/10/2023
Casting
If you want to specify the data type of a variable, this
can be done with casting.
Example:
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
22
5/10/2023
Python List
Lists are used to store multiple items in a single
variable.
Lists are one of 4 built-in data types in Python used to
store collections of data, the other 3 are Tuple, Set,
and Dictionary, all with different qualities and usage.
Lists are created using square brackets:
Example:
mylist = ["apple", "banana", "cherry"]
23
5/10/2023
Python Tuples
Tuples are used to store multiple items in a single
variable.
A tuple is a collection which is ordered
and unchangeable.
Tuples are written with round brackets.
For example:
mytuple = ("apple", "banana", "cherry")
Python Set
Sets are used to store multiple items in a single variable.
A set is a collection which is unordered, unchangeable*,
and unindexed.
Sets are written with curly brackets.
For example:
myset = {"apple", "banana", "cherry"}
Note: Set items are unchangeable, but you can remove items
and add new items.
24
5/10/2023
Python Dictionary
Dictionaries are used to store data values in key: value
pairs.
A dictionary is a collection which is ordered*,
changeable and do not allow duplicates.
As of Python version 3.7, dictionaries are ordered. In
Python 3.6 and earlier, dictionaries are unordered.
Dictionaries are written with curly brackets, and have
keys and values.
Python Dictionary
For example:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
25