Innovative Technologist — Python Basics— Part 1/3

Understanding the Basics Will 10x Your Progress

Chen Margalit
10 min readMar 19, 2024

Intro

IIMachine learning is a scientific, theoretical and excellent idea about modeling real-world data. It has great capabilities and has demonstrated great success in solving complex problems like recognizing faces, creating conversations, understanding voice, etc. But, in order to make that mathematical theory come to life and actually great products, we need to write it in a language the computer understands, a programming language. Today we will be speaking about one of the world’s favorite programming languages and the most dominant and important languages in machine learning — Python.

The Very Basics of Python

Programming languages — video by author

Salvador: Python is a programming language. It's a language the computer understands. In the same way you understand English, the computer understands programming languages. To be more precise, the computer understands binary code. Tools called compilers or interpreters, translate (compile/interpret) what you write in Python, to binary code.

Compiling Python Code — video by author

Dali: Binary code?
Salvador: Binary code is a language that only has 0 and 1.
Dali: That doesn’t sound very efficient. How will I say: “I love Data Science” with 0 and 1?
Salvador: You’d be surprised.
Dali: Please, surprise me.
Salvador: Any number can be represented with a bunch of 0s and 1s. The number 0 in binary, means 0 in the real world. The same goes for 1. When you want to say 2, you say 10. Don’t think of it as the number 10. Think of it as 0 digits combined, zero and one. Three would be 11 (one, one), 4 is 100, 5 is 101.
Dali: I don't get how you increase the numbers.
Salvador: It’s not important. Look up binary code if you’re interested, it's really not important. What is important, is knowing that you can write any number using some combination of 0s and 1s and represent any number.
Dali: Maybe I wasn’t clear. I LOVE DATA SCIENCE. Do you see numbers here?
Salvador: 😠.
Salvador: These words, these letters, are behind the scenes represented as numbers. As I said, the computer only understands numbers.
Dali: You’re saying the computer doesn’t get the word I, it gets a number?
Salvador: Exactly. Lowercase i is 105, and uppercase is 73. But it doesn’t get 73, it gets the binary representation of 73, which is 1001001.
Dali: So I write some reasonable-looking Python code, and some software turns it into binary code?
Salvador: Pretty much, yes. It’s a two-step process, but yes.
Dali: Two steps? it doesn’t get translated from my Python code straight to binary?
Salvador: No. Two-step process. First a software known as a compiler takes your Python code and turns it into machine code, which is some gibrish-looking language. Then another software called interpreter, part of the Python Virtual Machine (PVM) takes that machine code and translates it to binary code. This is the binary code that gets run in the CPU.
Dali: CP who?

Image from bndsibbarry.

Salvador: CPU. The Central Processing Unit, is the computer’s brain. It’s a chip, that does the real work in the computer, calculating everything needed to do pretty much everything the computer does. It's a rather small fellow, depending on the chip, but think of something like 35mm by 35mm. Very small, very important.
Salvador: Back to the compiling/interpreting journey. This two-step process is what happens in Cpython, the official (and most common) implementation of Python. Other implementations do it differently.
Dali: What do you mean other implementations do it differently?
Salvador: What is Python? it is a software. It’s some code, that does something. You can do that something, differently. You don’t have to do it in a 2 steps process, other languages do it differently.
Dali: Aha. Suspicious, but I’ll take your word for it, for now.
Salvador: I’m a lucky man, moving on!
Salvador: Now that we’ve got this part, let’s step out a bit and look at a wider (yet more confusing) definition. When introducing Python, I sometimes like to start with Wikipedia’s horrible definitions.
Dali: That's pretty harsh for one of the most successful websites in the world.
Salvador: I love Wikipedia, I just hate their definitions of things. I very rarely understand it.
Dali: I understand every word written there. every single word.
Salvador: Excellent. Go ahead, explain their definition of Python to me:

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.

Salvador: And the fun continues:

Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming. It is often described as a “batteries included” language due to its comprehensive standard library.

Dali: It's high, you can do anything with it, it's involved in philosophy and design, and it's indented. Always indented. What could possibly be clearer than that? 🤨
Salvador: Got my point?
Dali: Okay, so why did you bring it up? If we’re into doing things that don’t progress me in my ML journey, I can think of more fun stuff.
Salvador: No no, it's a rather excellent definition.
Dali: Didn’t you just say …?
Salvador: It’s excellent because it holds much of the important stuff about Python. It's horrible because if you don’t already know quite a lot about programming, you’ll keep on not knowing after this.
Dali: If only there was someone here who knows much about programming 🙄
Salvador: Yes yes, as I was saying. This definition holds many of the most important details. Let’s break it down piece by piece.
Salvador: “Python is a high-level general-purpose programming language”.
Salvador: With high, they don’t mean it likes to party, they mean it's further away from the hardware. There are languages (like C) where handling the hardware is much less abstract.
Dali: What do you mean less abstract? What is abstracted?
Salvador: Abstracted is when someone hides details from you. When you go to a restaurant and order something, you don’t tell the waiter, go to the kitchen, tell this person that, this person that. You’re counting on that the waiter does what he does and you get what you requested. The details of how they made the food, are abstracted from you.
Salvador: As I said, there are languages (like C), where you have to manage things like memory consumption yourself.
Dali: Why would I possibly want to manage the hardware myself?
Salvador: Usually (and in this case as well), because it can provide you with better performance.
Dali: Bullshit. It’s just newer, so it's better. In the same way I’m newer than you and I’m better.
Salvador: 😮‍💨.

Salvador: With general purpose, the definition means it was designed to do many different things like web, scientific computing, network programming, machine learning, etc.
Dali: Contrary to what? you can theoretically do anything with any language, it’s just a way to speak to the computer. Can’t you?
Salvador: Theoretically you can, but Python was built with the intent that it would be like that and it affected how it was built. We don't know much about Python yet so it's too early to give examples, but you could build Python otherwise if you wanted it to be more proficient for example.
Salvador: The definition continues talking about how it's designed with readability in mind, which is definitely true as we’ll see.
Salvador: It also uses indentation, well, not sure that's so critical to write in a definition, but it is true. We’ll be seeing that as well.

Salvador: Python is dynamically typed. Programming languages range on a scale from being very strongly typed to very dynamically typed. Languages like Java are considered strongly typed which means you need to declare what is the data type you are using. In Python you don’t have to do that, the interpreter will understand it by itself.
Dali: Let me guess! Java is older.
Salvador: Yes … but that doesn’t mean it's less good!. It’s not that Java doesn’t “understand” the data type alone because it's not smart enough. Some of the best (modern!) tools we use today are built in Java. The reason strongly typed languages are .. strongly typed .. is that it has a few advantages. If you write strongly typed code, it’s clearer to another developer (or future you) reading the code what is the expected data type. It enables the compiler (part of the process running the code, we’ll go through this later) to optimize the code better because it knows what data type to expect and so on.
Dali: Okay, I’m convinced. So why is Python not doing this? These young people (languages) have no respect for tradition, always do a bad job.
Salvador: 🤨
Salvador: Because like anything in software, it has advantages. If you don’t have to write the data type, it makes code writing faster. It’s easier, it’s more fun. It's one of the reasons Python is much higher in the charts in regards to how much people love Python compared to other languages.

Stackoverflow Survey

Salvador: Python is garbage collected. This means you don’t have to handle the grasp and release of memory.
Dali: What does that mean?
Salvador: It’s like I said before. In Python you don't have to manage memory yourself, Python does it for you.

Salvador: All in all, you can think of Python as a flexible, easy-to-learn, very comfortable language. On the other hand, the same features that make people love it so much, make others hate it. In programming, you read code much more than you write code. If the language doesn’t make you do things, perhaps you won’t do it, although it could’ve improved something, like readability.

Salvador: Python is by far the most dominant language in data science. It’s difficult to say why, it just kind of caught on. You could say it's because of how easy it is to learn, how versatile it is, the amount of community, etc. In the end, most people use what very few people wrote. Those people, in deep learning’s case, mostly those companies, wrote their packages in Python. Hence, for machine learning you usually use Python. You can do some machine (and deep) learning in other languages, but it won't come near the amount of tools and community you have in Python. It’s difficult as it is, so if you’re a beginner do yourself a favor, and start with Python.

Salvador: This concludes our first part of talking about Python. As stated Python is a very important language for machine learning and a very easy language to begin with overall.
Salvador: Would you like to summarize what we’ve been through?

Dali: I’ll try. We’ve introduced Python, a programming language dominant in the ML field. We talked about whats a programming language as a whole, which is basically a language the computer understands, not much different than English, just for computers. You explained some of its characteristics such as being dynamically typed.
Dali: We also talked about Python being flexible, having an easy-to-understand syntax, A claim you had not shown any evidence for. You talked some about binary language and trashed talked Wikipedia (a website a gazillion people visit a day).
Salvador: 🤥. Blunt lie, I did no such thing.
Dali: It’s right here, above us, like a god, watching us. Have a look. 🧐.
Salvador: Aaaanything more we talked about today?

Dali: Yes. We talked about Python being garbage collected, which makes things easier regarding handling memory consumption but it’s an example of why Python isn’t very fast. Finally, we spoke about Python being very important for machine learning we finished with me allegedly believing everything you say, seeing no real proof for anything.
Salvador: Yes as I said, it's rather complicated to show proofs for that now, but you will see it for yourself, as we move along.

We’re done for now! In our next lesson, we’ll take what we’ve learned here and move up a nudge, get a real feeling of Python using our fingers.

Did you know Medium lets you clap up to 50 times? interesting feature and it helps other people see it, please feel very free to try it!

Also, if you think other people should be reading this, please share!

Finally, I’d love to hear your opinions in the comments:

  1. How do you feel about Python’s easygoingness? do you agree with the survey that placed Python so high?
  2. Why do you think Python has become the standard in Data Science?

--

--