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