0% found this document useful (0 votes)
4 views

Python1_Papia

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python1_Papia

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

5/10/2023

Dr. Papia Sultana


Professor, Dept. of Statistics
University of Rajshahi

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.

What can Python do?


 Python can be used on a server to create web applications.
 Python can be used alongside software to create workflows.
 Python can connect to database systems. It can also read
and modify files.
 Python can be used to handle big data and perform
complex mathematics.
 Python can be used for rapid prototyping, or for
production-ready software development.

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.

Access Python to your PC


 Python is a real-world, production language that is
freely available at http:www.python.org

3
5/10/2023

Basic Syntax
 The print statement:
print(“Hello, world!”)

When you want to display The text must be within


text on the screen, you use quotation marks inside
the print statement. parentheses.

 Python accepts single (‘), double (“) and triple (‘‘‘


or “ “ “) quotes to string literals.
 print(“Hello, world!”)
 print(“““Hello, world!”””)
 print(‘Hello, world!’)
 print(‘‘‘Hello, world!’’’)

4
5/10/2023

 It is like Python convention, that single quotes are


used for words, double quotes are used for
sentences and triple quotes are used to span the
string across multiple lines.

Example on appropriate use of


quotation:
 print(‘It’s sunny cloudy day!’) × Syntax error

 print(“It’s sunny cloudy day!”) √

5
5/10/2023

Example on appropriate use of


quotation:
 print(“““
Today is Tuesday!
And it’s cloudy day!
”””)

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

 \n prints the output in a new line or breakes the


statement inside the print() function.
 print(“Hello! \n I have started learning Python”)
 Output:

Hello!
I have started learning Python

 In syntax, any comments start with a # sign.


 # how to display text on screen
print(“Hello! I am Papia.”)

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

less than or equal to <= a <= b FALSE 0

greater than or equal to >= a >= b TRUE 1

does not equal != a != b TRUE 1

9
5/10/2023

Logical operators
Logical operators are used to combine conditional statements:

Operator Description Example


and Returns True if both x < 5 and x < 10
statements are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns not(x < 5 and x < 10)
False if the result is true

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

~ NOT Inverts all the bits ~x


<< Zero fill left Shift left by pushing zeros in from the right and let the x << 2
shift leftmost bits fall off
>> Signed right Shift right by pushing copies of the leftmost bit in from the x >> 2
shift left, and let the rightmost bits fall off

11
5/10/2023

Variables and data types


 Variables
 Data types
 String
 Integer
 Float

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)

Variable name Value

Variable naming rules


 Variable names can contain only letters, numbers, and
underscores.
 Letters should be of lowercase, with words separated by
underscores as necessary to improve the readability.
 Avoid using Python keywords or function names as
variable names. That is, don not use words that Python has
reserved for particular purposes. For example, print.
 Names should be short but descriptive.

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

Many Values to Multiple Variables


Python allows you to assign values to multiple variables
in one line:
 x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

One Value to Multiple Variables


 x = y = z = "Orange"
print(x)
print(y)
print(z)

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

Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset


Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

19
5/10/2023

 We can assign three different types of data to a


variable:
 String
 Integer
 Float

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

Finding out the data types


 You can use type() function to determine data type
assigned to a variable.
 Example:
Output:
print(type(website))
print(type(age)) <class ‘str’>
<class ‘int’>
print(type(pi)) <class ‘float’

22
5/10/2023

Example Data Type


x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = frozenset({"apple", "banana", "cherry"}) frozenset
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview

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

You might also like