WEBVTT

1
00:00:00.120 --> 00:00:02.879
<v Speaker 1>Welcome to the deep dive. So, if you're a scientist,

2
00:00:03.359 --> 00:00:07.040
<v Speaker 1>and we meet that pretty broadly, right, anyone using numbers,

3
00:00:07.120 --> 00:00:08.679
<v Speaker 1>models to figure things out.

4
00:00:08.599 --> 00:00:13.720
<v Speaker 2>Yeah, analyzing data, simulating theories, asking those big what if questions.

5
00:00:13.199 --> 00:00:16.679
<v Speaker 1>Exactly, and you're maybe curious about using Python, Well, you're

6
00:00:16.719 --> 00:00:18.679
<v Speaker 1>definitely in the right place today. Yeah.

7
00:00:18.679 --> 00:00:22.559
<v Speaker 2>Today we're digging into Michael Mommert's Python for Scientists, the

8
00:00:22.640 --> 00:00:25.679
<v Speaker 2>third edition. We're basically pulling out the essentials for you.

9
00:00:26.079 --> 00:00:28.640
<v Speaker 1>Think of this as the fast track, getting you the

10
00:00:28.679 --> 00:00:31.800
<v Speaker 1>core stuff you need to know about Python in science

11
00:00:31.960 --> 00:00:34.119
<v Speaker 1>without you know, getting totally lost in the weeds.

12
00:00:34.159 --> 00:00:37.520
<v Speaker 2>We're aiming to highlight what's actually useful for your scientific work.

13
00:00:37.880 --> 00:00:41.479
<v Speaker 1>Okay, so let's dive in Python for scientists. What are

14
00:00:41.479 --> 00:00:44.320
<v Speaker 1>we really talking about here? First off, just to be clear,

15
00:00:44.679 --> 00:00:46.479
<v Speaker 1>it's the programming language, not the snake.

16
00:00:46.719 --> 00:00:51.119
<v Speaker 2>Yeah, yeah, important distinction. And scientists, it's not just lab codes, right.

17
00:00:51.320 --> 00:00:56.119
<v Speaker 1>If you're using quantitative models, processing data, asking what if

18
00:00:56.640 --> 00:00:57.799
<v Speaker 1>with numbers.

19
00:00:57.479 --> 00:01:00.240
<v Speaker 2>And this is for you. It's amazing how much modern

20
00:01:00.320 --> 00:01:02.240
<v Speaker 2>science relies on computation.

21
00:01:01.840 --> 00:01:06.200
<v Speaker 1>Now totally experiments are getting huge, data sets are massive.

22
00:01:07.120 --> 00:01:08.680
<v Speaker 1>You just can't do it by hand anymore.

23
00:01:08.719 --> 00:01:12.159
<v Speaker 2>And our theories are getting more complex too, needing computers

24
00:01:12.200 --> 00:01:15.439
<v Speaker 2>to really explore them. You basically need computational tools to

25
00:01:15.480 --> 00:01:16.480
<v Speaker 2>do science today.

26
00:01:16.920 --> 00:01:20.120
<v Speaker 1>But here's a curious thing the book points out. You'd

27
00:01:20.120 --> 00:01:23.120
<v Speaker 1>think with all this powerful hardware, there'd be tons of

28
00:01:23.200 --> 00:01:27.920
<v Speaker 1>amazing specialized software for scientists just readily available.

29
00:01:27.959 --> 00:01:29.400
<v Speaker 2>But that's not always the case, is it.

30
00:01:29.560 --> 00:01:32.879
<v Speaker 1>No, And a big reason is just market size. Scientists, well,

31
00:01:32.920 --> 00:01:36.680
<v Speaker 1>we're a relatively small market compared to say, gamers or businesses,

32
00:01:36.840 --> 00:01:37.159
<v Speaker 1>so the.

33
00:01:37.079 --> 00:01:40.519
<v Speaker 2>Big money isn't always there to constantly push the boundaries

34
00:01:40.519 --> 00:01:43.959
<v Speaker 2>of specialized scientific software, even though these tools are like

35
00:01:44.319 --> 00:01:45.439
<v Speaker 2>essential for us, right.

36
00:01:45.439 --> 00:01:48.560
<v Speaker 1>Which brings us to a key choice using proprietary versus

37
00:01:48.680 --> 00:01:49.599
<v Speaker 1>open source software.

38
00:01:49.680 --> 00:01:53.400
<v Speaker 2>Okay, yeah, proprietary stuff. That's the commercial software companies make it.

39
00:01:53.439 --> 00:01:56.680
<v Speaker 2>They've costs, payroll shareholders, so you've got to pay for

40
00:01:56.719 --> 00:01:59.719
<v Speaker 2>it exactly, and often the actual code, the source code

41
00:01:59.760 --> 00:02:02.760
<v Speaker 2>is secret. It's their intellectual property.

42
00:02:02.519 --> 00:02:05.159
<v Speaker 1>Which means you can't easily customize it for your specific

43
00:02:05.439 --> 00:02:07.359
<v Speaker 1>maybe weird research needs.

44
00:02:07.879 --> 00:02:12.319
<v Speaker 2>Right, and the book mentions this daneingel de effect ah yes,

45
00:02:12.479 --> 00:02:16.759
<v Speaker 2>where because they need ongoing revenue, you often find yourself

46
00:02:16.840 --> 00:02:20.240
<v Speaker 2>paying for frequent updates or new versions just to keep

47
00:02:20.319 --> 00:02:21.360
<v Speaker 2>using it effectively.

48
00:02:21.520 --> 00:02:23.919
<v Speaker 1>Then you've got the flip side. Open source.

49
00:02:24.120 --> 00:02:27.080
<v Speaker 2>Yeah, open source software is usually free. It's often built

50
00:02:27.080 --> 00:02:30.360
<v Speaker 2>by communities, maybe researchers at universities.

51
00:02:29.879 --> 00:02:32.639
<v Speaker 1>People sharing tools with colleagues basically.

52
00:02:32.199 --> 00:02:34.759
<v Speaker 2>And the licensing is different too. It's usually designed so

53
00:02:34.960 --> 00:02:37.360
<v Speaker 2>no one company can just take it over and lock

54
00:02:37.400 --> 00:02:39.159
<v Speaker 2>it down anti copyright.

55
00:02:39.560 --> 00:02:43.360
<v Speaker 1>Often then you might think, okay, free software made by volunteers,

56
00:02:44.520 --> 00:02:46.360
<v Speaker 1>it can't be as good as the paid stuff.

57
00:02:46.159 --> 00:02:48.719
<v Speaker 2>Right, That's a common thought, but the book really pushes

58
00:02:48.759 --> 00:02:52.000
<v Speaker 2>back on that. It argues that actually, in many scientific fields,

59
00:02:52.199 --> 00:02:55.360
<v Speaker 2>open source tools are incredibly powerful. Sometimes they even lead

60
00:02:55.360 --> 00:02:55.680
<v Speaker 2>the way.

61
00:02:56.080 --> 00:02:59.560
<v Speaker 1>Interesting. So, okay, we have this proprietary versus open source split.

62
00:03:00.360 --> 00:03:03.639
<v Speaker 1>But the book also talks about different kinds of scientific software.

63
00:03:03.719 --> 00:03:06.439
<v Speaker 2>Right, so you have what you might call scientific software tools.

64
00:03:06.439 --> 00:03:09.120
<v Speaker 2>These are kind of prepackaged applications.

65
00:03:08.439 --> 00:03:11.840
<v Speaker 1>Like matt Lab or even Excel on the commercial.

66
00:03:11.400 --> 00:03:15.159
<v Speaker 2>Side exactly, and open source ones like are again Octave,

67
00:03:15.520 --> 00:03:20.599
<v Speaker 2>Libre Office. Some have nice graphical user interfaces guys, you know,

68
00:03:21.360 --> 00:03:22.800
<v Speaker 2>click buttons, get results.

69
00:03:23.000 --> 00:03:25.840
<v Speaker 1>Makes common stuff easy, definitely.

70
00:03:25.759 --> 00:03:29.120
<v Speaker 2>But that ease of use can mean less flexibility if

71
00:03:29.120 --> 00:03:31.840
<v Speaker 2>you want to do something really specific the designers didn't

72
00:03:31.840 --> 00:03:32.159
<v Speaker 2>think of.

73
00:03:32.599 --> 00:03:33.960
<v Speaker 1>It can be tough, Okay.

74
00:03:34.199 --> 00:03:36.400
<v Speaker 2>Other tools try to bridge that gap. They might have

75
00:03:36.439 --> 00:03:39.520
<v Speaker 2>a user interface but also include their own built in

76
00:03:39.639 --> 00:03:41.039
<v Speaker 2>programming language.

77
00:03:41.000 --> 00:03:44.120
<v Speaker 1>Like interpreters. They run your code bit by bit precisely.

78
00:03:44.199 --> 00:03:45.960
<v Speaker 2>It gives you more power than just a GUI.

79
00:03:46.120 --> 00:03:49.520
<v Speaker 1>But underneath all these tools, there are the actual programming languages, right,

80
00:03:49.560 --> 00:03:51.039
<v Speaker 1>the fundamental building blocks.

81
00:03:51.199 --> 00:03:55.800
<v Speaker 2>Absolutely. Languages like C and Fotran have been huge in

82
00:03:55.879 --> 00:03:59.000
<v Speaker 2>science historically. They're often called compiled languages.

83
00:03:59.120 --> 00:04:03.039
<v Speaker 1>Compiled language, Yeah, the book uses that racing car analogy.

84
00:04:02.719 --> 00:04:04.879
<v Speaker 2>Right, Yeah, like a formula one car. They can be

85
00:04:04.919 --> 00:04:08.520
<v Speaker 2>incredibly fast because your code gets translated directly into efficient

86
00:04:08.560 --> 00:04:12.360
<v Speaker 2>machine instructions by a compiler. Super powerful, But like driving

87
00:04:12.439 --> 00:04:14.919
<v Speaker 2>enough one car, using them well takes a lot of

88
00:04:15.000 --> 00:04:18.519
<v Speaker 2>skill and sometimes getting things working across different compilers can

89
00:04:18.560 --> 00:04:21.639
<v Speaker 2>be tricky. Plus, you often need lots of external libraries

90
00:04:21.680 --> 00:04:22.560
<v Speaker 2>for basic stuff.

91
00:04:22.879 --> 00:04:26.279
<v Speaker 1>So why don't scientists just use these super fast languages

92
00:04:26.560 --> 00:04:27.199
<v Speaker 1>for everything?

93
00:04:27.439 --> 00:04:30.839
<v Speaker 2>Well, back to the analogy. You wouldn't take enough one

94
00:04:30.879 --> 00:04:32.279
<v Speaker 2>car to pop to the shops, would you?

95
00:04:32.560 --> 00:04:36.160
<v Speaker 1>Eh? Probably not? Speed isn't everything there exactly?

96
00:04:36.360 --> 00:04:39.160
<v Speaker 2>Compiled languages aren't always the best for quickly trying out

97
00:04:39.199 --> 00:04:41.879
<v Speaker 2>an idea or writing a simple script for a daily task.

98
00:04:42.120 --> 00:04:45.319
<v Speaker 1>Okay, so what's the alternative for those everyday tasks?

99
00:04:45.720 --> 00:04:48.839
<v Speaker 2>That's where interpreted languages shine. We mentioned them already. They

100
00:04:48.879 --> 00:04:51.560
<v Speaker 2>often power those more flexible software tools.

101
00:04:51.639 --> 00:04:54.079
<v Speaker 1>It might not be quite as fast as the compiled.

102
00:04:53.680 --> 00:04:57.639
<v Speaker 2>Ones generally, no, but they're often much easier to learn

103
00:04:57.680 --> 00:05:00.879
<v Speaker 2>and use. For a lot of scientists, especially for scripting

104
00:05:01.000 --> 00:05:03.279
<v Speaker 2>or getting started, they're way more approachable.

105
00:05:03.319 --> 00:05:06.000
<v Speaker 1>And this is where Python comes in, right, It's huge now.

106
00:05:05.839 --> 00:05:08.920
<v Speaker 2>It really is especially popular with younger researchers, it seems.

107
00:05:09.000 --> 00:05:12.160
<v Speaker 1>So why what makes Python so attractive for science?

108
00:05:12.360 --> 00:05:15.720
<v Speaker 2>A big reason the book highlights is this idea of explicitness.

109
00:05:16.360 --> 00:05:18.199
<v Speaker 1>Explicitness what does that mean in code?

110
00:05:18.279 --> 00:05:21.680
<v Speaker 2>Okay, imagine you need to read data from say, three

111
00:05:21.680 --> 00:05:25.680
<v Speaker 2>different file types. You could try writing one super complicated

112
00:05:25.720 --> 00:05:29.000
<v Speaker 2>function to handle all three, right, or the Pythonic way,

113
00:05:29.040 --> 00:05:32.279
<v Speaker 2>the sort of encouraged style is to write three separate,

114
00:05:32.480 --> 00:05:37.160
<v Speaker 2>clear functions. One function explicitly handles format A, another handles

115
00:05:37.199 --> 00:05:38.600
<v Speaker 2>format B, and so on.

116
00:05:38.959 --> 00:05:41.199
<v Speaker 1>Ah. Okay, one function, one.

117
00:05:41.079 --> 00:05:43.600
<v Speaker 2>Job exactly And why is that good?

118
00:05:44.199 --> 00:05:47.720
<v Speaker 1>I guess it's easier to understand what's going on less confusing.

119
00:05:47.920 --> 00:05:51.399
<v Speaker 2>Precisely, it's easier for collaborators to read, easier for you

120
00:05:51.439 --> 00:05:53.879
<v Speaker 2>to read six months later when you've forgotten the details,

121
00:05:54.279 --> 00:05:57.680
<v Speaker 2>and in science that kind of clarity and reproducibility is vital.

122
00:05:57.759 --> 00:05:59.839
<v Speaker 1>Makes sense. Okay, So let's say you're sold on Python.

123
00:06:00.199 --> 00:06:02.160
<v Speaker 1>How do you actually get started? Like? Install it?

124
00:06:02.240 --> 00:06:06.240
<v Speaker 2>Right installation? The book mentions you might already have Python

125
00:06:06.240 --> 00:06:06.600
<v Speaker 2>on your.

126
00:06:06.480 --> 00:06:08.319
<v Speaker 1>System, but you shouldn't mess with that one.

127
00:06:08.519 --> 00:06:11.000
<v Speaker 2>Probably best not to, Yeah, it might break other things.

128
00:06:11.160 --> 00:06:14.439
<v Speaker 2>The recommendation is to install something called Anaconda. It's a

129
00:06:14.480 --> 00:06:17.879
<v Speaker 2>distribution of Python that comes bundled with a tool called Conda,

130
00:06:17.920 --> 00:06:22.680
<v Speaker 2>and Conda is fantastic for managing different programming environments environments.

131
00:06:22.720 --> 00:06:24.079
<v Speaker 1>Why do I need different environments?

132
00:06:24.120 --> 00:06:27.000
<v Speaker 2>Well, imagine you're working on project X and it needs

133
00:06:27.120 --> 00:06:30.879
<v Speaker 2>version one point zero of a specific library, but project

134
00:06:31.000 --> 00:06:33.720
<v Speaker 2>Y needs version two point zero of the same library.

135
00:06:33.839 --> 00:06:35.759
<v Speaker 1>Ah conflicts exactly.

136
00:06:36.279 --> 00:06:41.040
<v Speaker 2>Conda lets you create isolated sandboxes or environments. Project X

137
00:06:41.079 --> 00:06:44.279
<v Speaker 2>gets its own environment with its specific libraries. Project Y

138
00:06:44.480 --> 00:06:46.720
<v Speaker 2>gets its own. Nothing clashes.

139
00:06:47.000 --> 00:06:50.319
<v Speaker 1>Okay, that sounds super useful for keeping research projects separate

140
00:06:50.360 --> 00:06:51.279
<v Speaker 1>and reproducible.

141
00:06:51.360 --> 00:06:54.680
<v Speaker 2>It's essential, really, and installing packages into an environment is

142
00:06:54.680 --> 00:06:57.680
<v Speaker 2>easy with Conda. Just Conda install and the package name

143
00:06:57.759 --> 00:06:59.160
<v Speaker 2>like cond install NumPy.

144
00:06:59.399 --> 00:07:01.920
<v Speaker 1>In these pack like NumPy. Where do they come from?

145
00:07:01.959 --> 00:07:03.160
<v Speaker 1>Where they like add ons?

146
00:07:03.480 --> 00:07:03.879
<v Speaker 2>Sort of?

147
00:07:04.000 --> 00:07:04.240
<v Speaker 1>Yeah?

148
00:07:04.360 --> 00:07:06.560
<v Speaker 2>Think of them as libraries of pre written code for

149
00:07:06.600 --> 00:07:10.839
<v Speaker 2>specific tasks. Numb Pi is fundamental for numerical stuff and

150
00:07:10.920 --> 00:07:13.519
<v Speaker 2>most packages scientists need are available through Conda.

151
00:07:13.560 --> 00:07:14.600
<v Speaker 1>Where does condfind them?

152
00:07:14.639 --> 00:07:18.120
<v Speaker 2>A major source is Conda Forge. It's a huge community

153
00:07:18.160 --> 00:07:22.319
<v Speaker 2>effort maintaining recipes to build thousands of packages. It's a

154
00:07:22.360 --> 00:07:24.480
<v Speaker 2>great place to look for scientific tools.

155
00:07:24.839 --> 00:07:28.920
<v Speaker 1>Okay, installed via Anaconda using Conda environments, how do I

156
00:07:28.959 --> 00:07:30.800
<v Speaker 1>actually write and run Python code?

157
00:07:30.839 --> 00:07:34.519
<v Speaker 2>Then the book really focuses on Jupiter notebooks, and honestly

158
00:07:34.560 --> 00:07:37.680
<v Speaker 2>they're a fantastic tool for scientists. The book itself is

159
00:07:37.720 --> 00:07:39.920
<v Speaker 2>written as notebooks jupitern notebooks.

160
00:07:40.160 --> 00:07:41.319
<v Speaker 1>What's special about them.

161
00:07:41.240 --> 00:07:45.160
<v Speaker 2>They let you mix live, runnable code with text, equations,

162
00:07:45.199 --> 00:07:47.600
<v Speaker 2>plots all in one document. You can run them in

163
00:07:47.639 --> 00:07:50.480
<v Speaker 2>your web browser, either locally or in the cloud.

164
00:07:50.519 --> 00:07:54.000
<v Speaker 1>So it's not just code. It's like an interactive lab notebook.

165
00:07:54.120 --> 00:07:56.120
<v Speaker 2>That's a great way to put it. It integrates your code,

166
00:07:56.279 --> 00:08:00.000
<v Speaker 2>your explanations, and your results together. Really powerful for documents

167
00:08:00.319 --> 00:08:01.319
<v Speaker 2>and sharing your work.

168
00:08:01.439 --> 00:08:02.920
<v Speaker 1>What are the main parts of a notebook?

169
00:08:03.040 --> 00:08:05.759
<v Speaker 2>They're made up of cells. You have code cells for

170
00:08:05.800 --> 00:08:08.680
<v Speaker 2>your Python code, which you can run individually.

171
00:08:08.160 --> 00:08:09.319
<v Speaker 1>And see the output right away.

172
00:08:09.439 --> 00:08:11.759
<v Speaker 2>Yep, right underneath the cell. Then you have markedown cells.

173
00:08:12.199 --> 00:08:15.279
<v Speaker 2>Markdown is a simple way to format text headings lists

174
00:08:15.639 --> 00:08:17.319
<v Speaker 2>bold italics.

175
00:08:16.959 --> 00:08:20.000
<v Speaker 1>So you can write explanations right alongside the chemical exactly.

176
00:08:20.079 --> 00:08:23.319
<v Speaker 2>You can even put in mathematical equations using latex notation.

177
00:08:23.800 --> 00:08:26.199
<v Speaker 2>It encourages you to explain what you're doing and why

178
00:08:26.240 --> 00:08:26.759
<v Speaker 2>as you go.

179
00:08:27.000 --> 00:08:29.600
<v Speaker 1>That sounds incredibly useful for keeping track of things.

180
00:08:29.680 --> 00:08:32.559
<v Speaker 2>It really is. And inside the code cells, you can

181
00:08:32.559 --> 00:08:36.039
<v Speaker 2>even run special commands called magic commands that interact with

182
00:08:36.080 --> 00:08:39.600
<v Speaker 2>the operating system or control the notebook itself. They start

183
00:08:39.600 --> 00:08:40.679
<v Speaker 2>with eight percent.

184
00:08:40.559 --> 00:08:43.279
<v Speaker 1>Or percent magic commands like what you can.

185
00:08:43.120 --> 00:08:47.000
<v Speaker 2>List files, change directories, time your code. Things like that.

186
00:08:47.360 --> 00:08:50.120
<v Speaker 2>Typing percent magic gives you the full rundown and percents

187
00:08:50.200 --> 00:08:51.519
<v Speaker 2>magic gives a quick list.

188
00:08:51.720 --> 00:08:54.120
<v Speaker 1>Cool. Okay, so we're in a notebook. What about the

189
00:08:54.159 --> 00:08:58.799
<v Speaker 1>basic Python language elements a scientist needs to grasp right fundamentals.

190
00:08:59.279 --> 00:09:02.320
<v Speaker 2>Let's start with com comments, essential for explaining your code.

191
00:09:02.480 --> 00:09:04.559
<v Speaker 1>How do you write comments two main ways.

192
00:09:04.879 --> 00:09:07.759
<v Speaker 2>Single line comments start with a hash symbol hashtag. Anything

193
00:09:07.799 --> 00:09:10.039
<v Speaker 2>after the hashtag on that line is ignored by Python.

194
00:09:10.440 --> 00:09:13.720
<v Speaker 2>And then there are dock strings. These are multiline strings,

195
00:09:13.799 --> 00:09:16.679
<v Speaker 2>usually enclosed in triple quotes like this is a dock string?

196
00:09:16.840 --> 00:09:17.679
<v Speaker 1>What are they used for?

197
00:09:17.919 --> 00:09:19.919
<v Speaker 2>They're typically put right at the start of a function

198
00:09:20.039 --> 00:09:22.279
<v Speaker 2>or a class to explain what it does. It's inputs,

199
00:09:22.279 --> 00:09:24.399
<v Speaker 2>its outputs really important for documentation.

200
00:09:24.679 --> 00:09:27.440
<v Speaker 1>Got it? What about the actual data we work with?

201
00:09:27.799 --> 00:09:29.039
<v Speaker 1>Numbers text?

202
00:09:29.519 --> 00:09:33.000
<v Speaker 2>In Python? Basically everything is an object. A number is

203
00:09:33.039 --> 00:09:35.200
<v Speaker 2>an object, A piece of text is an object, a

204
00:09:35.279 --> 00:09:40.600
<v Speaker 2>list is an object. Each object has a type like integer, float, string,

205
00:09:40.799 --> 00:09:44.480
<v Speaker 2>and an identity, which is basically its address and memory.

206
00:09:44.600 --> 00:09:46.759
<v Speaker 1>We don't usually worry about the memory address though right

207
00:09:46.879 --> 00:09:47.639
<v Speaker 1>not directly.

208
00:09:47.720 --> 00:09:51.000
<v Speaker 2>No, Instead we use identifiers. These are just names or

209
00:09:51.080 --> 00:09:53.799
<v Speaker 2>variables that we assign to point to these objects. X

210
00:09:53.879 --> 00:09:57.200
<v Speaker 2>equals five means the identifier X now refers to the

211
00:09:57.200 --> 00:09:58.440
<v Speaker 2>integer object five.

212
00:09:58.960 --> 00:10:01.360
<v Speaker 1>Are there rules for naming these identifiers? Yep.

213
00:10:01.679 --> 00:10:03.639
<v Speaker 2>They have to start with a letter or an underscore.

214
00:10:03.720 --> 00:10:06.279
<v Speaker 2>After that, they can have letters, numbers, or underscores.

215
00:10:06.360 --> 00:10:08.879
<v Speaker 1>In case matters data is different from.

216
00:10:08.799 --> 00:10:13.159
<v Speaker 2>Data absolutely case sensitive Also, names starting or ending with

217
00:10:13.240 --> 00:10:16.039
<v Speaker 2>underscores often have special meanings, so maybe you avoid those

218
00:10:16.080 --> 00:10:18.639
<v Speaker 2>at first, and you can't use pythons on keywords like

219
00:10:18.919 --> 00:10:22.000
<v Speaker 2>if for list int as names.

220
00:10:22.000 --> 00:10:25.679
<v Speaker 1>Okay, and using clear descriptive names is good practice. I

221
00:10:25.720 --> 00:10:27.559
<v Speaker 1>assume like reaction rate instead of just.

222
00:10:27.519 --> 00:10:29.720
<v Speaker 2>Are readability accounts, especially in science.

223
00:10:29.840 --> 00:10:33.039
<v Speaker 1>So when we write x equals five, you said that's assignment.

224
00:10:33.440 --> 00:10:36.240
<v Speaker 1>Can you elaborate on that? It's not quite like algebra's

225
00:10:36.279 --> 00:10:37.519
<v Speaker 1>equals sign exactly.

226
00:10:37.600 --> 00:10:41.639
<v Speaker 2>It's crucial to get this in Python means assign the

227
00:10:41.679 --> 00:10:44.720
<v Speaker 2>object on the right to the identifier on the left.

228
00:10:44.919 --> 00:10:48.120
<v Speaker 2>It's an instruction, not a statement of fact. The object

229
00:10:48.200 --> 00:10:50.120
<v Speaker 2>five has the type int, not the.

230
00:10:50.159 --> 00:10:52.279
<v Speaker 1>Name X, and I could later say X hello, yep.

231
00:10:52.600 --> 00:10:55.480
<v Speaker 2>You can reassign x to point to a completely different object,

232
00:10:55.519 --> 00:10:57.879
<v Speaker 2>even one of a different type. The name just points

233
00:10:57.879 --> 00:10:58.799
<v Speaker 2>wherever you tell it to.

234
00:10:59.279 --> 00:11:01.320
<v Speaker 1>How does Python and keep track of all these names

235
00:11:01.320 --> 00:11:02.120
<v Speaker 1>and what they point to?

236
00:11:02.440 --> 00:11:05.200
<v Speaker 2>It uses name spaces. Think of a name space as

237
00:11:05.200 --> 00:11:09.440
<v Speaker 2>a dictionary mapping names identifiers to objects. When you're working

238
00:11:09.519 --> 00:11:12.120
<v Speaker 2>interactively or in a script, there's a main name space

239
00:11:12.200 --> 00:11:15.200
<v Speaker 2>called main. When you import modules, they get their own

240
00:11:15.320 --> 00:11:16.320
<v Speaker 2>name spaces too.

241
00:11:16.559 --> 00:11:18.919
<v Speaker 1>Like folders for names to avoid clashes.

242
00:11:19.039 --> 00:11:21.200
<v Speaker 2>That's a good analogy. Yeah, it keeps things organized.

243
00:11:21.240 --> 00:11:23.960
<v Speaker 1>Okay, what about different kinds of numbers? Scientists use lots

244
00:11:24.000 --> 00:11:24.320
<v Speaker 1>of those.

245
00:11:24.519 --> 00:11:27.720
<v Speaker 2>Python has the basics covered. You've got integers, int, whole

246
00:11:27.840 --> 00:11:32.000
<v Speaker 2>numbers positive negative, zero, and in modern Python they can

247
00:11:32.039 --> 00:11:33.679
<v Speaker 2>be basically as big as you need.

248
00:11:33.919 --> 00:11:37.360
<v Speaker 1>Count equals one hundred, no maximum size practically speaking, not.

249
00:11:37.320 --> 00:11:40.399
<v Speaker 2>Really, which is nice. Then for numbers with decimal points,

250
00:11:40.440 --> 00:11:42.679
<v Speaker 2>you have floating point numbers or floats.

251
00:11:42.919 --> 00:11:44.639
<v Speaker 1>Floats like measurements.

252
00:11:44.120 --> 00:11:47.679
<v Speaker 2>Exactly temperature ecals twenty five point five. They typically have

253
00:11:47.840 --> 00:11:50.759
<v Speaker 2>about sixteen digits of precision. You can write them normally

254
00:11:51.000 --> 00:11:55.440
<v Speaker 2>or using scientific notation like speedo flight it calls three.

255
00:11:55.360 --> 00:11:59.159
<v Speaker 1>E eight and doing math addition subtraction.

256
00:11:58.840 --> 00:12:03.080
<v Speaker 2>All the standard operators work plus and for multiplication. For division,

257
00:12:03.360 --> 00:12:05.879
<v Speaker 2>one thing always gives a float result. Even if you

258
00:12:06.039 --> 00:12:08.480
<v Speaker 2>divide integers like four to two, you get two point zero.

259
00:12:08.519 --> 00:12:10.679
<v Speaker 1>What if I want injured division, like just a whole

260
00:12:10.759 --> 00:12:11.480
<v Speaker 1>number part.

261
00:12:11.480 --> 00:12:14.279
<v Speaker 2>Use the double slash then, so seven to three gives two,

262
00:12:14.320 --> 00:12:16.240
<v Speaker 2>And if you want the remainder, use a modulle operator

263
00:12:16.279 --> 00:12:17.759
<v Speaker 2>percent seven percent three gives one.

264
00:12:17.799 --> 00:12:19.879
<v Speaker 1>Powers like squaring something, use.

265
00:12:19.799 --> 00:12:22.440
<v Speaker 2>The double asters collapse, so five to two is twenty five.

266
00:12:22.840 --> 00:12:25.320
<v Speaker 2>And there are shorthand operators like x plus x, which

267
00:12:25.360 --> 00:12:26.840
<v Speaker 2>is the same as x plus one.

268
00:12:27.039 --> 00:12:28.639
<v Speaker 1>Standard order operations applies.

269
00:12:28.759 --> 00:12:32.919
<v Speaker 2>Parentheses work yep, just like in regular math pemdas bodnus,

270
00:12:33.480 --> 00:12:37.159
<v Speaker 2>and for more complex math trig functions logs you import

271
00:12:37.200 --> 00:12:37.960
<v Speaker 2>the math module.

272
00:12:38.360 --> 00:12:41.840
<v Speaker 1>What about complex numbers useful in physics and engineering.

273
00:12:41.879 --> 00:12:45.720
<v Speaker 2>Sometimes Python has built in support for complex numbers. You

274
00:12:45.720 --> 00:12:47.960
<v Speaker 2>write them like three plus four J, where J is

275
00:12:47.960 --> 00:12:51.879
<v Speaker 2>the imaginary unit. All the standard complex arithmetic.

276
00:12:51.320 --> 00:12:53.519
<v Speaker 1>Works and true false values.

277
00:12:53.360 --> 00:12:56.639
<v Speaker 2>Those are boullions holy think they can only be true

278
00:12:56.679 --> 00:12:58.039
<v Speaker 2>or false. Note the capital letters.

279
00:12:58.039 --> 00:12:59.320
<v Speaker 1>How do we get bullion values?

280
00:12:59.399 --> 00:13:03.279
<v Speaker 2>Usually often from comparisons. You use operators like less than,

281
00:13:03.639 --> 00:13:05.919
<v Speaker 2>greater than, greater than, are equal to, less than are

282
00:13:05.960 --> 00:13:10.159
<v Speaker 2>equal to for equals right, double equals for testing equality

283
00:13:10.399 --> 00:13:13.480
<v Speaker 2>because single is for assignment and r is for not

284
00:13:13.559 --> 00:13:15.720
<v Speaker 2>equal to. These all return true or false.

285
00:13:15.840 --> 00:13:19.039
<v Speaker 1>Can you combine conditions like temperature is high, any pressure

286
00:13:19.080 --> 00:13:19.600
<v Speaker 1>is low.

287
00:13:19.519 --> 00:13:22.360
<v Speaker 2>Yep, using the logical operators and or and not.

288
00:13:22.720 --> 00:13:25.879
<v Speaker 1>Okay, that covers basic data types, but scientists often work

289
00:13:25.879 --> 00:13:28.759
<v Speaker 1>with collections of data points. How does Python handle that.

290
00:13:29.000 --> 00:13:32.000
<v Speaker 2>There are several ways, but the most fundamental container is

291
00:13:32.120 --> 00:13:32.600
<v Speaker 2>the list.

292
00:13:32.879 --> 00:13:33.279
<v Speaker 1>Lists.

293
00:13:33.600 --> 00:13:36.200
<v Speaker 2>Okay, A list is an ordered sequence of items. You

294
00:13:36.279 --> 00:13:40.200
<v Speaker 2>create it using square brackets with commas between items, like

295
00:13:40.320 --> 00:13:42.399
<v Speaker 2>my data is one point two one point five to

296
00:13:42.440 --> 00:13:43.000
<v Speaker 2>one point one.

297
00:13:43.519 --> 00:13:46.919
<v Speaker 1>Can lists hold different types of data like numbers and texts?

298
00:13:46.919 --> 00:13:50.600
<v Speaker 2>Absolutely mixed list equals one apple three point one four.

299
00:13:50.679 --> 00:13:53.879
<v Speaker 2>That's perfectly fine. You can even have lists inside other lists,

300
00:13:54.039 --> 00:13:54.840
<v Speaker 2>nested lists.

301
00:13:55.320 --> 00:13:56.840
<v Speaker 1>How do you find out how many items are in

302
00:13:56.879 --> 00:13:57.240
<v Speaker 1>a list?

303
00:13:57.480 --> 00:14:00.480
<v Speaker 2>Use the built in len function lend my data would

304
00:14:00.480 --> 00:14:01.559
<v Speaker 2>give you three in that example.

305
00:14:01.679 --> 00:14:03.879
<v Speaker 1>Are lists fixed once you create them? Or can you

306
00:14:03.960 --> 00:14:05.080
<v Speaker 1>change them? Add things?

307
00:14:05.320 --> 00:14:07.360
<v Speaker 2>They are mutable, meaning you can change them. You can

308
00:14:07.360 --> 00:14:09.080
<v Speaker 2>add an item to the end using the a pen

309
00:14:09.240 --> 00:14:11.879
<v Speaker 2>method like my data dot a pen one point four

310
00:14:12.279 --> 00:14:13.720
<v Speaker 2>That modifies the list directly.

311
00:14:13.759 --> 00:14:15.159
<v Speaker 1>If you join lists together.

312
00:14:15.000 --> 00:14:18.120
<v Speaker 2>YEP, using the plus operator list one plus list two

313
00:14:18.200 --> 00:14:20.279
<v Speaker 2>creates a new list with all the elements from both.

314
00:14:20.320 --> 00:14:22.360
<v Speaker 2>You can also repeat a list using one one two

315
00:14:22.360 --> 00:14:23.960
<v Speaker 2>three gives one two, one two one two.

316
00:14:24.159 --> 00:14:25.879
<v Speaker 1>How do you get a specific item out of a

317
00:14:25.919 --> 00:14:27.480
<v Speaker 1>list like the first one or the third one?

318
00:14:27.559 --> 00:14:30.480
<v Speaker 2>Using indexing, You put the index number in square brackets

319
00:14:30.519 --> 00:14:34.399
<v Speaker 2>after the list name. Crucially, Python uses zero based indexing,

320
00:14:34.480 --> 00:14:34.799
<v Speaker 2>so the.

321
00:14:34.759 --> 00:14:36.519
<v Speaker 1>First item is at index zero.

322
00:14:36.840 --> 00:14:39.120
<v Speaker 2>Correct might dad A zero would be one point two,

323
00:14:39.360 --> 00:14:41.519
<v Speaker 2>My data one would be one point five, and so on.

324
00:14:41.879 --> 00:14:43.600
<v Speaker 1>What about getting items from the end?

325
00:14:44.240 --> 00:14:47.480
<v Speaker 2>Python has negative indexing for that. My data one gives

326
00:14:47.480 --> 00:14:49.919
<v Speaker 2>you the last item, my data two gives a second

327
00:14:49.919 --> 00:14:52.000
<v Speaker 2>to last, et cetera. Very handy.

328
00:14:52.120 --> 00:14:54.000
<v Speaker 1>What if I want a range of items like the

329
00:14:54.000 --> 00:14:55.200
<v Speaker 1>second through fourth elements.

330
00:14:55.279 --> 00:14:59.120
<v Speaker 2>That's called slicing. The syntax is list namestart dot end.

331
00:14:59.360 --> 00:15:01.159
<v Speaker 2>It gives you an new list starting at the start

332
00:15:01.159 --> 00:15:03.639
<v Speaker 2>index up to but not including the end index.

333
00:15:03.919 --> 00:15:06.120
<v Speaker 1>So my data one point four would give items in

334
00:15:06.200 --> 00:15:08.240
<v Speaker 1>index one, two, and three exactly.

335
00:15:08.320 --> 00:15:10.600
<v Speaker 2>If you leave at start, it assumes zero. If you

336
00:15:10.679 --> 00:15:12.480
<v Speaker 2>leave out end, it goes to the end of the list.

337
00:15:12.600 --> 00:15:13.639
<v Speaker 1>Is there more to slicing?

338
00:15:13.720 --> 00:15:16.840
<v Speaker 2>You can add a step listenamestart dot end dot step,

339
00:15:17.000 --> 00:15:19.279
<v Speaker 2>So my list dot two would give you every second element,

340
00:15:19.440 --> 00:15:21.279
<v Speaker 2>and my listash one is a common trip to get

341
00:15:21.279 --> 00:15:22.600
<v Speaker 2>a reversed copy of the list.

342
00:15:22.679 --> 00:15:25.759
<v Speaker 1>Okay, lists seem very flexible. Are there built in functions

343
00:15:25.879 --> 00:15:27.279
<v Speaker 1>useful for lists in science?

344
00:15:27.480 --> 00:15:30.279
<v Speaker 2>Definitely? Some adds up all the numbers in a list,

345
00:15:30.279 --> 00:15:31.360
<v Speaker 2>but they all have to be numbers.

346
00:15:31.480 --> 00:15:33.759
<v Speaker 1>What about working with multiple lists together, Like if I

347
00:15:33.799 --> 00:15:36.679
<v Speaker 1>have time points in one list and measurements in another.

348
00:15:36.879 --> 00:15:41.200
<v Speaker 2>ZIP is perfect for that. Zip times measurements pairs up

349
00:15:41.240 --> 00:15:44.240
<v Speaker 2>the corresponding elements from each list into two poles. You

350
00:15:44.320 --> 00:15:47.159
<v Speaker 2>usually wrap it in list to see the result. Like

351
00:15:47.240 --> 00:15:49.159
<v Speaker 2>list zip times measurements.

352
00:15:49.320 --> 00:15:52.480
<v Speaker 1>That sounds really useful. What about filtering or transforming lists?

353
00:15:52.799 --> 00:15:55.720
<v Speaker 2>You have filter in map. Filter takes a function and

354
00:15:55.759 --> 00:15:58.159
<v Speaker 2>a list and keeps only the items where the function

355
00:15:58.200 --> 00:16:01.120
<v Speaker 2>returns true. Dot map take the function and applies it

356
00:16:01.159 --> 00:16:03.399
<v Speaker 2>to every item in the list, giving you a new

357
00:16:03.519 --> 00:16:04.840
<v Speaker 2>list of the results.

358
00:16:04.480 --> 00:16:07.399
<v Speaker 1>And their functions like all any all.

359
00:16:07.159 --> 00:16:09.960
<v Speaker 2>Checks if every item in a list is true or

360
00:16:09.960 --> 00:16:12.919
<v Speaker 2>evaluates to true, and any checks if at least one

361
00:16:12.960 --> 00:16:16.799
<v Speaker 2>item is true. Useful for checking conditions across data.

362
00:16:16.960 --> 00:16:19.480
<v Speaker 1>You mentioned Zip creates tuples. What are tuples? How are

363
00:16:19.519 --> 00:16:20.519
<v Speaker 1>they different from lists?

364
00:16:20.679 --> 00:16:23.679
<v Speaker 2>Tuples are very similar, but you create them with parentheses

365
00:16:23.720 --> 00:16:27.120
<v Speaker 2>instead of square brackets like point twenty to twenty. Indexing

366
00:16:27.159 --> 00:16:29.159
<v Speaker 2>and slicing work exactly the same way.

367
00:16:29.279 --> 00:16:30.159
<v Speaker 1>So what's the difference? Then?

368
00:16:30.440 --> 00:16:35.679
<v Speaker 2>The key difference? Tuples are immutable. Once you create a tuple,

369
00:16:35.919 --> 00:16:38.720
<v Speaker 2>you cannot change its contents. You can't append to it.

370
00:16:38.759 --> 00:16:39.799
<v Speaker 2>You can't change an element.

371
00:16:39.919 --> 00:16:42.919
<v Speaker 1>Why would you want something you can't change? Is it mutable? Better?

372
00:16:43.240 --> 00:16:47.240
<v Speaker 2>Immutability has its advantages. Sometimes you want to guarantee that

373
00:16:47.279 --> 00:16:51.200
<v Speaker 2>a collection of values won't be accidentally changed. Also, because

374
00:16:51.240 --> 00:16:54.919
<v Speaker 2>they're immutable, tuples can be used in places lists can't,

375
00:16:55.120 --> 00:16:58.399
<v Speaker 2>like as keys and dictionaries another data structure. Any performance

376
00:16:58.399 --> 00:17:03.320
<v Speaker 2>difference Sometimes ones maybe slightly less memory, but usually not

377
00:17:03.480 --> 00:17:06.359
<v Speaker 2>the main reason you choose one. It's more about signaling intent.

378
00:17:06.880 --> 00:17:09.039
<v Speaker 2>This group of values shouldn't change.

379
00:17:09.519 --> 00:17:11.480
<v Speaker 1>How do you make a tuple with just one item?

380
00:17:11.880 --> 00:17:13.200
<v Speaker 1>Item doesn't work? Does it right?

381
00:17:13.200 --> 00:17:15.359
<v Speaker 2>That just looks like parentheses. For grouping, you need a

382
00:17:15.400 --> 00:17:18.960
<v Speaker 2>trailing comma. Okay, looks a bit weird, but that's the syntax.

383
00:17:19.039 --> 00:17:22.519
<v Speaker 1>Okay. Lifts are mutable, Tuples are immutable. What about text

384
00:17:22.599 --> 00:17:25.400
<v Speaker 1>data reading, file labels notes?

385
00:17:25.599 --> 00:17:29.079
<v Speaker 2>That's where strings stent come in. There a's sequences of characters.

386
00:17:29.279 --> 00:17:32.559
<v Speaker 2>You cree them with either single quotes or double quotes.

387
00:17:32.920 --> 00:17:35.880
<v Speaker 1>Can you convert other things to strings like numbers?

388
00:17:35.960 --> 00:17:38.720
<v Speaker 2>Yep? The stem function does that? Startletar function? Does that

389
00:17:38.880 --> 00:17:41.000
<v Speaker 2>one two three? Gives you the string one two be three?

390
00:17:41.359 --> 00:17:44.279
<v Speaker 2>Very useful for printing output or saving data.

391
00:17:44.720 --> 00:17:46.799
<v Speaker 1>Is there a way to go the other way? String

392
00:17:46.839 --> 00:17:47.359
<v Speaker 1>to code?

393
00:17:47.519 --> 00:17:50.880
<v Speaker 2>There's Evil, which tries to run a string as Python code.

394
00:17:51.279 --> 00:17:54.039
<v Speaker 2>But you have to be really careful with Evil, especially

395
00:17:54.079 --> 00:17:56.720
<v Speaker 2>if the string comes from an external source, because it

396
00:17:56.720 --> 00:17:59.720
<v Speaker 2>can be a security risk. Generally best to avoid it

397
00:17:59.759 --> 00:18:01.400
<v Speaker 2>and you know exactly what you're doing.

398
00:18:01.480 --> 00:18:04.480
<v Speaker 1>Strings are mostly for impute output and format of printing.

399
00:18:04.160 --> 00:18:08.440
<v Speaker 2>Then, yeah, primarily, And the string object itself has tons

400
00:18:08.440 --> 00:18:12.440
<v Speaker 2>of useful built in methods for searching, replacing texts, splitting strings,

401
00:18:12.559 --> 00:18:13.759
<v Speaker 2>changing case, and so on.

402
00:18:14.319 --> 00:18:17.440
<v Speaker 1>All right, so we have data types containers. How do

403
00:18:17.440 --> 00:18:20.279
<v Speaker 1>we control what our code does? Like making decisions?

404
00:18:20.319 --> 00:18:23.440
<v Speaker 2>That's control flow. The most basic is the if statement

405
00:18:23.599 --> 00:18:26.720
<v Speaker 2>if condition followed by an indented block of code that

406
00:18:26.799 --> 00:18:28.359
<v Speaker 2>only runs if the condition is true.

407
00:18:28.599 --> 00:18:30.640
<v Speaker 1>What if the condition is false? Can you do something else?

408
00:18:30.880 --> 00:18:33.160
<v Speaker 2>Yes, you can add inn else dot block. And if

409
00:18:33.200 --> 00:18:36.400
<v Speaker 2>you have multiple conditions to check in sequence, use l

410
00:18:36.480 --> 00:18:39.519
<v Speaker 2>if short for else if if condition one, ale condition

411
00:18:39.559 --> 00:18:41.000
<v Speaker 2>two only one.

412
00:18:40.799 --> 00:18:44.759
<v Speaker 1>Block ever runs essential for handling different cases in experiments

413
00:18:44.839 --> 00:18:45.519
<v Speaker 1>or simulations.

414
00:18:45.559 --> 00:18:46.160
<v Speaker 2>Absolutely.

415
00:18:46.200 --> 00:18:48.279
<v Speaker 1>What about doing things repeatedly? Loops?

416
00:18:48.559 --> 00:18:51.680
<v Speaker 2>Two main loop types for and while before loop is

417
00:18:51.720 --> 00:18:54.599
<v Speaker 2>great for iterating over a sequence like stepping through each

418
00:18:54.680 --> 00:18:56.480
<v Speaker 2>item in a list or tuple.

419
00:18:56.720 --> 00:18:58.720
<v Speaker 1>For item in my list exactly.

420
00:18:58.440 --> 00:19:01.240
<v Speaker 2>The indented code block runs once for each item. You

421
00:19:01.279 --> 00:19:04.599
<v Speaker 2>can also loop over zipped sequences, like for tea in

422
00:19:04.920 --> 00:19:06.400
<v Speaker 2>zip time's measurements.

423
00:19:06.640 --> 00:19:09.160
<v Speaker 1>You mentioned range earlier. How does that fit with four loops?

424
00:19:09.480 --> 00:19:11.920
<v Speaker 2>Range is perfect when you just want to loop a

425
00:19:11.960 --> 00:19:15.240
<v Speaker 2>specific number of times. Range five effectively gives you the

426
00:19:15.279 --> 00:19:18.400
<v Speaker 2>sequence zero, one, two, three, four. So for I in

427
00:19:18.559 --> 00:19:22.119
<v Speaker 2>range five, will run the loop body five times with

428
00:19:22.279 --> 00:19:23.640
<v Speaker 2>I taking those values.

429
00:19:24.119 --> 00:19:27.079
<v Speaker 1>Very useful for fixed iterations. What about the wile loop.

430
00:19:27.279 --> 00:19:29.480
<v Speaker 2>A wile loop keeps running as long as its condition

431
00:19:29.680 --> 00:19:31.880
<v Speaker 2>stays true wile condition.

432
00:19:32.079 --> 00:19:34.359
<v Speaker 1>So you need to make sure the condition eventually becomes false.

433
00:19:34.480 --> 00:19:38.039
<v Speaker 2>Yes, otherwise you get an infinite loop and your program hangs.

434
00:19:38.119 --> 00:19:41.000
<v Speaker 2>You usually modify something inside the loop that affects the condition.

435
00:19:41.200 --> 00:19:42.759
<v Speaker 1>Do you ever break out of a loop early?

436
00:19:43.079 --> 00:19:46.559
<v Speaker 2>Yep, using the brake statement. If Python hits brake inside

437
00:19:46.599 --> 00:19:49.319
<v Speaker 2>a loop, it immediately jumps out and continues after the loop.

438
00:19:49.720 --> 00:19:52.400
<v Speaker 2>Useful if you've found what you needed or hit an error, and.

439
00:19:52.359 --> 00:19:55.440
<v Speaker 1>You mentioned list comprehensions again. A shortcut for creating lists.

440
00:19:55.640 --> 00:19:58.599
<v Speaker 2>Yeah, there are really neat concise way to build lists.

441
00:19:58.920 --> 00:20:01.279
<v Speaker 2>Instead of writing a full loop to create a list,

442
00:20:01.559 --> 00:20:04.559
<v Speaker 2>you can often do it in one line inside square.

443
00:20:04.200 --> 00:20:07.000
<v Speaker 1>Brackets, like by two for x in numbers to get

444
00:20:07.000 --> 00:20:08.519
<v Speaker 1>a list of squares exactly.

445
00:20:08.720 --> 00:20:11.400
<v Speaker 2>You can even add an if condition x for x

446
00:20:11.440 --> 00:20:14.759
<v Speaker 2>in numbers if backs zero to get only the positive numbers.

447
00:20:15.160 --> 00:20:17.960
<v Speaker 2>Very pythonic and often more readable once you get used

448
00:20:18.000 --> 00:20:18.240
<v Speaker 2>to them.

449
00:20:18.400 --> 00:20:22.559
<v Speaker 1>Okay, moving on the functions. They're key for reasonable code, right,

450
00:20:23.079 --> 00:20:24.559
<v Speaker 1>how do we make them in Python?

451
00:20:24.759 --> 00:20:28.680
<v Speaker 2>You use the deaf keyword deaf function name, parameters, then

452
00:20:28.720 --> 00:20:31.039
<v Speaker 2>an indented block for the function's code.

453
00:20:30.839 --> 00:20:32.960
<v Speaker 1>And the docks ring goes right after the deafline.

454
00:20:33.000 --> 00:20:37.119
<v Speaker 2>Good practice. Yes, explain what this function does, then the code.

455
00:20:37.359 --> 00:20:39.400
<v Speaker 2>If you want the function to give back a result,

456
00:20:39.480 --> 00:20:41.680
<v Speaker 2>you use the return statement return result.

457
00:20:41.799 --> 00:20:43.759
<v Speaker 1>What if there's no return statement, then the.

458
00:20:43.680 --> 00:20:46.960
<v Speaker 2>Function implicitly returns a special value called none.

459
00:20:47.279 --> 00:20:51.200
<v Speaker 1>And calling a function is just function name arguments.

460
00:20:50.839 --> 00:20:54.400
<v Speaker 2>YEP, providing the values arguments for the parameters defined in

461
00:20:54.440 --> 00:20:55.240
<v Speaker 2>the deafline.

462
00:20:55.400 --> 00:20:59.519
<v Speaker 1>Now a critical point for science. Can functions change the

463
00:20:59.559 --> 00:21:00.880
<v Speaker 1>data you pass into them?

464
00:21:01.200 --> 00:21:04.000
<v Speaker 2>It depends on the data type. If you pass immutable

465
00:21:04.079 --> 00:21:07.559
<v Speaker 2>things numbers, strings, tupples the function works on a copy.

466
00:21:07.640 --> 00:21:10.359
<v Speaker 2>It can't change the original outside the function, but.

467
00:21:10.319 --> 00:21:13.640
<v Speaker 1>If you pass mutable things like lists.

468
00:21:13.240 --> 00:21:16.759
<v Speaker 2>Then yes, the function can modify the original list directly.

469
00:21:17.079 --> 00:21:19.799
<v Speaker 2>This is called a side effect. Sometimes you want that,

470
00:21:19.920 --> 00:21:21.000
<v Speaker 2>sometimes it's a surprise.

471
00:21:21.079 --> 00:21:23.440
<v Speaker 1>So if you don't want the original list changed, you

472
00:21:23.440 --> 00:21:25.599
<v Speaker 1>should make a copy inside the function first.

473
00:21:25.759 --> 00:21:28.200
<v Speaker 2>That's the safe way to do it. Yes, use slicing

474
00:21:28.279 --> 00:21:31.559
<v Speaker 2>like localist input lists or the dot copy method.

475
00:21:31.759 --> 00:21:35.000
<v Speaker 1>Python seems flexible about function arguments. You mentioned positional and

476
00:21:35.119 --> 00:21:36.279
<v Speaker 1>keyword arguments. Right.

477
00:21:36.359 --> 00:21:40.079
<v Speaker 2>Positional arguments are matched by order. Keyword arguments you define

478
00:21:40.119 --> 00:21:43.920
<v Speaker 2>with a default value like my funk input threshold zero

479
00:21:44.000 --> 00:21:44.839
<v Speaker 2>point five, so.

480
00:21:44.799 --> 00:21:46.759
<v Speaker 1>You can call it like myfunk data and threshold to

481
00:21:46.839 --> 00:21:47.559
<v Speaker 1>b point five.

482
00:21:47.440 --> 00:21:51.240
<v Speaker 2>Yep, or you can override it myfunk's data threshold zero

483
00:21:51.240 --> 00:21:54.359
<v Speaker 2>point one. You can provide keyword arguments by name in

484
00:21:54.400 --> 00:21:55.160
<v Speaker 2>any order.

485
00:21:55.000 --> 00:21:56.359
<v Speaker 1>And those aregs and quorgs thing.

486
00:21:56.519 --> 00:21:59.000
<v Speaker 2>They let you create functions that accept any number of

487
00:21:59.079 --> 00:22:02.839
<v Speaker 2>extra positional are arguments, collects them into a tucle or

488
00:22:02.920 --> 00:22:06.279
<v Speaker 2>any number of extra keyword arguments quarks collects them into

489
00:22:06.319 --> 00:22:08.039
<v Speaker 2>a dictionary. Very flexible.

490
00:22:08.200 --> 00:22:14.480
<v Speaker 1>Okay, we have code functions, how do we get results out?

491
00:22:14.720 --> 00:22:15.880
<v Speaker 1>Displaying or saving.

492
00:22:16.160 --> 00:22:19.160
<v Speaker 2>Simplest display is the print function. We've used it implicitly

493
00:22:19.240 --> 00:22:20.319
<v Speaker 2>just print some value.

494
00:22:20.440 --> 00:22:22.440
<v Speaker 1>But often you want more control over the format right

495
00:22:22.480 --> 00:22:24.640
<v Speaker 1>like decimal places for numbers exactly.

496
00:22:24.640 --> 00:22:28.000
<v Speaker 2>That's string formatting. The modern recommended way is using the

497
00:22:28.039 --> 00:22:31.680
<v Speaker 2>dot format method on a string, or even better F strings.

498
00:22:31.680 --> 00:22:32.839
<v Speaker 1>How does dot format work.

499
00:22:33.079 --> 00:22:36.319
<v Speaker 2>You put curly braces as placeholders in your string the

500
00:22:36.400 --> 00:22:38.839
<v Speaker 2>value is and the limit is than you call format

501
00:22:38.880 --> 00:22:42.400
<v Speaker 2>on that string with the values dot format value.

502
00:22:42.200 --> 00:22:45.240
<v Speaker 1>Limit, Can you control the formatting inside the braces?

503
00:22:45.359 --> 00:22:49.599
<v Speaker 2>Yes, you can specify alignment with number of decimal places

504
00:22:49.640 --> 00:22:53.279
<v Speaker 2>for floats or two decimal places, scientific notation and more.

505
00:22:53.359 --> 00:22:56.559
<v Speaker 2>It's quite powerful. F strings f the value is valued,

506
00:22:56.559 --> 00:22:58.920
<v Speaker 2>DOT two s are often even more concise.

507
00:22:59.599 --> 00:23:02.880
<v Speaker 1>What about saving results to a file or reading data

508
00:23:02.880 --> 00:23:05.039
<v Speaker 1>from a file crucial for science.

509
00:23:05.400 --> 00:23:08.200
<v Speaker 2>Python has built in file handling. You start by using

510
00:23:08.200 --> 00:23:10.799
<v Speaker 2>the open function. You give the file name and.

511
00:23:10.759 --> 00:23:14.200
<v Speaker 1>A mode modes like R for read, W for wright.

512
00:23:14.319 --> 00:23:18.319
<v Speaker 2>Exactly, R for reading file must exist, W for writing

513
00:23:18.519 --> 00:23:22.200
<v Speaker 2>creates new file or overwrites existing A for appending to

514
00:23:22.240 --> 00:23:25.440
<v Speaker 2>the end of a file, add B for binary files

515
00:23:25.559 --> 00:23:26.839
<v Speaker 2>like RB or WEB.

516
00:23:27.160 --> 00:23:30.240
<v Speaker 1>So open gives you a file object yes.

517
00:23:30.519 --> 00:23:34.240
<v Speaker 2>And that object has methods like dot read to read

518
00:23:34.279 --> 00:23:37.400
<v Speaker 2>the whole file, dot redline to read one line, dot

519
00:23:37.480 --> 00:23:39.880
<v Speaker 2>red lines to read all lines into a list, and

520
00:23:39.960 --> 00:23:41.799
<v Speaker 2>dot write to write a string to the file.

521
00:23:41.839 --> 00:23:43.960
<v Speaker 1>And you absolutely must close the file afterwards.

522
00:23:44.279 --> 00:23:47.240
<v Speaker 2>Critically important use the dot close method if you forget

523
00:23:47.400 --> 00:23:49.440
<v Speaker 2>data might not get fully written, or you might run

524
00:23:49.440 --> 00:23:50.279
<v Speaker 2>out of resources.

525
00:23:50.400 --> 00:23:52.559
<v Speaker 1>Is there a safer way that handles closing automatically?

526
00:23:52.640 --> 00:23:55.799
<v Speaker 2>Yes, and it's strongly recommended use the with statement dot

527
00:23:55.839 --> 00:24:00.160
<v Speaker 2>with open data, dot txdrs F you work with the

528
00:24:00.200 --> 00:24:03.960
<v Speaker 2>file object f inside the indented block. When the block finishes,

529
00:24:04.279 --> 00:24:07.680
<v Speaker 2>Python automatically closes the file even if errors happened.

530
00:24:07.880 --> 00:24:09.880
<v Speaker 1>Uh. The context manager much safer.

531
00:24:09.960 --> 00:24:11.039
<v Speaker 2>Definitely the preferred way.

532
00:24:11.319 --> 00:24:15.200
<v Speaker 1>Okay. Inevitably things go wrong. Code has errors. How does

533
00:24:15.240 --> 00:24:16.519
<v Speaker 1>Python react.

534
00:24:16.440 --> 00:24:19.960
<v Speaker 2>When an error occurs? Python raises an exception if you

535
00:24:19.960 --> 00:24:22.599
<v Speaker 2>don't do anything about it. The default behavior is to

536
00:24:22.640 --> 00:24:25.440
<v Speaker 2>print an error message the trace back and stop the program.

537
00:24:25.599 --> 00:24:27.680
<v Speaker 1>What kind of errors might a scientist run into?

538
00:24:27.960 --> 00:24:32.119
<v Speaker 2>Lots? Indentation error is common for beginners. Python cares about

539
00:24:32.160 --> 00:24:35.559
<v Speaker 2>spaces type error if you try to say, add a

540
00:24:35.640 --> 00:24:38.519
<v Speaker 2>number in a string. Name error. If you use a

541
00:24:38.559 --> 00:24:42.119
<v Speaker 2>variable you haven't defined value error if a function gets

542
00:24:42.160 --> 00:24:44.599
<v Speaker 2>the right type but a bad value, like trying to

543
00:24:44.599 --> 00:24:49.039
<v Speaker 2>convert zxyz to an integer. Zero division error is obvious.

544
00:24:49.240 --> 00:24:51.839
<v Speaker 2>Index error if you try to access a list element

545
00:24:51.880 --> 00:24:55.039
<v Speaker 2>that doesn't exist. File not founder if open can't find

546
00:24:55.039 --> 00:24:57.079
<v Speaker 2>the file. Import error if it can't find a module

547
00:24:57.079 --> 00:24:57.880
<v Speaker 2>you try to import.

548
00:24:58.000 --> 00:24:59.839
<v Speaker 1>Can you stop the program from just crashing when these

549
00:25:00.559 --> 00:25:01.519
<v Speaker 1>maybe try something else?

550
00:25:01.640 --> 00:25:04.079
<v Speaker 2>Yes, that's what tri dot except blocks are for. You

551
00:25:04.160 --> 00:25:06.519
<v Speaker 2>put the code that might cause an error inside the

552
00:25:06.559 --> 00:25:08.880
<v Speaker 2>try dot block, and then you have one or more

553
00:25:08.920 --> 00:25:13.119
<v Speaker 2>accept blocks. If an exception occurs in the triblock, Python

554
00:25:13.160 --> 00:25:15.240
<v Speaker 2>looks for an accept block that matches the type of

555
00:25:15.279 --> 00:25:18.400
<v Speaker 2>exception raised. If it finds one, it runs the code

556
00:25:18.400 --> 00:25:20.920
<v Speaker 2>in that accept block instead of crashing.

557
00:25:20.640 --> 00:25:24.079
<v Speaker 1>So you can handle the error gracefully, log it, try again,

558
00:25:24.240 --> 00:25:26.319
<v Speaker 1>provide a default value exactly.

559
00:25:26.359 --> 00:25:28.960
<v Speaker 2>It lets you build more robust programs that don't fall

560
00:25:29.000 --> 00:25:30.200
<v Speaker 2>over at the first hurdle.

561
00:25:30.640 --> 00:25:34.960
<v Speaker 1>Now, the book also introduces classes. This sounds a bit

562
00:25:34.960 --> 00:25:36.240
<v Speaker 1>more advanced. What's the idea.

563
00:25:36.359 --> 00:25:39.720
<v Speaker 2>Classes are a way to create your own custom object types.

564
00:25:40.279 --> 00:25:43.640
<v Speaker 2>Think of them as blueprints for creating objects that bundle

565
00:25:43.720 --> 00:25:47.839
<v Speaker 2>together data called attributes and functions that operate on that data,

566
00:25:48.000 --> 00:25:48.799
<v Speaker 2>called methods.

567
00:25:49.119 --> 00:25:50.920
<v Speaker 1>Why would a scientist use classes?

568
00:25:51.079 --> 00:25:53.799
<v Speaker 2>You could model things from your research. Maybe create a

569
00:25:53.839 --> 00:25:57.599
<v Speaker 2>molecule class with attributes like atoms and bonds and methods

570
00:25:57.640 --> 00:26:00.839
<v Speaker 2>to calculate its mass or energy. It helps organized complex

571
00:26:00.920 --> 00:26:02.440
<v Speaker 2>data and related operations.

572
00:26:02.640 --> 00:26:03.559
<v Speaker 1>How do you define one?

573
00:26:03.720 --> 00:26:07.480
<v Speaker 2>Using the class keyword class my object? Inside you often

574
00:26:07.480 --> 00:26:10.519
<v Speaker 2>define a special method called innit that's the constructor. It

575
00:26:10.599 --> 00:26:12.839
<v Speaker 2>runs when you create a new instance of the class

576
00:26:12.839 --> 00:26:14.640
<v Speaker 2>and usually sets up the initial attributes.

577
00:26:14.759 --> 00:26:17.640
<v Speaker 1>The book had that frack example for fractions right with

578
00:26:17.799 --> 00:26:18.680
<v Speaker 1>AD and mole.

579
00:26:18.480 --> 00:26:21.240
<v Speaker 2>Methods exactly, and it used other special methods like repro

580
00:26:21.440 --> 00:26:24.160
<v Speaker 2>to control how the fraction object gets printed.

581
00:26:23.799 --> 00:26:25.880
<v Speaker 1>And things like AD and moll right.

582
00:26:25.920 --> 00:26:30.200
<v Speaker 2>Those allow operator overloading. By defining AD, you tell Python

583
00:26:30.279 --> 00:26:32.880
<v Speaker 2>what the plus operator should do when used with your

584
00:26:32.880 --> 00:26:36.680
<v Speaker 2>Frack objects. It makes your custom objects behave more intuitively

585
00:26:36.839 --> 00:26:37.720
<v Speaker 2>like built in types.

586
00:26:37.920 --> 00:26:40.160
<v Speaker 1>Very cool. Let's look back to modules. You said they're

587
00:26:40.240 --> 00:26:41.519
<v Speaker 1>key for scientific work.

588
00:26:41.640 --> 00:26:45.799
<v Speaker 2>Absolutely central. Most of the powerful scientific tools NUMBPI for arrays,

589
00:26:45.880 --> 00:26:50.319
<v Speaker 2>SIPI for algorithms, matt plotlib for plotting, PANDAS for data

590
00:26:50.319 --> 00:26:53.319
<v Speaker 2>analysis are external libraries you access as modules.

591
00:26:53.319 --> 00:26:55.279
<v Speaker 1>So a module is just a dot pi file with.

592
00:26:55.319 --> 00:26:59.640
<v Speaker 2>Code essentially Yes, it contains functions, classes, variables. You use

593
00:26:59.680 --> 00:27:03.000
<v Speaker 2>the import statement to bring that code into your program's namespace,

594
00:27:03.200 --> 00:27:06.400
<v Speaker 2>like import math YEP. Then you access things inside it

595
00:27:06.559 --> 00:27:09.960
<v Speaker 2>using the dot dot math dot pi math dot sinx.

596
00:27:10.440 --> 00:27:13.880
<v Speaker 2>This dot mechanism is fundamental in Python for accessing attributes

597
00:27:13.920 --> 00:27:15.599
<v Speaker 2>and methods of objects and modules.

598
00:27:15.640 --> 00:27:18.440
<v Speaker 1>Can you import just one thing or give the module

599
00:27:18.480 --> 00:27:19.160
<v Speaker 1>a shorter name.

600
00:27:19.400 --> 00:27:22.839
<v Speaker 2>Yes. From math import score, lets you use squort directly

601
00:27:23.359 --> 00:27:27.440
<v Speaker 2>and import NumPy as NP is super common. It imports numpi,

602
00:27:27.480 --> 00:27:29.920
<v Speaker 2>but lets you refer to it as MP, saving typing

603
00:27:30.440 --> 00:27:33.279
<v Speaker 2>NP dot ray, NP, dot mean, et cetera.

604
00:27:33.759 --> 00:27:37.039
<v Speaker 1>Mastering that import in dot mechanism seems crucial, then.

605
00:27:37.000 --> 00:27:39.920
<v Speaker 2>It really is key to using Python effectively for science.

606
00:27:40.039 --> 00:27:43.240
<v Speaker 1>Finally, the book mentions PEP eight the style Guide. Why

607
00:27:43.279 --> 00:27:46.000
<v Speaker 1>worry about style isn't getting the right answer the main thing.

608
00:27:46.200 --> 00:27:48.759
<v Speaker 2>Getting the right answer is critical, of course, but how

609
00:27:48.799 --> 00:27:52.359
<v Speaker 2>you write the code matters hugely, especially in science. PP

610
00:27:52.440 --> 00:27:55.599
<v Speaker 2>eight is about making code readable, consistent, and understandable.

611
00:27:55.680 --> 00:27:57.440
<v Speaker 1>Why is readability so important?

612
00:27:57.599 --> 00:28:01.519
<v Speaker 2>Remember the zen of Python readability count. Code is read

613
00:28:01.559 --> 00:28:04.200
<v Speaker 2>far more often than it's written. If you're collaborating, others

614
00:28:04.200 --> 00:28:06.599
<v Speaker 2>need to understand your code. If you revisit your own

615
00:28:06.640 --> 00:28:09.680
<v Speaker 2>code months later, you need to understand it. Clear. Consistent

616
00:28:09.720 --> 00:28:12.200
<v Speaker 2>code aids transparency and reproducibility.

617
00:28:12.279 --> 00:28:14.680
<v Speaker 1>So what are some key PP eight guidelines?

618
00:28:14.920 --> 00:28:19.400
<v Speaker 2>Things like using four spaces for indentation, never tabs, limiting

619
00:28:19.440 --> 00:28:21.759
<v Speaker 2>line length to make it easy to read around seventy

620
00:28:21.839 --> 00:28:25.720
<v Speaker 2>nine or maybe a bit more characters, using meaningful variable names,

621
00:28:26.279 --> 00:28:30.920
<v Speaker 2>using blank lines to group logical sections, consistent spacing around operators,

622
00:28:31.400 --> 00:28:34.960
<v Speaker 2>adding comments where needed, but not over commenting obvious things.

623
00:28:35.160 --> 00:28:37.519
<v Speaker 1>So it's worth taking the time to learn and follow.

624
00:28:37.279 --> 00:28:40.079
<v Speaker 2>These Definitely it makes you a better programmer and makes

625
00:28:40.079 --> 00:28:42.680
<v Speaker 2>your scientific code more valuable in the long run. The

626
00:28:42.680 --> 00:28:44.119
<v Speaker 2>book links to the full guide.

627
00:28:44.200 --> 00:28:45.839
<v Speaker 1>Okay, wow, we've covered a lot.

628
00:28:45.920 --> 00:28:48.960
<v Speaker 2>Yeah, quite a tour. So just to recap, we've seen

629
00:28:48.960 --> 00:28:53.039
<v Speaker 2>why Python is so big in science. It's accessible, flexible, explicit.

630
00:28:53.359 --> 00:28:57.519
<v Speaker 2>We hit the basics data types like numbers, strings, lists, tuples,

631
00:28:57.920 --> 00:29:01.039
<v Speaker 2>control flow with if and loops, building or usable code

632
00:29:01.079 --> 00:29:04.920
<v Speaker 2>with functions, handling files and errors, even dipped into classes,

633
00:29:04.920 --> 00:29:07.559
<v Speaker 2>and the vital role of modules like NUMPI.

634
00:29:07.359 --> 00:29:10.240
<v Speaker 1>And the importance of clean, readable code. With PP eight,

635
00:29:10.640 --> 00:29:13.920
<v Speaker 1>it really feels like a solid foundation for tackling scientific programming.

636
00:29:13.960 --> 00:29:17.200
<v Speaker 2>Absolutely. Python gives you the tools to handle complex data,

637
00:29:17.279 --> 00:29:21.279
<v Speaker 2>build models, test hypotheses, all that good scientific stuff.

638
00:29:21.160 --> 00:29:24.160
<v Speaker 1>And the book and the official Python docs online are

639
00:29:24.240 --> 00:29:26.559
<v Speaker 1>great places to go next for more details, for.

640
00:29:26.599 --> 00:29:32.200
<v Speaker 2>Sure, the documentation for lists, control flow functions, file io errors,

641
00:29:32.400 --> 00:29:37.119
<v Speaker 2>classes PEP eight. It's all there and very comprehensive, definitely

642
00:29:37.160 --> 00:29:37.880
<v Speaker 2>worth exploring.

643
00:29:38.000 --> 00:29:39.559
<v Speaker 1>All right, So here's something to chew on as we

644
00:29:39.599 --> 00:29:42.160
<v Speaker 1>wrap up. Take a look at how you currently work

645
00:29:42.720 --> 00:29:46.759
<v Speaker 1>your analysis you're modeling. Where could Python fit in? Could

646
00:29:46.759 --> 00:29:49.039
<v Speaker 1>it automate some tedious stuff? Could it let you analyze

647
00:29:49.079 --> 00:29:52.279
<v Speaker 1>data in new ways or build more complex models than

648
00:29:52.279 --> 00:29:52.839
<v Speaker 1>you could before?

649
00:29:52.920 --> 00:29:55.279
<v Speaker 2>Yeah, think about how it might streamline things or even

650
00:29:55.359 --> 00:29:58.160
<v Speaker 2>open up entirely new scientific questions you could tackle.

651
00:29:58.359 --> 00:30:00.640
<v Speaker 1>Food for thought. Thanks for joining us for this deep dive.
