Print function, strings, variables and development environment
A development environment is absolutely necessary for Python.
Welcome back to the Python basics tutorial! Today I'm gonna teach how to set up a development environment for Python and more on the mysterious print function. Till now, you've been entering a line at a time in the python prompt. But what if you make a mistake somewhere in the program? Then you'll have to start over again. But if you save it to a file, you can just edit the program if you have any mistakes. Now, you can not use any text editor for editing Python code. Sorry buddy, no MS Word this time. So you need an IDE (Integrated Development Environment) or a Text Editor. They have some useful features, such as identifying errors and bad practices when coding and much more. As you're still a beginner, you'll not need them. They might just end up confusing you with all the setup. Your main focus will be writing code.
Some good IDE's are PyCharm, Visual Studio Code, and Eclipse (with PyDev plugin). Some good text editors are Atom, Sublime Text, PyPad and Vim.
On Windows, you should have a program called IDLE (Integrated DeveLopment Environment) installed by default with Python. In Mac, you can fire up TextEdit and in Linux you should have a program called Text Editor installed as well.
Or if you want to install IDLE, you can use brew
in Mac and your Linux distro's package manager to install the package idle
. IDLE is almost perfect for a beginner who doesn't need advanced features right away.
You should type the code in the tutorials instead of copy-pasting since it helps with learning.
Now, open IDLE and type the same line of code:
in the blank field.
The colored text is called "Syntax Highlighting".
Now use CTRL + S for PC and CMD + S for Mac and save the file as hello.py
We use the .py
extension to indicate that this is a Python program.
Now that you've saved the file, press F5 to run the program. You'll see a new window open up with the Python prompt and you'll see "Hello World" displayed again. Now go back to the editor and type in:
and run. Now you should see the text being printed to the screen.
Great! Now you have a development environment where you can save your programs and more!
There's a free web service called https://repl.it where you can run Python code in your browser directly without having to install Python. However, it is not recommended for actual projects.
Alright, I've learned all about development environments.
Good. Now we can get to learning about the print
function. First of all, what are functions? Functions are "blocks" or pieces of code that are run when "called". You can pass data to them too. A function is usually called like this:
You'll learn about functions in another chapter.
print
is also a function. When you call print
like print('Hello World')
it prints the text given inside the quotes. You can add more text like this:
Strings
It's an odd name, right? What do strings have to do with programming? Well, here a "string" is a string of characters. So, strings in programming are actually just text data. To make a string in Python, you need to enclose it in quotes, like:
or double quotes:
But you cannot start with a single quote and end with a double or vice versa:
This will result in a SyntaxError
which means your code has mistakes such as a missing quote or bracket.
The text you pass to print
is also a string.
Adding two strings together is called concatenation. You do it like this:
To print it out:
which results in "String 1String 2"
What if you had to put a quote in a string like 'I don't think I can'? It would make Python think that you're ending the string at don
. To prevent that, you have to "escape" the quote with a backlash like 'I don\'t think I can'.
Now, if you had two print "Hello World" three times:
No big deal, right? But what if you had a larger string? That would be a pain to type out. So, what do you do? Here's where variables come in.
Variables
So, you have a really long string you want to print three times. To avoid typing it out every time, you can assign it to a variable. Let's say we want to assign "This is a really really long string to type out" to x
just like we did in algebra. But this time, with strings. So, we assign it like this:
Now, instead of the string, you can just use x
to refer to it. So, this should work:
Run the code. Does it work? Nope. It just prints 'x'. This is because you are creating a whole new string. Variables are used just like how they were assigned, without quotes.
Does it work now? Yes it does!
Remember, a variable can only start with a letter. The variable name can not start with a number or a symbol. It can not have a symbol within either. It cannot have spaces.
Correct variable names: memory_error123
, python_is_cool
Incorrect variable names: 123harry_potter
, this$is$really$wrong
Variable names should always be all-lowercase and words should be separated with underscores.
Good - days_of_week
Bad - dAYsOFtHEWeEk
You can add more variables and print them too!
That's a lot you've learned today. Some important points to remember:
Strings are created with quotes, single or double.
Variables are refered to normally without quotes.
SyntaxError
is shown when you have a mistake in your code
You're good to go!
Homework
Your homework will be trying to add words as strings together and make a sentence.
Next chapter we're going to learn more about other data types.
Last updated
Was this helpful?