Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ii:labs:02:tasks:01 [2021/11/07 17:25]
radu.mantu
ii:labs:02:tasks:01 [2024/10/25 01:48] (current)
radu.mantu
Line 73: Line 73:
 </​code>​ </​code>​
  
-Notice how we we accessed the real and imaginary parts of the complex number ''​c''?​ Being able to do this should immediately remind of structures in //C//, or classes in //C++//. We should investigate this further...+Notice how we accessed the real and imaginary parts of the complex number ''​c''?​ Being able to do this should immediately remind of structures in //C//, or classes in //C++//. We should investigate this further...
  
 <code python> <code python>
Line 103: Line 103:
 So the //complex// type has two internal variables that hold the real and imaginary parts of the number. But what's this? It also has a method (i.e.: function) defined. Try to invoke **conjugate()** for ''​c''​. Does it work? Does it change the **imag** and **real** internal variables? So the //complex// type has two internal variables that hold the real and imaginary parts of the number. But what's this? It also has a method (i.e.: function) defined. Try to invoke **conjugate()** for ''​c''​. Does it work? Does it change the **imag** and **real** internal variables?
  
-Well, that was an interesting detour. But let's get back to our numbers. As you might imagine, all the basic arithmetic operators work in python ​pretty much like they would in //C//; even the modulus operator. There are, however, two extra ones that might be useful: //​exponentiation//​ and //floor division//:+Well, that was an interesting detour. But let's get back to our numbers. As you might imagine, all the basic arithmetic operators work in //​Python// ​pretty much like they would in //C//; even the modulus operator. There are, however, two extra ones that might be useful: //​exponentiation//​ and //floor division//:
  
 <code python> <code python>
Line 154: Line 154:
 ...     "​and his comrades would chuckle at the sheer treason of it." ...     "​and his comrades would chuckle at the sheer treason of it."
  
->>>​ the length of the string can be obtained using len()+>>> ​the length of the string can be obtained using len()
 >>>​ len(s) >>>​ len(s)
 230 230
Line 216: Line 216:
  
 # here, we extract every second month starting with January # here, we extract every second month starting with January
-# the z in [x:y:z] is basically ​the iteration step+# the z in [x:y:z] is the iteration step
 # in this case, x and y didn't need to be explicitly stated # in this case, x and y didn't need to be explicitly stated
 >>>​ months[0:​12:​2] >>>​ months[0:​12:​2]
Line 223: Line 223:
 ['​January',​ '​March',​ '​May',​ '​July',​ '​September',​ '​November'​] ['​January',​ '​March',​ '​May',​ '​July',​ '​September',​ '​November'​]
  
-# here, we extract months ranging from the one indexed 6th to the one indexed 0th, in reverse order+# here, we extract months ranging from the one indexed 6th to the one indexed 0th (excluding it), in reverse order
 >>>​ months[6:​0:​-1] >>>​ months[6:​0:​-1]
 +['​July',​ '​June',​ '​May',​ '​April',​ '​March',​ '​February'​]
 </​code>​ </​code>​
  
Line 255: Line 256:
 </​code>​ </​code>​
  
-Let's take a closer look at the final two examples. In the first, we used the //Python// equivalent of the //C// ternary operator: ''​it %%**%% 3 if it % 2 == 0 else None''​. This would roughly translate to ''​(it % 2 == 0) ? pow(it, 3) : None''​. Notice that in this example, we either have to have ''​it ** 3''​ or ''​None''​. In other words, we can't drop the ''​else''​. Otherwise, we would get an invalid syntax error. For a similar outcome however, we have the second example. Here, the use of ''​if it % 2 == 1''​ at the end is specific to this type of array initialization and will most likely generate an error in any other contexts.+Let's take a closer look at the final two examples. In the first, we used the //Python// equivalent of the //C// ternary operator: ''​it %%**%% 3 if it % 2 == 0 else None''​. This would roughly translate to ''​(it % 2 == 0) ? pow(it, 3) : None''​. Notice that in this example, we either have to have ''​it ** 3''​ or ''​None''​. In other words, we can't drop the ''​else''​. Otherwise, we would get an invalid syntax error. For a similar outcome however, we have the second example. Here, the use of ''​if it % 2 == 1''​ at the end is specific to this type of array initialization and will most likely generate an error in any other context.
  
 Since we know how to initialize an array, and access elements of an array, all that's left is manipulating an array. Since we know how to initialize an array, and access elements of an array, all that's left is manipulating an array.
Line 324: Line 325:
 >>>​ list(t) >>>​ list(t)
 ['​pi',​ 3.1415, True] ['​pi',​ 3.1415, True]
->>>​ list(t) + [ '3', .5772, True ] +>>>​ list(t) + [ 'e', ​2.7182, True ] 
-['​pi',​ 3.1415, True, '3', ​0.5772, True]+['​pi',​ 3.1415, True, 'e', ​2.7182, True]
  
 >>>​ # similarly, a tuple can be generated from a list >>>​ # similarly, a tuple can be generated from a list
->>>​ tuple(list(t) + [ '3', .5772, True ]) +>>>​ tuple(list(t) + [ 'e', ​2.7182, True ]) 
-('​pi',​ 3.1415, True, '3', ​0.5772, True)+('​pi',​ 3.1415, True, 'e', ​2.7182, True)
  
 >>>​ # tuples can be used as a shortcut to assign multiple values at once >>>​ # tuples can be used as a shortcut to assign multiple values at once
ii/labs/02/tasks/01.1636298747.txt.gz · Last modified: 2021/11/07 17:25 by radu.mantu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0