Keywords¶

(Def) Predefined words, that has specific meaning and can’t be used as constants, variables or other identifier names.

In [ ]:
import keyword
keyword.iskeyword("?")
Out[ ]:
False
In [ ]:
keyword.kwlist
Out[ ]:
['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'async',
 'await',
 'break',
 'class',
 'continue',
 'def',
 'del',
 'elif',
 'else',
 'except',
 'finally',
 'for',
 'from',
 'global',
 'if',
 'import',
 'in',
 'is',
 'lambda',
 'nonlocal',
 'not',
 'or',
 'pass',
 'raise',
 'return',
 'try',
 'while',
 'with',
 'yield']

Dynamic Typing¶

(Def) One can reassign the variable to different data type.

In [ ]:
city_schools = 2

city_schools = ["public", "private"]

type(city_schools)
Out[ ]:
list
In [ ]:
type(2)
Out[ ]:
int

Data Types & Structures¶

1. Strings¶

Collection of characters

In [ ]:
name = "Jagiellonian University"
In [ ]:
len(name)
Out[ ]:
23
In [ ]:
name[10]
Out[ ]:
'a'
In [ ]:
name[3:]
Out[ ]:
'iellonian University'
In [ ]:
name[:3]
Out[ ]:
'Jag'
In [ ]:
name[-3]
Out[ ]:
'i'
In [ ]:
name[-2:]
Out[ ]:
'ty'
In [ ]:
# name[0] = "L" #Can you think of a way to do this?
In [ ]:
name + " is oldest university in Krakow."
Out[ ]:
'Jagiellonian University is oldest university in Krakow.'
In [ ]:
name_course = "This is {initial} course for beginners".format(initial="Python")
name_course
Out[ ]:
'This is Python course for beginners'
In [ ]:
Marks = "In Graduation I got %5.2f percent marks but in masters I got %6.3f percent marks:"%(73.778,84.54432)
Marks
Out[ ]:
'In Graduation I got 73.78 percent marks but in masters I got 84.544 percent marks:'
In [ ]:
Marks.split()
Out[ ]:
['In',
 'Graduation',
 'I',
 'got',
 '73.78',
 'percent',
 'marks',
 'but',
 'in',
 'masters',
 'I',
 'got',
 '84.544',
 'percent',
 'marks:']
In [ ]:
Marks.split("I")
Out[ ]:
['',
 'n Graduation ',
 ' got 73.78 percent marks but in masters ',
 ' got 84.544 percent marks:']
In [ ]:
Marks.upper()
Out[ ]:
'IN GRADUATION I GOT 73.78 PERCENT MARKS BUT IN MASTERS I GOT 84.544 PERCENT MARKS:'
In [ ]:
Marks.lower()
Out[ ]:
'in graduation i got 73.78 percent marks but in masters i got 84.544 percent marks:'
In [ ]:
Marks.find("T") #-1 when not found
Out[ ]:
-1
In [ ]:
Marks.find("I", 0, 11)
Out[ ]:
0
In [ ]:
Marks.replace("I", "Y")
Out[ ]:
'Yn Graduation Y got 73.78 percent marks but in masters Y got 84.544 percent marks:'

2. Lists¶

Lists are mutable.

In [ ]:
l = ["Jagiellonian", "Bio", 1354, "Bio", "Phy", "Che"]
In [ ]:
len(l)
Out[ ]:
6
In [ ]:
l[-1]
Out[ ]:
'Che'
In [ ]:
l[3]
Out[ ]:
'Bio'
In [ ]:
l + ["Hello"]
Out[ ]:
['Jagiellonian', 'Bio', 1354, 'Bio', 'Phy', 'Che', 'Hello']
In [ ]:
l
Out[ ]:
['Jagiellonian', 'Bio', 1354, 'Bio', 'Phy', 'Che']
In [ ]:
l += ["Hello"]
In [ ]:
l
Out[ ]:
['Jagiellonian', 'Bio', 1354, 'Bio', 'Phy', 'Che', 'Hello']
In [ ]:
l*5
Out[ ]:
['Jagiellonian',
 'Bio',
 1354,
 'Bio',
 'Phy',
 'Che',
 'Hello',
 'Jagiellonian',
 'Bio',
 1354,
 'Bio',
 'Phy',
 'Che',
 'Hello',
 'Jagiellonian',
 'Bio',
 1354,
 'Bio',
 'Phy',
 'Che',
 'Hello',
 'Jagiellonian',
 'Bio',
 1354,
 'Bio',
 'Phy',
 'Che',
 'Hello',
 'Jagiellonian',
 'Bio',
 1354,
 'Bio',
 'Phy',
 'Che',
 'Hello']
In [ ]:
# l.sort() #Not for list with strings
In [ ]:
l.reverse()
In [ ]:
l
Out[ ]:
['Hello', 'Che', 'Phy', 'Bio', 1354, 'Bio', 'Jagiellonian']
In [ ]:
l.index("Che")
Out[ ]:
1
In [ ]:
l.extend([1,2,3])
In [ ]:
l
Out[ ]:
['Hello', 'Che', 'Phy', 'Bio', 1354, 'Bio', 'Jagiellonian', 1, 2, 3]
In [ ]:
l.clear()
In [ ]:
l
Out[ ]:
[]
In [ ]:
l = [3,1,4,1,5,9,2,6,5,3]
In [ ]:
l.insert(2,7)
In [ ]:
l
Out[ ]:
[3, 1, 7, 4, 1, 5, 9, 2, 6, 5, 3]
In [ ]:
lc = l
lc[-1] = 3
lc,l
Out[ ]:
([3, 1, 7, 4, 1, 5, 9, 2, 6, 5, 3], [3, 1, 7, 4, 1, 5, 9, 2, 6, 5, 3])
In [ ]:
lc = l.copy()
In [ ]:
lc
Out[ ]:
[3, 1, 7, 4, 1, 5, 9, 2, 6, 5, 3]
In [ ]:
lc[-1]=2
In [ ]:
lc,l
Out[ ]:
([3, 1, 7, 4, 1, 5, 9, 2, 6, 5, 2], [3, 1, 7, 4, 1, 5, 9, 2, 6, 5, 3])
In [ ]:
l.remove(3) #Removes the first occurence of 3
In [ ]:
l
Out[ ]:
[1, 7, 4, 1, 5, 9, 2, 6, 5, 3]
In [ ]:
l.pop(3) #Removes the element at 3rd index
Out[ ]:
1
In [ ]:
l.append(2)

3. Tuples¶

Tuples are immutable.

In [ ]:
my_first_tuple = ("apple", "mango", "banana")
In [ ]:
my_first_tuple = ("apple", "mango", "banana", "mango")
In [ ]:
my_first_tuple
Out[ ]:
('apple', 'mango', 'banana', 'mango')
In [ ]:
my_first_tuple = ("apple", "mango", 32)
In [ ]:
my_first_tuple = ("apple",)
In [ ]:
fruits_tuple = ('apple', 'banana', 'mango')

print("First element:", fruits_tuple[0])
print("Last element:", fruits_tuple[-1])

duplicate_tuple = ('apple', 'mango', 'banana', 'mango', 'apple')
print("Tuple with duplicates:", duplicate_tuple)

# Tuple immutability (this would raise an error if uncommented)
# fruits_tuple[1] = 'orange'

print("Length of duplicate tuple:", len(duplicate_tuple))

banana_index = duplicate_tuple.index('banana')
print("Index of 'banana':", banana_index)

apple_count = duplicate_tuple.count('apple')
print("Number of times 'apple' appears:", apple_count)

final_tuple = fruits_tuple + duplicate_tuple
print("Concatenated tuple:", final_tuple)

my_tuple = (10, 20, 30)
a, b, c = my_tuple
print("Unpacked values:", a, b, c)

my_tuple = (10, 20, 30)
print(20 in my_tuple)
print(40 not in my_tuple)

my_tuple = (1, 2)
print(my_tuple * 3)

my_tuple = (10, 20, 30)
print("Minimum value:", min(my_tuple))
print("Maximum value:", max(my_tuple))
First element: apple
Last element: mango
Tuple with duplicates: ('apple', 'mango', 'banana', 'mango', 'apple')
Length of duplicate tuple: 5
Index of 'banana': 2
Number of times 'apple' appears: 2
Concatenated tuple: ('apple', 'banana', 'mango', 'apple', 'mango', 'banana', 'mango', 'apple')
Unpacked values: 10 20 30
True
True
(1, 2, 1, 2, 1, 2)
Minimum value: 10
Maximum value: 30

4. Dictionary¶

Key-value pairs

In [ ]:
my_first_dict = {
    'Best_City': 'Krakow',
    'Best_University': 'Jagiellonian',
    'Best_place': 'market_square',
    'Best_food': 'spicy',
    'Best_sport': 'volleyball',
    'Year': 2022
}

print(my_first_dict)

print(my_first_dict['Best_place'])
print(my_first_dict['Best_food'])
{'Best_City': 'Krakow', 'Best_University': 'Jagiellonian', 'Best_place': 'market_square', 'Best_food': 'spicy', 'Best_sport': 'volleyball', 'Year': 2022}
market_square
spicy
In [ ]:
my_first_dict = {
    'Best_City': ['Krakow', 'Warsaw', 'Lublin'],
    'Best_University': 'Jagiellonian'
}

print(my_first_dict['Best_City'][2])   # Output: 'Lublin'

my_first_dict['Best_habit'] = 'reading'
my_first_dict['year'] = 2022

print(my_first_dict['year'] - 5)       # Output: 2017
Lublin
2017
In [ ]:
fruit_inventory = {'apple': 2, 'banana': 5, 'mango': 10}

print("Keys:",fruit_inventory.keys())
print("Values:",fruit_inventory.values())

mango_quantity = fruit_inventory.get('mango')

fruit_inventory['orange'] = 7
fruit_inventory['apple'] = 4
print("Updated inventory:",fruit_inventory)

fruit_inventory.pop('banana')
Keys: dict_keys(['apple', 'banana', 'mango'])
Values: dict_values([2, 5, 10])
Updated inventory: {'apple': 4, 'banana': 5, 'mango': 10, 'orange': 7}
Out[ ]:
5

Decision Making¶

Should I go to class or not?

In [ ]:
x = 10
if x>10:
  print("Number is Greater")
In [ ]:
if x>10:
  print("Number is Greater")
else:
  print("Number is Smaller")
Number is Smaller
In [ ]:
if x>10:
  print("Number is Greater")
elif x==10:
  print("Number is smaller")
else:
  print("Number is smaller")
Number is smaller

Looping in Python¶

Executing same action several times

In [ ]:
for x in range(1,11):
  print(x)
1
2
3
4
5
6
7
8
9
10
In [ ]:
#Q: Print only odd/even numbers?
In [ ]:
for letter in "coffee":
  print(letter*10)
cccccccccc
oooooooooo
ffffffffff
ffffffffff
eeeeeeeeee
eeeeeeeeee
In [ ]: