Getting Started with Python

If you’re just getting started with programming, Python is one of the best languages to learn. Because the language is designed to be simple and readable, you can focus more on learning and how to think like a programmer.

While Python is a great language for beginners to learn, it’s also a language that’s used by professionals every day. Python touches on many different domains, including web development, automation, and data science.

Here are a few reasons why Python is so popular:

  • Easy to read and write – Python code often looks like plain English.
  • Huge community – Which means it’s easy to find support and to use open-source tools.
  • Versatile – You can build websites, automate tasks, analyze data, and lots more.

What is Python?

Python is an interpreted, high-level, general-purpose programming language. It is dynamically typed and garbage collected.

But what does that mean?

Interpreted: meaning that it is directly executed, without needing to be compiled first.
High-level: meaning that it uses many natural language elements to make code easier to read.
General-purpose: meaning it can be used in many different domains.
Dynamically typed: meaning that the data types are checked at runtime but are not explicitly enforced.
Garbage collected: meaning that the program tries to clear up memory as items are no longer used.

Run Python Online

To make it easier for you to get up and running, I have built a compiler that allows you to run your Python code online. Being able to practice writing your own code is the best way to learn.

When you see a window like the one shown below, you can enter your own code on the left side. When you hit the Run button, the output of that code will be returned on the right side. Try clicking the run button below:

You’ve just run your first Python script!

Printing in Python

In the code block above you used the print() function. The function allows you to display things in your terminal’s console, giving you an easy way to understand your code or help debug it.

Try changing the values that you’re printing in the code editor above:

  • Try adding some more text, but be sure to wrap it in single quotes or double quotes,
  • Try adding a number, either with or without a decimal place
  • Try printing a True or False.

Printing allows you to display an output, which can be helpful for many different reasons. One of the main ones you’ll encounter is being able to display helpful information either for yourself or for your end user.

Variables in Python

Variables are like containers that can hold information. They allow you to assign a name to a value, allowing you to reuse and modify a value throughout your code.

In order to create a variable, you can use the = operator. Let’s create our first variable:

message = "Let's learn some Python!"

We assigned a piece of text information (or a string, as it’s called in Python) to a variable named message. You can use this variable throughout your program. For now, let’s try printing this message using the print() function. You can print a variable by passing that variable into the print() function. Give it a try below:

Variables themselves are quite flexible, but they do have two main rules:

  1. They must start with a letter, and
  2. They can only contain letters, numbers, or underscores

While not required, Python variables should also be:

  1. Descriptive (use something like message instead of a,
  2. Styled consistently (either in snake case (my_message) or camel case (MyMessage))

You might remember from the earlier definition of Python that it’s dynamically typed. This means that we can update the type of a variable without needing to redefine it. In other languages, you’d need to declare the variable type and retain it.

But what are data types? Let’s learn about those now.

Data Types in Python

Data types define the types of data that a variable can hold.

You’ve already used three different types of variables:

  • When you defined a variable holding text, you defined a string variable,
  • When you defined a variable holding a number, you defined either an integer or a floating-point value variable, and
  • When you defined True or False variable, you defined a boolean variable.

Variables determine what you can do with your data, how Python chooses to store it, and even how much memory it takes up.

Some of the most important data types in Python are shown in the table below:

TypeTypeDescriptionExample
intNumericInteger (whole) numbers5, -42
floatNumericFloating-point numbers (decimals)3.14, -0.001
booleanBooleanBoolean valuesTrue, False
strStringStrings / text values"Welcome"
listCollectionOrdered and mutable[1, 2, 3, 3]
tupleCollectionOrdered and immutable(1, 2, 3, 4)
setCollectionUnordered and unique{1, 2, 3}
dictMappingKey-value pairs{'age': 35'}

There are more data types, but these are the essential ones you’ll encounter in your day-to-day programming journey.

Let’s now learn a bit about how to use these different data types by doing some math.

Doing Math in Python

We can do math in Python using numeric operators such as integers and floating point values.

For example, we can add two numbers using the + operator.

print(1+2)

# Returns: 3

The table below highlights the different operators you can use on numeric data types.

OperatorNameDescription
x + yAdditionThe sum of x and y
x – ySubtractionThe difference between x and y
x * yMultiplicationThe product of x and y
x ** yExponentiationThe value of x raised to the power y
x / yDivisionThe quotient of x and y
x // yFloored DivisionThe quotient of x and y, rounded down to the nearest integer
x % yModulusThe remainder of the quotient between x and y
-xNegationThe negative value of x

Try using some of these operators in the code block below. Give the following challenges a try:

  1. Multiply 2 by 3
  2. Subtract 7 from 23
  3. Raise 2 to the power of 4
  4. Divide 7 by 3 in such a way that it returns the floored quotient

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top