From the course: Programming Foundations: Fundamentals

Writing source code - Python Tutorial

From the course: Programming Foundations: Fundamentals

Writing source code

- Writing source code is what programmers do all day. It's the instructions we have for the computer, and it's written in plain text. In other words, we write it without special formatting, like bold, italic, or different font types. It's mostly just the actual characters. This means that word processing applications aren't suitable for writing code, because by default, they insert bits of information in files that prevent them from being plain text. Instead, you can use a text editor. If you're on a PC, you already have a text editor available to you. Notepad. And for Mac users, there's TextEdit. Both applications allow you to write source code. Let's look at an example. I'll be using TextEdit since I'm on a Mac. We're going to go ahead and open up TextEdit, and the first thing that you'll notice is this formatting bar. By default, TextEdit stores files in Rich Text Format. That means you can do things like bold or italics, or even underline. But this is not suitable for writing source code. To change it, we're going to go over to TextEdit, choose Preferences, and then we're going to choose plain text under the Format section. Also, make sure you uncheck all of the options down below. Great. We can close our Preferences pane, and let's restart TextEdit to make sure that our changes are still present. Perfect. Now that formatting bar is no longer here. If you're on a PC and you're using Notepad, you don't have to worry about this. It uses plain text by default. So let's start off by doing our favorite program, Hello World. We're going to do the Python version. Let me zoom in a bit so you can see what I'm doing. I'm going to type print, open parentheses, double quotes, hello world, exclamation mark, double quotes, and a closing parentheses. We did it. We've written some source code. Source code can either be one line, like you see here, or thousands. It just depends on the needs of your program. Let's go ahead and save this file. By default, TextEdit saves files with a .txt extension. This is because it's plain text. But it's a best practice to use an extension that corresponds to the programming language that's inside of your file. Since this code was written in Python, we're going to use the Python extension, which is .py. So let's save this as 01_03.py. Perfect. Each programming language has it's own file extensions. For JavaScript, it's .js, Perl, .pl, whereas a language like Kotlin uses .kt. Now that we know how to write our code, we need to learn how to translate it into the language that the computer speaks. Machine language. This is a vital step to get it running on any device.

Contents