suzan
log
cara
boyana
aglaia
Hello Python!
ada
Now we will add an if statement, with the condition that the Python is a high-level, general-purpose programming
variable name should start with an "a". language. Its design philosophy emphasizes code
readability with the use of significant indentation.
If the condition is met (if it is True so to say), then it will continue:
([Link]
In [67]: for name in xpub1:
Python_(programming_language))
if [Link]("a"):
print(name) Where can you work with Python?
aglaia
ada • $ python3
• [Link] + $ python3 [Link]
You can add an else statement too, which will catch all the
• in Jupyter Lab using notebooks
cases that were False :
Documentation and resources:
In [68]:for name in xpub1:
if [Link]("a"):
• [Link]
print(name)
else: • [Link]
print("This name does not start with an • [Link]
'a':", name)
• SHIFT + TAB (to read Python documentation inline)
This name does not start with an 'a': suzan
• TAB (to get a list of options inline)
This name does not start with an 'a': log
This name does not start with an 'a': cara
This name does not start with an 'a': boyana
aglaia
ada variables
In [ ]:
In [2]: a = 3
b = 2
In [3]: a * b
Out[3]: 6
In [4]: b + b Out[59]: 'XPUB says Hello!'
Out[4]: 4 In [60]: [Link]()
You can store the result of an arithmic in another variable, for Out[60]: ['XPUB', 'says', 'Hello!']
example:
In [61]: [Link]("x")
In [5]: c = a + b Out[61]: False
In [6]: c In [62]: [Link]("X")
Out[6]: 5 Out[62]: True
In [63]: [Link]("P")
print() Out[63]: 1
You can see the result of your arithmic expression by directly In [64]: [Link]("H")
running a cell, or, you can use the built-in print function.
Out[64]: 10
In [7]: a + b
Out[7]: 5 if/else statements
In [8]: print(a + b) Sometimes you want to only do something if a condition is met.
5
For example, we only want to print the names of XPUB1 that start
with an "a"...
numbers
Let's first make a for loop and print all the names:
integers
In [65]: for name in xpub1:
print(name)
In [9]: type(2)
Out[9]: int
If we combine that with the f-string we used above: floats
In [53]: print(f"hello { choice(xpub1) }, welcome to my { In [10]: type(1.5)
choice(items) }")
hello boyana, welcome to my library Out[10]: float
You can turn a float into an integer, sometimes this is useful. Or
for loop with range() the other way around.
In [11]: float(2)
In [54]:for number in range(6):
print(f"hello { choice(xpub1) }, welcome to
Out[11]: 2.0
my { choice(items) }")
hello ada, welcome to my library
In [12]: int(1.5)
hello aglaia, welcome to my studio
hello ada, welcome to my classroom Out[12]: 1
hello cara, welcome to my library
hello cara, welcome to my library
hello suzan, welcome to my library
strings
String operations In [13]: c = "hello"
d = "world"
In [55]: x = "XPUB says Hello! " In [14]: c + d
In [56]: [Link]() Out[14]: 'helloworld'
Out[56]: 'XPUB SAYS HELLO! ' In [15]: type(c)
In [57]: [Link]() Out[15]: str
Out[57]: 'Xpub Says Hello! ' In [16]: str(2)
In [58]: [Link]() Out[16]: '2'
Out[58]: 'xpub SAYS hELLO! ' In [17]: "3" + "2"
In [59]: [Link]() Out[17]: '32'
hello suzan, welcome to my studio
It is possible to access specific characters in a string.
hello suzan, welcome to my classroom
hello suzan, welcome to my library
For example, you can select the first letter of a string with [0] or hello log, welcome to my studio
the last with [-1] . hello log, welcome to my classroom
hello log, welcome to my library
hello cara, welcome to my studio
In [18]: c[0]
hello cara, welcome to my classroom
Out[18]: 'h' hello cara, welcome to my library
hello boyana, welcome to my studio
hello boyana, welcome to my classroom
In [19]: c[-1]
hello boyana, welcome to my library
hello aglaia, welcome to my studio
Out[19]: 'o'
hello aglaia, welcome to my classroom
hello aglaia, welcome to my library
In [20]: c[1]
hello ada, welcome to my studio
hello ada, welcome to my classroom
Out[20]: 'e'
hello ada, welcome to my library
lists pseudo-random numbers
In [21]: xpub1 = ["log", "cara", "ada", "aglaia", You can load built-in modules or external libraries to use
"boyana", "suzan"]
functionalities that other people wrote.
In [22]: xpub1
In [49]: from random import choice
Out[22]: ['log', 'cara', 'ada', 'aglaia', 'boyana',
'suzan']
In [50]: choice(xpub1)
In [23]: type(xpub1)
Out[50]: 'suzan'
Out[23]: list
In [51]: choice(xpub1)
In [24]: xpub1[0]
Out[51]: 'aglaia'
Out[24]: 'log'
In [52]: choice(xpub1)
In [25]: xpub1[-1]
Out[52]: 'aglaia'
Out[25]: 'suzan'
In [43]: s = f"hello { name }, welcome to my { item }" You can also select a range of items in a list, which is called
slicing:
In [44]: s
Out[44]: 'hello you, welcome to my house' In [26]: xpub1[0:3]
Out[26]: ['log', 'cara', 'ada']
for loops In [27]: xpub1[3:]
Out[27]: ['aglaia', 'boyana', 'suzan']
In [45]: for name in xpub1:
print(name)
In [28]: xpub1[:2]
suzan
log Out[28]: ['log', 'cara']
cara
boyana You can sort a list on alphabetical order, or reversed:
aglaia
ada
In [29]: [Link]()
In [46]: items = ["studio", "classroom", "library"]
In [30]: xpub1
In [47]: for name in xpub1:
print(f"hello { name }, welcome to my { Out[30]: ['ada', 'aglaia', 'boyana', 'cara', 'log',
items[0] }") 'suzan']
hello suzan, welcome to my studio
In [31]: [Link](reverse=True)
hello log, welcome to my studio
hello cara, welcome to my studio
hello boyana, welcome to my studio In [32]: xpub1
hello aglaia, welcome to my studio
Out[32]: ['suzan', 'log', 'cara', 'boyana', 'aglaia',
hello ada, welcome to my studio
'ada']
In [48]: for name in xpub1:
for item in items:
print(f"hello { name }, welcome to my {
item }")
string-to-list, list-to-string
string-to-list
In [33]: xpub = "XPUB focuses on the acts of making things Out[38]: ['XPUB',
public and creating publics in the age of post- 'acts',
digital networks. " 'age',
'and',
In [34]: xpub 'creating',
'focuses',
Out[34]: 'XPUB focuses on the acts of making things 'in',
public and creating publics in the age of 'making',
post-digital networks. ' 'networks.',
'of',
In [35]: [Link]() 'of',
'on',
Out[35]: ['XPUB',
'post-digital',
'focuses',
'public',
'on',
'publics',
'the',
'the',
'acts',
'the',
'of',
'things']
'making',
'things', In [39]: " ".join(xpub_list)
'public',
'and', Out[39]: 'XPUB acts age and creating focuses in making
'creating', networks. of of on post-digital public
'publics', publics the the things'
'in',
'the', In [40]: xpub1
'age',
'of', Out[40]: ['suzan', 'log', 'cara', 'boyana', 'aglaia',
'post-digital', 'ada']
'networks.']
In [41]: "------".join(xpub1)
list-to-string
Out[41]: 'suzan------log------cara------boyana------
aglaia------ada'
In [36]: xpub_list = [Link]()
In [37]: xpub_list.sort()
f-strings
In [38]: xpub_list
In [42]: name = "you"
item = "house"