Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started
← Back to Lessons
Edit on Github

How to Code in Python?

Programming in Python
What can you do with Python?
1 2# Python as a calculator 3print(2 + 2) 4 5# Output 4

Programming in Python

Python is an extremely powerful programming language, not for nothing is it the most widely used programming language today, and at the same time, the most recommended language as a gateway to the world of application development.

For some time now, Python has taken over many specializations due to its simplicity and power, being able to be used from the creation of web applications, to Machine Learning, AI and as a tool for research and development of technologies and techniques in the medical field.

But of course, if we are going to talk about how to program with Python, we will not look for specific or advanced examples, but rather, we will look at examples that are simpler but understanding the basis of them, we would already have to be able to sit down and write our first scripts.

What can you do with Python?

Calculator

A very simple example would be to use Python as our personal calculator, so that with the following code: print(2+2), Python would return 4, but could we do more complex operations?

1# More complex mathematical operations 2 3print(((10 + 25) * 12 - 5) /6 ) 4 5# Output: 69.16666666666667

We will always use the print() method to display in the terminal what we pass between the brackets.

As you can see, Python will be able to perform complex calculations as long as they are written as we would do in mathematics. The () and order of execution of the operations will be respected.

Creating lists

Normally, having a list of tasks that we have to perform, is a method widely used by everyone when we want to prioritize the tasks we have to perform.

In Python we can create lists in the following way:

1todo_list = ["make the bed", "take the dog for a walk", "learn python"] 2 3print(todo_list) 4# Output: ['make the bed', 'take the dog for a walk', 'learn python'] 5 6# If we want to retrieve a single element from a list, we'll use square brackets [] and pass the position of the element on the list we want to retrieve, just remember that the first position is 0 and not 1. 7 8print(todo_list[0]) 9# Output: make the bed

Whenever we work with a group of elements, like the tasks in our task list in this case, we group them in a "list" or "array" by using [].

All programming languages make use of variables, which are like little boxes where we are going to store the information we need. In our example, todoList is a variable that stores a list of tasks.

Working with loops

It is very common to come across a task that requires us to do the same thing a certain amount of times, which can be (and usually is) tedious.

Python, like all other programming languages, allows us to create cycles to perform the same task as many times as we need. An example, would be to get out of the punishment of the teachers who put us to write the same sentence 100 times... With Python, this is solved in a very simple way

1for x in range(10): 2 print("I will study python") 3 4# Output: 5# I will study python 6# I will study python 7# I will study python 8# I will study python 9# I will study python 10# I will study python 11# I will study python 12# I will study python 13# I will study python 14# I will study python

For x in range(10) is a loop that will repeat the code block we have set as long as x, which will start with a value of 0, is less than 10.

Making use of random numbers

Before moving on to the next example, I think it is necessary to explain a little bit about functions in Python:

1# Example of function in Python 2 3def myFunc(params): 4 # Code block (code to be executed). 5 return # Code to be returned

def (definition) is a reserved word used to define a function.

myFunc is the name of the function, which will be used to execute it,

(params) parameters that the function will receive, they are as if they were variables that will only exist inside the function.

: They indicate where the code block begins.

code_block here will go all the logical operations that our function will need to perform to be able to return the result that we want.

return what the function will return when executed.

Note that some programming languages, like JavaScript, make use of {} to define the code block that will be executed, Python on the other hand, makes use of the correct indexing of the elements, that is why the code block and the return will never be at the same height as the declaration of the function.

Now that it is clear how a Python function is composed, we can use what we learned to create a program that returns random colors from a list of colors that we have defined.

1import random 2 3def randomColor(): 4 colors = ["blue", "red", "black", "yellow", "pink"] 5 random_selected_color = colors[random.randrange(4)] 6 return random_selected_color 7 8print(randomColor()) 9 10# Output: yellow

random is a Python module that allows us to generate random numbers, so we have to import (bring) to our program the module in order to use it.

In the previous example, we have a function called randomColor that in the code block what it has is a list of colors and a variable, randomSelectedColor.

To select a random color, from our list of colors, we make use of the random module that we imported at the beginning, using its randrange() method to define up to what number we want to generate random numbers (in this case 4, which is the length of our list of colors).

After we have extracted from our list of colors one randomly and stored it in our randomSelectedColor variable, what would be left would be to return it in the return of our function.

Working with dates:

Every now and then, we wonder how many days until my birthday? Well, let's create a small program to know it

1import datetime 2now = datetime.date.today() 3date = str(input("Enter your birthdate month and day with this format MM/DD: ")).split("/") 4birthday = datetime.date(now.year, int(date[0]), int(date[1])) 5days = (birthday - now).days 6print(fdays + " left until you level up!")
  • First we will need to import the date module with import datetime.
  • The now variable stores the current date making use of datetime.date.today() which is a method that specifically returns that information.
  • We make use of input to ask the user to enter the date of birth and use str() so that Python sees that information as text and not numbers. Similarly, we then separate the day of the month from the user's birthday using .split("/") and this information is what we will store in our date variable.
  • The birthday variable will store the date given by the user, together with the current year in the format needed by the datetime module to be able to perform operations.
  • days will be the subtraction between the date of birth that we have formatted and the current date, that returned in days how many days are missing so that the subtraction of 0 (if it gave 0, it would be the day of the birthday).
  • We print making use of concatenation to return our days variable along with a text how many days are left to level up!

Mixing it up a bit

As you can imagine, when we make a program, we mix different types of variables, modules and other tools at our disposal.

In the following example we will be making use of objects, lists, cycles and random module. The example will come to solve a problem in which sometimes we are, we do not know what to put on to go out. This script, will allow us from a list of elements in our closet, to help us to select randomly what to put on.

1import random 2 3def outfit_maker(wardrobe): 4 for clothing in wardrobe: 5 print(f"{clothing}: {wardrobe[clothing][random.randrange(len(wardrobe[clothing]))]}") 6 return "and that's what you should wear today!" 7 8 9my_wardrobe = {"up": ["t-shirt", "long sleeves", "shirt"], 10 "down": ["jean", "formal", "bomber"], 11 "shoes": ["snickers", "formal", "flip-flop"], 12 "accessories": ["sunglasses", "bag", "cap"], 13 "main_color": ["red", "black", "white", "blue"]} 14 15print(outfit_maker(my_wardrobe)) 16 17# Output: 18# up: long sleeves 19# down: bomber 20# shoes: flip-flop 21# accessories: sunglasses 22# main_color: white 23# and that's what you should wear today!

Analyzing the example:

  • We import random to be able to use it in our code.
  • We created a myWardrobe object that will have the properties (keys) of our closet and the values will be the options we have to use.

An object (or dictionary) is a data type in Python that stores pairs of values (key : value).

  • We created the outfitMaker function that will receive as parameter our closet and will be in charge of choosing our outfit.
    • First we will create a loop to traverse our object.
    • Then we will look at each of the properties (keys) of the object and from the list of options, we will randomly select an element.
    • When the loop is finished, we will have received one item for each of the keys and it will then return the function "and that 's what you should wear today!"
  • The last step would be to execute the function by calling it and passing it our closet so that it can perform the operations.

As you can see, Python allows us to make small programs, as simple as one line, as well as programs a little more complex and others, which were not covered in this article, which would be much more complex programs that when done using Python, allow simplicity and at the same time robustness in the development and quality of the programs, you can read this article on What is Python for to have a broader idea about all its uses.

Hope you enjoy the reading and keep on the Geek side with 4Geeks!