Back to Basics

[Python3 Workshop]

PythonPune

Trishna Guha

@trishnag

$ whoami

  • Fedora Contributor (Cloud, Atomic, Infrastructure)
  • Software Engineering Intern at Red Hat
  • Computer Science Engineering Undergrad

Python2 -> EOL -> 2020

Time to Brush up your Basics &

Learn Python 3  skills

>>> print("Hello World!")
Hello World!

Using Interpreter

Using Source file

# This is helloworld.py 

#!/usr/bin/env python3
print("Hello World!")

Few Basic Rules & Conventions

  • 4 Spaces for Indentation
  • Never mix Tab and Spaces
  • One blank line between two functions
  • Add a space after ”,” in dicts, lists, tuples, and argument lists and after ”:” in dicts.
  • No spaces just inside parentheses.

Comments

>>> # This is a comment

>>> # print("Hello World!")
...
>>>

Variables and Datatypes

Keywords and Identifiers

False      class      finally    is         return
None       continue   for        lambda     try
True       def        from       nonlocal   while
and        del        global     not        with
as         elif       if         or         yield
assert     else       import     pass
break      except     in         raise

>>> a = 12
>>> type(a)
<class 'int'>


>>> a = 1.0
>>> type(a)
<class 'float'>


>>> a = "Hello World"
>>> type(a)
<class 'str'>

No Need To Specify Datatype

>>> 'India'
'India'
>>> "India"
'India'
>>> 'India\'s Best'
"India's Best"
>>> "India's Best"
"India's Best"

Manipulate Strings

Read User Input

>>> number = int(input("Enter a number: "))
Enter a number: 5
>>> print(number)
5

 

name = str(input("Enter your name: "))
Enter your name: Trishna
>>> print(name)

Multiple Assignments

>>> a, b = 4, 5
>>> a
4
>>> b
5


>>> a, b = b, a
>>> a
5
>>> b
4

Operators

Mathematical

    +, -, /, %, *

Logical

    and, or 

 Relational

<, <=, >, >=, ==, !=

Expressions

a = 9
b = 12
c = 3
x = a - b / 3 + c * 2 - 1
y = a - b / (3 + c) * (2 - 1)
z = a - (b / (3 + c) * 2) - 1
print("X = ", x)
print("Y = ", y)
print("Z = ", z)

Type Conversion

float(string) -> float value
int(string) -> integer value
str(integer) -> string representation
str(float) -> string representation

>>> a = 8.126768
>>> str(a)
'8.126768'

How to Print ?

>>> name = 'Sumith'
>>> country = 'India'

>>> print("%s is from %s" % (name, country))
Sumith is from India

>>> print("{0} is from {1}".format(name, country))
Sumith is from India

>>> print("{0} is from {0}".format(name))
Sumith is from Sumith

>>> print("{} is from {}".format(name, country))
Sumith is from India

>>> print("{} is from {}".format(name))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

Conditions and Control Flow

if expression:

    do this

elif expression:

    do this

else: 

    do this

if x == True:

    do this  

 

if x == False:

    do this               

if x:

    do this

 

if not x:

    do this

Looping

While Loop

>>> # Print 0-10
>>> n = 0 
>>> while n < 11:
...     print(n)
...     n += 1

while condition:

    statement1

    statement2

For Loop

for iterating_var in sequence:

    statement

>>> for i in range(0, 11):  # Print 0-10
...     print(i)

 

# 1-9 SUM

>>> sum = 0
>>> n = 10
>>> for i in range(1, n):
...     sum += i
...
>>> print(sum)
45

Break

Test Condition

FALSE

Loop Starts

Break

Exits from Loop

TRUE

Statement

(Stays inside Loop)

>>> word = "Python2 Programming"
>>> for letter in word:
...     if letter == "2":
...         break
...     print(letter)
...
P
y
t
h
o
n

Continue

Test Condition

FALSE

Loop Starts

Continue

Goes back to the Loop

The Loop does not terminate but continues on with the next iteration

TRUE

Statement

(Stays inside Loop)

Skips the rest of the execution for the current iteration

>>> word = "Python3 Programming"
>>> for letter in word:
...     if letter == "3":
...         continue
...     print(letter)
...
P
y
t
h
o
n
 
P
r

.

 

Data Structure

List

Tuple

Dictionary

Set

String

Lists

>>> list = [1, 2, "hello", 1.0]

>>> list
[1, 2, 'hello', 1.0]

Supports Multiple Datatypes

List Operations

>>> a = [23, 45, 1, -3434, 43624356, 234]
>>> a.append(45)
>>> a
[23, 45, 1, -3434, 43624356, 234, 45]

>>> a.insert(0, 111) # Inserts 111 at 0th Position
>>> a
[111, 1, 23, 45, 1, -3434, 43624356, 234, 45]
>>> a.count(45)
2

>>> a.remove(234)

>>> a
[111, 1, 23, 45, 1, -3434, 43624356, 45]

Lists Operations

>>> b = [45, 56, 90]

>>> a.append(b)

>>> a [45, 43624356, -3434, 1, 45, 23, 1, 111, [45, 56, 90]]

>>> a[-1]

>>> [45, 56, 90]

>>> a.extend(b) #To add the values of b not the b itself
>>> a
[45, 43624356, -3434, 1, 45, 23, 1, 111, [45, 56, 90], 45, 56, 90]
>>> a[-1]
90
>>> a.remove(b)
>>> a
[45, 43624356, -3434, 1, 45, 23, 1, 111, 45, 56, 90]
>>> a.sort()
>>> a
[-3434, 1, 1, 23, 45, 45, 45, 56, 90, 111, 43624356]

Lists Operations (Stack and Queue)

>>> a = [1, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3, 4, 5, 6]

>>> a.pop() # Pop Operation

6
>>> a.pop()
5
>>> a.pop()
4
>>> a
[1, 2, 3]
>>> a.append(34) # Push Operation
>>> a
[1, 2, 3, 34]

>>> a.append(1)

>>> a

[1, 2, 3, 34, 1]

Lists Comprehensions

>>> a = [1, 2, 3]
>>> [x ** 2 for x in a]
[1, 4, 9]

 

>>> z = [x + 1 for x in [x ** 2 for x in a]]

>>> z
[2, 5, 10]

 

Tuple

>>> a = 'Fedora', 'Debian', 10, 12
>>> a
('Fedora', 'Debian', 10, 12)
>>> a[1]
'Debian'

>>> for x in a:

...     print(x, end=' ')
...
Fedora Debian 10 12

>>> type(a)
<class 'tuple'>

>>> len(a)

4

>>> a = (1, 2, 3, 4)
>>> del a[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion

Immutable (Cannot modify any value)

Tuple Packing & Unpacking

>>> a = (1, 2, 3) # Packing

>>> (first, second, third) = a # Unpacking
>>> first
1
>>> second
2
>>> third
3

>>> (first, second, third) = (1, 2, 3) # Another way

Dictionary

>>> data = {'kushal':'Fedora', 'kart_':'Debian', 'Jace':'Mac'}
>>> data
{'kushal': 'Fedora', 'Jace': 'Mac', 'kart_': 'Debian'}
>>> data['Jace']
'Mac'

>>> data['Sumith'] = 'Fedora'

>>> data

{'Sumith': 'Fedora', 'kushal': 'Fedora', 'Jace': 'Mac', 'kart_': 'Debian'}

>>> del data['Jace']
>>> data
{'Sumith': 'Fedora', 'kushal': 'Fedora', 'kart_': 'Debian'}

Loop through Dict

>>> for x, y in data.items():
...     print("%s uses %s" % (x, y))
...
Sumith uses Fedora
kushal uses Fedora
kart_ uses Debian

Sets

 

>>> a = set('abcthabcjwethddda')
>>> a
{'a', 'c', 'b', 'e', 'd', 'h', 'j', 't', 'w'}
>>> a.add('p')
>>> a
{'a', 'c', 'b', 'e', 'd', 'h', 'j', 'q', 'p', 't', 'w'}

Slicing

>>> a = [1, 2, 3, 4, 5, 6, 7] # List
>>> a[1:4]
[2, 3, 4]

>>> a[:-1]

[1, 2, 3, 4, 5, 6]

>>> a[::-1]

[7, 6, 5, 4, 3, 2, 1] # Reverse

 

>>> a = (1, 2, 3, 4, 5, 6, 7)  # Tuple
>>> a[1:4]
(2, 3, 4)
>>> a[::-1]
(7, 6, 5, 4, 3, 2, 1)

 

Slicing

>>> # String

>>> a = 'Hello World'
>>> a[0]
'H'
>>> a[5]
' '
>>> a[1:]
'ello World'
>>> a[::-1] # Reverse
'dlroW olleH'

String

>>> s = "I am Indian"

>>> s

I am Indian'

>>> s = "Here is a line \
... split in two lines"
>>> s
'Here is a line split in two lines

>>> s = """ This is a
... multiline string, so you can
... write many lines"""
>>> print(s)
This is a
multiline string, so you can
write many lines

String Operation

>>> s = "kushal das"

>>> s.title()

'Kushal Das'

>>> z = s.upper()
>>> z
'KUSHAL DAS'
>>> z.lower()
'kushal das'

>>> s = "123"
>>> s.isdigit()
True

>>> s = "Fedora24"
>>> s.isdigit()
False

 

Split Method String

>>> s = "Let's learn Python3"

>>> s.split(" ")

["Let's", 'learn', 'Python3']

 

>>> s.split()

["Let's", 'learn', 'Python3']

 

>>> s = "This is: ME"
>>> s.split(":")
['This is', ' ME']

Join Method

Opposite to Split

>>> s = "We all love Python."
>>> s.split(".")
['We all love Python', '']


>>> "3".join(s.split("."))
'We all love Python3'

Strip Method String

>>> s = "    abc  "
>>> s.strip()
'abc'

 

>>> # Strip from left hand and right hand size

>>> s = "www.fedoraproject.org"
>>> s.lstrip("www.")
'fedoraproject.org'

 

>>> s.rstrip(".org")
'www.fedoraproject'

How to use Enumerate

>>> for i, j in enumerate(['a', 'b', 'c']):
...     print(i, j)
...
0 a
1 b
2 c

Functions

def functionname(params):
    statement1
    statement2

 

>>> def sum(a, b):
...     return a + b

 

>>> res = sum(2, 3)
>>> res
5

>>> # Check Palindrome Using Function

 

>>> def check_palindrome(word):

...     rev_word = word[::-1]

...     if word == rev_word:

...         print("Palindrome")

...     else:

...         print("Not Palindrome")

...

>>> check_palindrome("madam")

Palindrome

>>> check_palindrome("python3")

Not Palindrome

Default Arguments

>>> def test(a , b=-99):
...     if a > b:
...         return True
...     else:
...         return False

 

>>> test(12, 23)
False
>>> test(12)
True

Keyword Arguments

>>> def func(a, b=5, c=10):
...     print('a is', a, 'and b is', b, 'and c is', c)
...
>>> func(12)
a is 12 and b is 5 and c is 10
>>> func(12, 24)
a is 12 and b is 24 and c is 10
>>> func(12, c = 25)
a is 12 and b is 5 and c is 25

>>> func(a=12)
a is 12 and b is 5 and c is 10

 

Note: Can not have non-keyword argument after a keyword based argument

File Handling

r - Open with read only mode

w - Open with write mode

a - Open with append mode

Reading File

>>> f = open('sample.txt', 'r')
>>> f.read()
"Hello World.\nLet's learn Python3.\n"

>>> f.close()

>>> f = open('sample.txt', 'r')
>>> f.readline()
'Hello World.\n'
>>> f.readline()
"Let's learn Python3.\n"

>>> f.close()

>>> f = open('sample.txt', 'r')
>>> f.readlines()
['Hello World.\n', "Let's learn Python3.\n"]

>>> f.close()

Loop through Lines

>>> f = open('sample.txt', 'r')
>>> for i in f:
...     if 'Python' in i:
...         print(i)
...    f.close()


Let's learn Python3.

Write and Append

>>> f = open('test.txt', 'w') # Write
>>> f.write("Hello world")

>>> f.close()

>>> f = open('test.txt', 'r')
>>> f.read()
'Hello world'

>>> f.close()

>>> f = open('test.txt', 'a') # Append
>>> f.write(" Python')

>>>f.close()

>>> f = open('test.txt', 'r')
>>> f.read()
"Hello world Python"

>>>f.close()

Using with Statement

>>> with open('sample.txt', 'r') as f:
...     f.read()
...
"Hello World.\nLet's learn Python3.\n"

 

>>> with open('sample.txt', 'r') as f:
...     for i in f:
...         if 'Python' in i:
...             print(i)
...
Let's learn Python3.

Modules

  • import module 
  • from module import something
  • from module import *

Modules

# math.py
def square(x):
    return x * x

 

 

# result.py

from math import square

 

print(square(5))

if __name__ == '__main__':

Run Module as Main Program

Sets __name__ to the string "__main__"

If imported from another module

Sets __name__ to the module's name

Standalone Program

Reusable Module

if __name__ == '__main__':

# math.py
def square(x):
    return x * x

if __name__ == '__main__':
    print("Result: for square(5) == %d " % square(5))

 

# result.py

import math

print(math.square(5))

Questions?

Fun with Programming

Make Sure your Pictures directory only has Image files

If there are other type of files move them to their appropriate directories!

  • IRC nick: trishnag

  • IRC: #dgplug, #fedora-apps, #fedora-cloud, #fedora-releng

  • Twitter: trishna_g

  • Blog: https://trishnag.wordpress.com

  • Github: https://github.com/trishnaguha

  • Drop me Mail at: trishnag@fedoraproject.org

Trishna Guha

Thanks!

Back to Basics [Python3 101 Workshop]

By Trishna Guha

Back to Basics [Python3 101 Workshop]

  • 1,794