Latest / CyberSecurity Summary / Python for Scientists
Transcript
- 0:00Speaker 1: Welcome to the deep dive. So, if you're a scientist,
- 0:03Speaker 1: and we meet that pretty broadly, right, anyone using numbers,
- 0:07Speaker 1: models to figure things out.
- 0:08Speaker 2: Yeah, analyzing data, simulating theories, asking those big what if questions.
- 0:13Speaker 1: Exactly, and you're maybe curious about using Python, Well, you're
- 0:16Speaker 1: definitely in the right place today. Yeah.
- 0:18Speaker 2: Today we're digging into Michael Mommert's Python for Scientists, the
- 0:22Speaker 2: third edition. We're basically pulling out the essentials for you.
- 0:26Speaker 1: Think of this as the fast track, getting you the
- 0:28Speaker 1: core stuff you need to know about Python in science
- 0:31Speaker 1: without you know, getting totally lost in the weeds.
- 0:34Speaker 2: We're aiming to highlight what's actually useful for your scientific work.
- 0:37Speaker 1: Okay, so let's dive in Python for scientists. What are
- 0:41Speaker 1: we really talking about here? First off, just to be clear,
- 0:44Speaker 1: it's the programming language, not the snake.
- 0:46Speaker 2: Yeah, yeah, important distinction. And scientists, it's not just lab codes, right.
- 0:51Speaker 1: If you're using quantitative models, processing data, asking what if
- 0:56Speaker 1: with numbers.
- 0:57Speaker 2: And this is for you. It's amazing how much modern
- 1:00Speaker 2: science relies on computation.
- 1:01Speaker 1: Now totally experiments are getting huge, data sets are massive.
- 1:07Speaker 1: You just can't do it by hand anymore.
- 1:08Speaker 2: And our theories are getting more complex too, needing computers
- 1:12Speaker 2: to really explore them. You basically need computational tools to
- 1:15Speaker 2: do science today.
- 1:16Speaker 1: But here's a curious thing the book points out. You'd
- 1:20Speaker 1: think with all this powerful hardware, there'd be tons of
- 1:23Speaker 1: amazing specialized software for scientists just readily available.
- 1:27Speaker 2: But that's not always the case, is it.
- 1:29Speaker 1: No, And a big reason is just market size. Scientists, well,
- 1:32Speaker 1: we're a relatively small market compared to say, gamers or businesses,
- 1:36Speaker 1: so the.
- 1:37Speaker 2: Big money isn't always there to constantly push the boundaries
- 1:40Speaker 2: of specialized scientific software, even though these tools are like
- 1:44Speaker 2: essential for us, right.
- 1:45Speaker 1: Which brings us to a key choice using proprietary versus
- 1:48Speaker 1: open source software.
- 1:49Speaker 2: Okay, yeah, proprietary stuff. That's the commercial software companies make it.
- 1:53Speaker 2: They've costs, payroll shareholders, so you've got to pay for
- 1:56Speaker 2: it exactly, and often the actual code, the source code
- 1:59Speaker 2: is secret. It's their intellectual property.
- 2:02Speaker 1: Which means you can't easily customize it for your specific
- 2:05Speaker 1: maybe weird research needs.
- 2:07Speaker 2: Right, and the book mentions this daneingel de effect ah yes,
- 2:12Speaker 2: where because they need ongoing revenue, you often find yourself
- 2:16Speaker 2: paying for frequent updates or new versions just to keep
- 2:20Speaker 2: using it effectively.
- 2:21Speaker 1: Then you've got the flip side. Open source.
- 2:24Speaker 2: Yeah, open source software is usually free. It's often built
- 2:27Speaker 2: by communities, maybe researchers at universities.
- 2:29Speaker 1: People sharing tools with colleagues basically.
- 2:32Speaker 2: And the licensing is different too. It's usually designed so
- 2:34Speaker 2: no one company can just take it over and lock
- 2:37Speaker 2: it down anti copyright.
- 2:39Speaker 1: Often then you might think, okay, free software made by volunteers,
- 2:44Speaker 1: it can't be as good as the paid stuff.
- 2:46Speaker 2: Right, That's a common thought, but the book really pushes
- 2:48Speaker 2: back on that. It argues that actually, in many scientific fields,
- 2:52Speaker 2: open source tools are incredibly powerful. Sometimes they even lead
- 2:55Speaker 2: the way.
- 2:56Speaker 1: Interesting. So, okay, we have this proprietary versus open source split.
- 3:00Speaker 1: But the book also talks about different kinds of scientific software.
- 3:03Speaker 2: Right, so you have what you might call scientific software tools.
- 3:06Speaker 2: These are kind of prepackaged applications.
- 3:08Speaker 1: Like matt Lab or even Excel on the commercial.
- 3:11Speaker 2: Side exactly, and open source ones like are again Octave,
- 3:15Speaker 2: Libre Office. Some have nice graphical user interfaces guys, you know,
- 3:21Speaker 2: click buttons, get results.
- 3:23Speaker 1: Makes common stuff easy, definitely.
- 3:25Speaker 2: But that ease of use can mean less flexibility if
- 3:29Speaker 2: you want to do something really specific the designers didn't
- 3:31Speaker 2: think of.
- 3:32Speaker 1: It can be tough, Okay.
- 3:34Speaker 2: Other tools try to bridge that gap. They might have
- 3:36Speaker 2: a user interface but also include their own built in
- 3:39Speaker 2: programming language.
- 3:41Speaker 1: Like interpreters. They run your code bit by bit precisely.
- 3:44Speaker 2: It gives you more power than just a GUI.
- 3:46Speaker 1: But underneath all these tools, there are the actual programming languages, right,
- 3:49Speaker 1: the fundamental building blocks.
- 3:51Speaker 2: Absolutely. Languages like C and Fotran have been huge in
- 3:55Speaker 2: science historically. They're often called compiled languages.
- 3:59Speaker 1: Compiled language, Yeah, the book uses that racing car analogy.
- 4:02Speaker 2: Right, Yeah, like a formula one car. They can be
- 4:04Speaker 2: incredibly fast because your code gets translated directly into efficient
- 4:08Speaker 2: machine instructions by a compiler. Super powerful, But like driving
- 4:12Speaker 2: enough one car, using them well takes a lot of
- 4:15Speaker 2: skill and sometimes getting things working across different compilers can
- 4:18Speaker 2: be tricky. Plus, you often need lots of external libraries
- 4:21Speaker 2: for basic stuff.
- 4:22Speaker 1: So why don't scientists just use these super fast languages
- 4:26Speaker 1: for everything?
- 4:27Speaker 2: Well, back to the analogy. You wouldn't take enough one
- 4:30Speaker 2: car to pop to the shops, would you?
- 4:32Speaker 1: Eh? Probably not? Speed isn't everything there exactly?
- 4:36Speaker 2: Compiled languages aren't always the best for quickly trying out
- 4:39Speaker 2: an idea or writing a simple script for a daily task.
- 4:42Speaker 1: Okay, so what's the alternative for those everyday tasks?
- 4:45Speaker 2: That's where interpreted languages shine. We mentioned them already. They
- 4:48Speaker 2: often power those more flexible software tools.
- 4:51Speaker 1: It might not be quite as fast as the compiled.
- 4:53Speaker 2: Ones generally, no, but they're often much easier to learn
- 4:57Speaker 2: and use. For a lot of scientists, especially for scripting
- 5:01Speaker 2: or getting started, they're way more approachable.
- 5:03Speaker 1: And this is where Python comes in, right, It's huge now.
- 5:05Speaker 2: It really is especially popular with younger researchers, it seems.
- 5:09Speaker 1: So why what makes Python so attractive for science?
- 5:12Speaker 2: A big reason the book highlights is this idea of explicitness.
- 5:16Speaker 1: Explicitness what does that mean in code?
- 5:18Speaker 2: Okay, imagine you need to read data from say, three
- 5:21Speaker 2: different file types. You could try writing one super complicated
- 5:25Speaker 2: function to handle all three, right, or the Pythonic way,
- 5:29Speaker 2: the sort of encouraged style is to write three separate,
- 5:32Speaker 2: clear functions. One function explicitly handles format A, another handles
- 5:37Speaker 2: format B, and so on.
- 5:38Speaker 1: Ah. Okay, one function, one.
- 5:41Speaker 2: Job exactly And why is that good?
- 5:44Speaker 1: I guess it's easier to understand what's going on less confusing.
- 5:47Speaker 2: Precisely, it's easier for collaborators to read, easier for you
- 5:51Speaker 2: to read six months later when you've forgotten the details,
- 5:54Speaker 2: and in science that kind of clarity and reproducibility is vital.
- 5:57Speaker 1: Makes sense. Okay, So let's say you're sold on Python.
- 6:00Speaker 1: How do you actually get started? Like? Install it?
- 6:02Speaker 2: Right installation? The book mentions you might already have Python
- 6:06Speaker 2: on your.
- 6:06Speaker 1: System, but you shouldn't mess with that one.
- 6:08Speaker 2: Probably best not to, Yeah, it might break other things.
- 6:11Speaker 2: The recommendation is to install something called Anaconda. It's a
- 6:14Speaker 2: distribution of Python that comes bundled with a tool called Conda,
- 6:17Speaker 2: and Conda is fantastic for managing different programming environments environments.
- 6:22Speaker 1: Why do I need different environments?
- 6:24Speaker 2: Well, imagine you're working on project X and it needs
- 6:27Speaker 2: version one point zero of a specific library, but project
- 6:31Speaker 2: Y needs version two point zero of the same library.
- 6:33Speaker 1: Ah conflicts exactly.
- 6:36Speaker 2: Conda lets you create isolated sandboxes or environments. Project X
- 6:41Speaker 2: gets its own environment with its specific libraries. Project Y
- 6:44Speaker 2: gets its own. Nothing clashes.
- 6:47Speaker 1: Okay, that sounds super useful for keeping research projects separate
- 6:50Speaker 1: and reproducible.
- 6:51Speaker 2: It's essential, really, and installing packages into an environment is
- 6:54Speaker 2: easy with Conda. Just Conda install and the package name
- 6:57Speaker 2: like cond install NumPy.
- 6:59Speaker 1: In these pack like NumPy. Where do they come from?
- 7:01Speaker 1: Where they like add ons?
- 7:03Speaker 2: Sort of?
- 7:04Speaker 1: Yeah?
- 7:04Speaker 2: Think of them as libraries of pre written code for
- 7:06Speaker 2: specific tasks. Numb Pi is fundamental for numerical stuff and
- 7:10Speaker 2: most packages scientists need are available through Conda.
- 7:13Speaker 1: Where does condfind them?
- 7:14Speaker 2: A major source is Conda Forge. It's a huge community
- 7:18Speaker 2: effort maintaining recipes to build thousands of packages. It's a
- 7:22Speaker 2: great place to look for scientific tools.
- 7:24Speaker 1: Okay, installed via Anaconda using Conda environments, how do I
- 7:28Speaker 1: actually write and run Python code?
- 7:30Speaker 2: Then the book really focuses on Jupiter notebooks, and honestly
- 7:34Speaker 2: they're a fantastic tool for scientists. The book itself is
- 7:37Speaker 2: written as notebooks jupitern notebooks.
- 7:40Speaker 1: What's special about them.
- 7:41Speaker 2: They let you mix live, runnable code with text, equations,
- 7:45Speaker 2: plots all in one document. You can run them in
- 7:47Speaker 2: your web browser, either locally or in the cloud.
- 7:50Speaker 1: So it's not just code. It's like an interactive lab notebook.
- 7:54Speaker 2: That's a great way to put it. It integrates your code,
- 7:56Speaker 2: your explanations, and your results together. Really powerful for documents
- 8:00Speaker 2: and sharing your work.
- 8:01Speaker 1: What are the main parts of a notebook?
- 8:03Speaker 2: They're made up of cells. You have code cells for
- 8:05Speaker 2: your Python code, which you can run individually.
- 8:08Speaker 1: And see the output right away.
- 8:09Speaker 2: Yep, right underneath the cell. Then you have markedown cells.
- 8:12Speaker 2: Markdown is a simple way to format text headings lists
- 8:15Speaker 2: bold italics.
- 8:16Speaker 1: So you can write explanations right alongside the chemical exactly.
- 8:20Speaker 2: You can even put in mathematical equations using latex notation.
- 8:23Speaker 2: It encourages you to explain what you're doing and why
- 8:26Speaker 2: as you go.
- 8:27Speaker 1: That sounds incredibly useful for keeping track of things.
- 8:29Speaker 2: It really is. And inside the code cells, you can
- 8:32Speaker 2: even run special commands called magic commands that interact with
- 8:36Speaker 2: the operating system or control the notebook itself. They start
- 8:39Speaker 2: with eight percent.
- 8:40Speaker 1: Or percent magic commands like what you can.
- 8:43Speaker 2: List files, change directories, time your code. Things like that.
- 8:47Speaker 2: Typing percent magic gives you the full rundown and percents
- 8:50Speaker 2: magic gives a quick list.
- 8:51Speaker 1: Cool. Okay, so we're in a notebook. What about the
- 8:54Speaker 1: basic Python language elements a scientist needs to grasp right fundamentals.
- 8:59Speaker 2: Let's start with com comments, essential for explaining your code.
- 9:02Speaker 1: How do you write comments two main ways.
- 9:04Speaker 2: Single line comments start with a hash symbol hashtag. Anything
- 9:07Speaker 2: after the hashtag on that line is ignored by Python.
- 9:10Speaker 2: And then there are dock strings. These are multiline strings,
- 9:13Speaker 2: usually enclosed in triple quotes like this is a dock string?
- 9:16Speaker 1: What are they used for?
- 9:17Speaker 2: They're typically put right at the start of a function
- 9:20Speaker 2: or a class to explain what it does. It's inputs,
- 9:22Speaker 2: its outputs really important for documentation.
- 9:24Speaker 1: Got it? What about the actual data we work with?
- 9:27Speaker 1: Numbers text?
- 9:29Speaker 2: In Python? Basically everything is an object. A number is
- 9:33Speaker 2: an object, A piece of text is an object, a
- 9:35Speaker 2: list is an object. Each object has a type like integer, float, string,
- 9:40Speaker 2: and an identity, which is basically its address and memory.
- 9:44Speaker 1: We don't usually worry about the memory address though right
- 9:46Speaker 1: not directly.
- 9:47Speaker 2: No, Instead we use identifiers. These are just names or
- 9:51Speaker 2: variables that we assign to point to these objects. X
- 9:53Speaker 2: equals five means the identifier X now refers to the
- 9:57Speaker 2: integer object five.
- 9:58Speaker 1: Are there rules for naming these identifiers? Yep.
- 10:01Speaker 2: They have to start with a letter or an underscore.
- 10:03Speaker 2: After that, they can have letters, numbers, or underscores.
- 10:06Speaker 1: In case matters data is different from.
- 10:08Speaker 2: Data absolutely case sensitive Also, names starting or ending with
- 10:13Speaker 2: underscores often have special meanings, so maybe you avoid those
- 10:16Speaker 2: at first, and you can't use pythons on keywords like
- 10:18Speaker 2: if for list int as names.
- 10:22Speaker 1: Okay, and using clear descriptive names is good practice. I
- 10:25Speaker 1: assume like reaction rate instead of just.
- 10:27Speaker 2: Are readability accounts, especially in science.
- 10:29Speaker 1: So when we write x equals five, you said that's assignment.
- 10:33Speaker 1: Can you elaborate on that? It's not quite like algebra's
- 10:36Speaker 1: equals sign exactly.
- 10:37Speaker 2: It's crucial to get this in Python means assign the
- 10:41Speaker 2: object on the right to the identifier on the left.
- 10:44Speaker 2: It's an instruction, not a statement of fact. The object
- 10:48Speaker 2: five has the type int, not the.
- 10:50Speaker 1: Name X, and I could later say X hello, yep.
- 10:52Speaker 2: You can reassign x to point to a completely different object,
- 10:55Speaker 2: even one of a different type. The name just points
- 10:57Speaker 2: wherever you tell it to.
- 10:59Speaker 1: How does Python and keep track of all these names
- 11:01Speaker 1: and what they point to?
- 11:02Speaker 2: It uses name spaces. Think of a name space as
- 11:05Speaker 2: a dictionary mapping names identifiers to objects. When you're working
- 11:09Speaker 2: interactively or in a script, there's a main name space
- 11:12Speaker 2: called main. When you import modules, they get their own
- 11:15Speaker 2: name spaces too.
- 11:16Speaker 1: Like folders for names to avoid clashes.
- 11:19Speaker 2: That's a good analogy. Yeah, it keeps things organized.
- 11:21Speaker 1: Okay, what about different kinds of numbers? Scientists use lots
- 11:24Speaker 1: of those.
- 11:24Speaker 2: Python has the basics covered. You've got integers, int, whole
- 11:27Speaker 2: numbers positive negative, zero, and in modern Python they can
- 11:32Speaker 2: be basically as big as you need.
- 11:33Speaker 1: Count equals one hundred, no maximum size practically speaking, not.
- 11:37Speaker 2: Really, which is nice. Then for numbers with decimal points,
- 11:40Speaker 2: you have floating point numbers or floats.
- 11:42Speaker 1: Floats like measurements.
- 11:44Speaker 2: Exactly temperature ecals twenty five point five. They typically have
- 11:47Speaker 2: about sixteen digits of precision. You can write them normally
- 11:51Speaker 2: or using scientific notation like speedo flight it calls three.
- 11:55Speaker 1: E eight and doing math addition subtraction.
- 11:58Speaker 2: All the standard operators work plus and for multiplication. For division,
- 12:03Speaker 2: one thing always gives a float result. Even if you
- 12:06Speaker 2: divide integers like four to two, you get two point zero.
- 12:08Speaker 1: What if I want injured division, like just a whole
- 12:10Speaker 1: number part.
- 12:11Speaker 2: Use the double slash then, so seven to three gives two,
- 12:14Speaker 2: And if you want the remainder, use a modulle operator
- 12:16Speaker 2: percent seven percent three gives one.
- 12:17Speaker 1: Powers like squaring something, use.
- 12:19Speaker 2: The double asters collapse, so five to two is twenty five.
- 12:22Speaker 2: And there are shorthand operators like x plus x, which
- 12:25Speaker 2: is the same as x plus one.
- 12:27Speaker 1: Standard order operations applies.
- 12:28Speaker 2: Parentheses work yep, just like in regular math pemdas bodnus,
- 12:33Speaker 2: and for more complex math trig functions logs you import
- 12:37Speaker 2: the math module.
- 12:38Speaker 1: What about complex numbers useful in physics and engineering.
- 12:41Speaker 2: Sometimes Python has built in support for complex numbers. You
- 12:45Speaker 2: write them like three plus four J, where J is
- 12:47Speaker 2: the imaginary unit. All the standard complex arithmetic.
- 12:51Speaker 1: Works and true false values.
- 12:53Speaker 2: Those are boullions holy think they can only be true
- 12:56Speaker 2: or false. Note the capital letters.
- 12:58Speaker 1: How do we get bullion values?
- 12:59Speaker 2: Usually often from comparisons. You use operators like less than,
- 13:03Speaker 2: greater than, greater than, are equal to, less than are
- 13:05Speaker 2: equal to for equals right, double equals for testing equality
- 13:10Speaker 2: because single is for assignment and r is for not
- 13:13Speaker 2: equal to. These all return true or false.
- 13:15Speaker 1: Can you combine conditions like temperature is high, any pressure
- 13:19Speaker 1: is low.
- 13:19Speaker 2: Yep, using the logical operators and or and not.
- 13:22Speaker 1: Okay, that covers basic data types, but scientists often work
- 13:25Speaker 1: with collections of data points. How does Python handle that.
- 13:29Speaker 2: There are several ways, but the most fundamental container is
- 13:32Speaker 2: the list.
- 13:32Speaker 1: Lists.
- 13:33Speaker 2: Okay, A list is an ordered sequence of items. You
- 13:36Speaker 2: create it using square brackets with commas between items, like
- 13:40Speaker 2: my data is one point two one point five to
- 13:42Speaker 2: one point one.
- 13:43Speaker 1: Can lists hold different types of data like numbers and texts?
- 13:46Speaker 2: Absolutely mixed list equals one apple three point one four.
- 13:50Speaker 2: That's perfectly fine. You can even have lists inside other lists,
- 13:54Speaker 2: nested lists.
- 13:55Speaker 1: How do you find out how many items are in
- 13:56Speaker 1: a list?
- 13:57Speaker 2: Use the built in len function lend my data would
- 14:00Speaker 2: give you three in that example.
- 14:01Speaker 1: Are lists fixed once you create them? Or can you
- 14:03Speaker 1: change them? Add things?
- 14:05Speaker 2: They are mutable, meaning you can change them. You can
- 14:07Speaker 2: add an item to the end using the a pen
- 14:09Speaker 2: method like my data dot a pen one point four
- 14:12Speaker 2: That modifies the list directly.
- 14:13Speaker 1: If you join lists together.
- 14:15Speaker 2: YEP, using the plus operator list one plus list two
- 14:18Speaker 2: creates a new list with all the elements from both.
- 14:20Speaker 2: You can also repeat a list using one one two
- 14:22Speaker 2: three gives one two, one two one two.
- 14:24Speaker 1: How do you get a specific item out of a
- 14:25Speaker 1: list like the first one or the third one?
- 14:27Speaker 2: Using indexing, You put the index number in square brackets
- 14:30Speaker 2: after the list name. Crucially, Python uses zero based indexing,
- 14:34Speaker 2: so the.
- 14:34Speaker 1: First item is at index zero.
- 14:36Speaker 2: Correct might dad A zero would be one point two,
- 14:39Speaker 2: My data one would be one point five, and so on.
- 14:41Speaker 1: What about getting items from the end?
- 14:44Speaker 2: Python has negative indexing for that. My data one gives
- 14:47Speaker 2: you the last item, my data two gives a second
- 14:49Speaker 2: to last, et cetera. Very handy.
- 14:52Speaker 1: What if I want a range of items like the
- 14:54Speaker 1: second through fourth elements.
- 14:55Speaker 2: That's called slicing. The syntax is list namestart dot end.
- 14:59Speaker 2: It gives you an new list starting at the start
- 15:01Speaker 2: index up to but not including the end index.
- 15:03Speaker 1: So my data one point four would give items in
- 15:06Speaker 1: index one, two, and three exactly.
- 15:08Speaker 2: If you leave at start, it assumes zero. If you
- 15:10Speaker 2: leave out end, it goes to the end of the list.
- 15:12Speaker 1: Is there more to slicing?
- 15:13Speaker 2: You can add a step listenamestart dot end dot step,
- 15:17Speaker 2: So my list dot two would give you every second element,
- 15:19Speaker 2: and my listash one is a common trip to get
- 15:21Speaker 2: a reversed copy of the list.
- 15:22Speaker 1: Okay, lists seem very flexible. Are there built in functions
- 15:25Speaker 1: useful for lists in science?
- 15:27Speaker 2: Definitely? Some adds up all the numbers in a list,
- 15:30Speaker 2: but they all have to be numbers.
- 15:31Speaker 1: What about working with multiple lists together, Like if I
- 15:33Speaker 1: have time points in one list and measurements in another.
- 15:36Speaker 2: ZIP is perfect for that. Zip times measurements pairs up
- 15:41Speaker 2: the corresponding elements from each list into two poles. You
- 15:44Speaker 2: usually wrap it in list to see the result. Like
- 15:47Speaker 2: list zip times measurements.
- 15:49Speaker 1: That sounds really useful. What about filtering or transforming lists?
- 15:52Speaker 2: You have filter in map. Filter takes a function and
- 15:55Speaker 2: a list and keeps only the items where the function
- 15:58Speaker 2: returns true. Dot map take the function and applies it
- 16:01Speaker 2: to every item in the list, giving you a new
- 16:03Speaker 2: list of the results.
- 16:04Speaker 1: And their functions like all any all.
- 16:07Speaker 2: Checks if every item in a list is true or
- 16:09Speaker 2: evaluates to true, and any checks if at least one
- 16:12Speaker 2: item is true. Useful for checking conditions across data.
- 16:16Speaker 1: You mentioned Zip creates tuples. What are tuples? How are
- 16:19Speaker 1: they different from lists?
- 16:20Speaker 2: Tuples are very similar, but you create them with parentheses
- 16:23Speaker 2: instead of square brackets like point twenty to twenty. Indexing
- 16:27Speaker 2: and slicing work exactly the same way.
- 16:29Speaker 1: So what's the difference? Then?
- 16:30Speaker 2: The key difference? Tuples are immutable. Once you create a tuple,
- 16:35Speaker 2: you cannot change its contents. You can't append to it.
- 16:38Speaker 2: You can't change an element.
- 16:39Speaker 1: Why would you want something you can't change? Is it mutable? Better?
- 16:43Speaker 2: Immutability has its advantages. Sometimes you want to guarantee that
- 16:47Speaker 2: a collection of values won't be accidentally changed. Also, because
- 16:51Speaker 2: they're immutable, tuples can be used in places lists can't,
- 16:55Speaker 2: like as keys and dictionaries another data structure. Any performance
- 16:58Speaker 2: difference Sometimes ones maybe slightly less memory, but usually not
- 17:03Speaker 2: the main reason you choose one. It's more about signaling intent.
- 17:06Speaker 2: This group of values shouldn't change.
- 17:09Speaker 1: How do you make a tuple with just one item?
- 17:11Speaker 1: Item doesn't work? Does it right?
- 17:13Speaker 2: That just looks like parentheses. For grouping, you need a
- 17:15Speaker 2: trailing comma. Okay, looks a bit weird, but that's the syntax.
- 17:19Speaker 1: Okay. Lifts are mutable, Tuples are immutable. What about text
- 17:22Speaker 1: data reading, file labels notes?
- 17:25Speaker 2: That's where strings stent come in. There a's sequences of characters.
- 17:29Speaker 2: You cree them with either single quotes or double quotes.
- 17:32Speaker 1: Can you convert other things to strings like numbers?
- 17:35Speaker 2: Yep? The stem function does that? Startletar function? Does that
- 17:38Speaker 2: one two three? Gives you the string one two be three?
- 17:41Speaker 2: Very useful for printing output or saving data.
- 17:44Speaker 1: Is there a way to go the other way? String
- 17:46Speaker 1: to code?
- 17:47Speaker 2: There's Evil, which tries to run a string as Python code.
- 17:51Speaker 2: But you have to be really careful with Evil, especially
- 17:54Speaker 2: if the string comes from an external source, because it
- 17:56Speaker 2: can be a security risk. Generally best to avoid it
- 17:59Speaker 2: and you know exactly what you're doing.
- 18:01Speaker 1: Strings are mostly for impute output and format of printing.
- 18:04Speaker 2: Then, yeah, primarily, And the string object itself has tons
- 18:08Speaker 2: of useful built in methods for searching, replacing texts, splitting strings,
- 18:12Speaker 2: changing case, and so on.
- 18:14Speaker 1: All right, so we have data types containers. How do
- 18:17Speaker 1: we control what our code does? Like making decisions?
- 18:20Speaker 2: That's control flow. The most basic is the if statement
- 18:23Speaker 2: if condition followed by an indented block of code that
- 18:26Speaker 2: only runs if the condition is true.
- 18:28Speaker 1: What if the condition is false? Can you do something else?
- 18:30Speaker 2: Yes, you can add inn else dot block. And if
- 18:33Speaker 2: you have multiple conditions to check in sequence, use l
- 18:36Speaker 2: if short for else if if condition one, ale condition
- 18:39Speaker 2: two only one.
- 18:40Speaker 1: Block ever runs essential for handling different cases in experiments
- 18:44Speaker 1: or simulations.
- 18:45Speaker 2: Absolutely.
- 18:46Speaker 1: What about doing things repeatedly? Loops?
- 18:48Speaker 2: Two main loop types for and while before loop is
- 18:51Speaker 2: great for iterating over a sequence like stepping through each
- 18:54Speaker 2: item in a list or tuple.
- 18:56Speaker 1: For item in my list exactly.
- 18:58Speaker 2: The indented code block runs once for each item. You
- 19:01Speaker 2: can also loop over zipped sequences, like for tea in
- 19:04Speaker 2: zip time's measurements.
- 19:06Speaker 1: You mentioned range earlier. How does that fit with four loops?
- 19:09Speaker 2: Range is perfect when you just want to loop a
- 19:11Speaker 2: specific number of times. Range five effectively gives you the
- 19:15Speaker 2: sequence zero, one, two, three, four. So for I in
- 19:18Speaker 2: range five, will run the loop body five times with
- 19:22Speaker 2: I taking those values.
- 19:24Speaker 1: Very useful for fixed iterations. What about the wile loop.
- 19:27Speaker 2: A wile loop keeps running as long as its condition
- 19:29Speaker 2: stays true wile condition.
- 19:32Speaker 1: So you need to make sure the condition eventually becomes false.
- 19:34Speaker 2: Yes, otherwise you get an infinite loop and your program hangs.
- 19:38Speaker 2: You usually modify something inside the loop that affects the condition.
- 19:41Speaker 1: Do you ever break out of a loop early?
- 19:43Speaker 2: Yep, using the brake statement. If Python hits brake inside
- 19:46Speaker 2: a loop, it immediately jumps out and continues after the loop.
- 19:49Speaker 2: Useful if you've found what you needed or hit an error, and.
- 19:52Speaker 1: You mentioned list comprehensions again. A shortcut for creating lists.
- 19:55Speaker 2: Yeah, there are really neat concise way to build lists.
- 19:58Speaker 2: Instead of writing a full loop to create a list,
- 20:01Speaker 2: you can often do it in one line inside square.
- 20:04Speaker 1: Brackets, like by two for x in numbers to get
- 20:07Speaker 1: a list of squares exactly.
- 20:08Speaker 2: You can even add an if condition x for x
- 20:11Speaker 2: in numbers if backs zero to get only the positive numbers.
- 20:15Speaker 2: Very pythonic and often more readable once you get used
- 20:18Speaker 2: to them.
- 20:18Speaker 1: Okay, moving on the functions. They're key for reasonable code, right,
- 20:23Speaker 1: how do we make them in Python?
- 20:24Speaker 2: You use the deaf keyword deaf function name, parameters, then
- 20:28Speaker 2: an indented block for the function's code.
- 20:30Speaker 1: And the docks ring goes right after the deafline.
- 20:33Speaker 2: Good practice. Yes, explain what this function does, then the code.
- 20:37Speaker 2: If you want the function to give back a result,
- 20:39Speaker 2: you use the return statement return result.
- 20:41Speaker 1: What if there's no return statement, then the.
- 20:43Speaker 2: Function implicitly returns a special value called none.
- 20:47Speaker 1: And calling a function is just function name arguments.
- 20:50Speaker 2: YEP, providing the values arguments for the parameters defined in
- 20:54Speaker 2: the deafline.
- 20:55Speaker 1: Now a critical point for science. Can functions change the
- 20:59Speaker 1: data you pass into them?
- 21:01Speaker 2: It depends on the data type. If you pass immutable
- 21:04Speaker 2: things numbers, strings, tupples the function works on a copy.
- 21:07Speaker 2: It can't change the original outside the function, but.
- 21:10Speaker 1: If you pass mutable things like lists.
- 21:13Speaker 2: Then yes, the function can modify the original list directly.
- 21:17Speaker 2: This is called a side effect. Sometimes you want that,
- 21:19Speaker 2: sometimes it's a surprise.
- 21:21Speaker 1: So if you don't want the original list changed, you
- 21:23Speaker 1: should make a copy inside the function first.
- 21:25Speaker 2: That's the safe way to do it. Yes, use slicing
- 21:28Speaker 2: like localist input lists or the dot copy method.
- 21:31Speaker 1: Python seems flexible about function arguments. You mentioned positional and
- 21:35Speaker 1: keyword arguments. Right.
- 21:36Speaker 2: Positional arguments are matched by order. Keyword arguments you define
- 21:40Speaker 2: with a default value like my funk input threshold zero
- 21:44Speaker 2: point five, so.
- 21:44Speaker 1: You can call it like myfunk data and threshold to
- 21:46Speaker 1: b point five.
- 21:47Speaker 2: Yep, or you can override it myfunk's data threshold zero
- 21:51Speaker 2: point one. You can provide keyword arguments by name in
- 21:54Speaker 2: any order.
- 21:55Speaker 1: And those aregs and quorgs thing.
- 21:56Speaker 2: They let you create functions that accept any number of
- 21:59Speaker 2: extra positional are arguments, collects them into a tucle or
- 22:02Speaker 2: any number of extra keyword arguments quarks collects them into
- 22:06Speaker 2: a dictionary. Very flexible.
- 22:08Speaker 1: Okay, we have code functions, how do we get results out?
- 22:14Speaker 1: Displaying or saving.
- 22:16Speaker 2: Simplest display is the print function. We've used it implicitly
- 22:19Speaker 2: just print some value.
- 22:20Speaker 1: But often you want more control over the format right
- 22:22Speaker 1: like decimal places for numbers exactly.
- 22:24Speaker 2: That's string formatting. The modern recommended way is using the
- 22:28Speaker 2: dot format method on a string, or even better F strings.
- 22:31Speaker 1: How does dot format work.
- 22:33Speaker 2: You put curly braces as placeholders in your string the
- 22:36Speaker 2: value is and the limit is than you call format
- 22:38Speaker 2: on that string with the values dot format value.
- 22:42Speaker 1: Limit, Can you control the formatting inside the braces?
- 22:45Speaker 2: Yes, you can specify alignment with number of decimal places
- 22:49Speaker 2: for floats or two decimal places, scientific notation and more.
- 22:53Speaker 2: It's quite powerful. F strings f the value is valued,
- 22:56Speaker 2: DOT two s are often even more concise.
- 22:59Speaker 1: What about saving results to a file or reading data
- 23:02Speaker 1: from a file crucial for science.
- 23:05Speaker 2: Python has built in file handling. You start by using
- 23:08Speaker 2: the open function. You give the file name and.
- 23:10Speaker 1: A mode modes like R for read, W for wright.
- 23:14Speaker 2: Exactly, R for reading file must exist, W for writing
- 23:18Speaker 2: creates new file or overwrites existing A for appending to
- 23:22Speaker 2: the end of a file, add B for binary files
- 23:25Speaker 2: like RB or WEB.
- 23:27Speaker 1: So open gives you a file object yes.
- 23:30Speaker 2: And that object has methods like dot read to read
- 23:34Speaker 2: the whole file, dot redline to read one line, dot
- 23:37Speaker 2: red lines to read all lines into a list, and
- 23:39Speaker 2: dot write to write a string to the file.
- 23:41Speaker 1: And you absolutely must close the file afterwards.
- 23:44Speaker 2: Critically important use the dot close method if you forget
- 23:47Speaker 2: data might not get fully written, or you might run
- 23:49Speaker 2: out of resources.
- 23:50Speaker 1: Is there a safer way that handles closing automatically?
- 23:52Speaker 2: Yes, and it's strongly recommended use the with statement dot
- 23:55Speaker 2: with open data, dot txdrs F you work with the
- 24:00Speaker 2: file object f inside the indented block. When the block finishes,
- 24:04Speaker 2: Python automatically closes the file even if errors happened.
- 24:07Speaker 1: Uh. The context manager much safer.
- 24:09Speaker 2: Definitely the preferred way.
- 24:11Speaker 1: Okay. Inevitably things go wrong. Code has errors. How does
- 24:15Speaker 1: Python react.
- 24:16Speaker 2: When an error occurs? Python raises an exception if you
- 24:19Speaker 2: don't do anything about it. The default behavior is to
- 24:22Speaker 2: print an error message the trace back and stop the program.
- 24:25Speaker 1: What kind of errors might a scientist run into?
- 24:27Speaker 2: Lots? Indentation error is common for beginners. Python cares about
- 24:32Speaker 2: spaces type error if you try to say, add a
- 24:35Speaker 2: number in a string. Name error. If you use a
- 24:38Speaker 2: variable you haven't defined value error if a function gets
- 24:42Speaker 2: the right type but a bad value, like trying to
- 24:44Speaker 2: convert zxyz to an integer. Zero division error is obvious.
- 24:49Speaker 2: Index error if you try to access a list element
- 24:51Speaker 2: that doesn't exist. File not founder if open can't find
- 24:55Speaker 2: the file. Import error if it can't find a module
- 24:57Speaker 2: you try to import.
- 24:58Speaker 1: Can you stop the program from just crashing when these
- 25:00Speaker 1: maybe try something else?
- 25:01Speaker 2: Yes, that's what tri dot except blocks are for. You
- 25:04Speaker 2: put the code that might cause an error inside the
- 25:06Speaker 2: try dot block, and then you have one or more
- 25:08Speaker 2: accept blocks. If an exception occurs in the triblock, Python
- 25:13Speaker 2: looks for an accept block that matches the type of
- 25:15Speaker 2: exception raised. If it finds one, it runs the code
- 25:18Speaker 2: in that accept block instead of crashing.
- 25:20Speaker 1: So you can handle the error gracefully, log it, try again,
- 25:24Speaker 1: provide a default value exactly.
- 25:26Speaker 2: It lets you build more robust programs that don't fall
- 25:29Speaker 2: over at the first hurdle.
- 25:30Speaker 1: Now, the book also introduces classes. This sounds a bit
- 25:34Speaker 1: more advanced. What's the idea.
- 25:36Speaker 2: Classes are a way to create your own custom object types.
- 25:40Speaker 2: Think of them as blueprints for creating objects that bundle
- 25:43Speaker 2: together data called attributes and functions that operate on that data,
- 25:48Speaker 2: called methods.
- 25:49Speaker 1: Why would a scientist use classes?
- 25:51Speaker 2: You could model things from your research. Maybe create a
- 25:53Speaker 2: molecule class with attributes like atoms and bonds and methods
- 25:57Speaker 2: to calculate its mass or energy. It helps organized complex
- 26:00Speaker 2: data and related operations.
- 26:02Speaker 1: How do you define one?
- 26:03Speaker 2: Using the class keyword class my object? Inside you often
- 26:07Speaker 2: define a special method called innit that's the constructor. It
- 26:10Speaker 2: runs when you create a new instance of the class
- 26:12Speaker 2: and usually sets up the initial attributes.
- 26:14Speaker 1: The book had that frack example for fractions right with
- 26:17Speaker 1: AD and mole.
- 26:18Speaker 2: Methods exactly, and it used other special methods like repro
- 26:21Speaker 2: to control how the fraction object gets printed.
- 26:23Speaker 1: And things like AD and moll right.
- 26:25Speaker 2: Those allow operator overloading. By defining AD, you tell Python
- 26:30Speaker 2: what the plus operator should do when used with your
- 26:32Speaker 2: Frack objects. It makes your custom objects behave more intuitively
- 26:36Speaker 2: like built in types.
- 26:37Speaker 1: Very cool. Let's look back to modules. You said they're
- 26:40Speaker 1: key for scientific work.
- 26:41Speaker 2: Absolutely central. Most of the powerful scientific tools NUMBPI for arrays,
- 26:45Speaker 2: SIPI for algorithms, matt plotlib for plotting, PANDAS for data
- 26:50Speaker 2: analysis are external libraries you access as modules.
- 26:53Speaker 1: So a module is just a dot pi file with.
- 26:55Speaker 2: Code essentially Yes, it contains functions, classes, variables. You use
- 26:59Speaker 2: the import statement to bring that code into your program's namespace,
- 27:03Speaker 2: like import math YEP. Then you access things inside it
- 27:06Speaker 2: using the dot dot math dot pi math dot sinx.
- 27:10Speaker 2: This dot mechanism is fundamental in Python for accessing attributes
- 27:13Speaker 2: and methods of objects and modules.
- 27:15Speaker 1: Can you import just one thing or give the module
- 27:18Speaker 1: a shorter name.
- 27:19Speaker 2: Yes. From math import score, lets you use squort directly
- 27:23Speaker 2: and import NumPy as NP is super common. It imports numpi,
- 27:27Speaker 2: but lets you refer to it as MP, saving typing
- 27:30Speaker 2: NP dot ray, NP, dot mean, et cetera.
- 27:33Speaker 1: Mastering that import in dot mechanism seems crucial, then.
- 27:37Speaker 2: It really is key to using Python effectively for science.
- 27:40Speaker 1: Finally, the book mentions PEP eight the style Guide. Why
- 27:43Speaker 1: worry about style isn't getting the right answer the main thing.
- 27:46Speaker 2: Getting the right answer is critical, of course, but how
- 27:48Speaker 2: you write the code matters hugely, especially in science. PP
- 27:52Speaker 2: eight is about making code readable, consistent, and understandable.
- 27:55Speaker 1: Why is readability so important?
- 27:57Speaker 2: Remember the zen of Python readability count. Code is read
- 28:01Speaker 2: far more often than it's written. If you're collaborating, others
- 28:04Speaker 2: need to understand your code. If you revisit your own
- 28:06Speaker 2: code months later, you need to understand it. Clear. Consistent
- 28:09Speaker 2: code aids transparency and reproducibility.
- 28:12Speaker 1: So what are some key PP eight guidelines?
- 28:14Speaker 2: Things like using four spaces for indentation, never tabs, limiting
- 28:19Speaker 2: line length to make it easy to read around seventy
- 28:21Speaker 2: nine or maybe a bit more characters, using meaningful variable names,
- 28:26Speaker 2: using blank lines to group logical sections, consistent spacing around operators,
- 28:31Speaker 2: adding comments where needed, but not over commenting obvious things.
- 28:35Speaker 1: So it's worth taking the time to learn and follow.
- 28:37Speaker 2: These Definitely it makes you a better programmer and makes
- 28:40Speaker 2: your scientific code more valuable in the long run. The
- 28:42Speaker 2: book links to the full guide.
- 28:44Speaker 1: Okay, wow, we've covered a lot.
- 28:45Speaker 2: Yeah, quite a tour. So just to recap, we've seen
- 28:48Speaker 2: why Python is so big in science. It's accessible, flexible, explicit.
- 28:53Speaker 2: We hit the basics data types like numbers, strings, lists, tuples,
- 28:57Speaker 2: control flow with if and loops, building or usable code
- 29:01Speaker 2: with functions, handling files and errors, even dipped into classes,
- 29:04Speaker 2: and the vital role of modules like NUMPI.
- 29:07Speaker 1: And the importance of clean, readable code. With PP eight,
- 29:10Speaker 1: it really feels like a solid foundation for tackling scientific programming.
- 29:13Speaker 2: Absolutely. Python gives you the tools to handle complex data,
- 29:17Speaker 2: build models, test hypotheses, all that good scientific stuff.
- 29:21Speaker 1: And the book and the official Python docs online are
- 29:24Speaker 1: great places to go next for more details, for.
- 29:26Speaker 2: Sure, the documentation for lists, control flow functions, file io errors,
- 29:32Speaker 2: classes PEP eight. It's all there and very comprehensive, definitely
- 29:37Speaker 2: worth exploring.
- 29:38Speaker 1: All right, So here's something to chew on as we
- 29:39Speaker 1: wrap up. Take a look at how you currently work
- 29:42Speaker 1: your analysis you're modeling. Where could Python fit in? Could
- 29:46Speaker 1: it automate some tedious stuff? Could it let you analyze
- 29:49Speaker 1: data in new ways or build more complex models than
- 29:52Speaker 1: you could before?
- 29:52Speaker 2: Yeah, think about how it might streamline things or even
- 29:55Speaker 2: open up entirely new scientific questions you could tackle.
- 29:58Speaker 1: Food for thought. Thanks for joining us for this deep dive.