The Python Syntax Guide — I

Important concepts to programming in Python

Dragon Nomada
nomadacode

--

Photo by David Clode on Unsplash

Python is an amazing programming language to solve a lot of problems. Python is very popular in Data Science, Machine Learning, Artificial Intelligence, Databases, and more. If you understand the main concepts and syntax, you can be able to develop amazing business reports, automation tools and data driven systems.

In this post we will review some syntax and concepts to start to programming. You don’t need to know advanced programming, only the basics.

Let’s go.

Variables

A variable is like a memory box, imagine a memory tag that allows you store some value and use it into your program.

Syntax — Define a variable <name> = <value>

Basic Python Types

Conditionals

Conditional block is a programming structure that determines which nested block of code can be executed based on a condition. You can use this structure to determine if your sentences will be evaluated based on a logic condition (True or False resulting value).

Syntax — Simple condition if <condition>: <block>

Simple condition determines when a nested code is evaluated based on a condition

Syntax — With else case if <condition>: <block True> else: <block False>

The IF-ELSE conditional can you determine what about when your condition is false

Syntax — Multi case if <condtion main>: <block main> elif <other condition>: <block to other> ... elif <another condition>: <block another> else: <block falsy>

You can handle several conditional cases

Iterators

An iterator is a structure that handle elements taken of a sequence. You can take each element of the sequence (one by one) and evaluate a nested block of code. You can use this structure to sum all elements, print reports, compute some statistics o solve tasks based on the next element.

Syntax — Iterate elements from a sequence for <element> in <sequence>: <block>

You can take the next element from a sequence to use it inside your code of block

Syntax — Simple range for <value> in range(<limit>): <block>

The ranges ignores the last number (the limit)

Syntax — From/To range for <value> in range(<from>, <to>): <block>

The ranges can start and end in custom values (ignores the limit value)

Syntax — From/To/By range for <value> in range(<from>, <to>, <by>): <block>

The ranges can change the increment value (the amount added)

Conditional Loops

A conditional loop is a structure that repeats a block of code while a condition has a true logic value. You can use this structure to repeat indeterminated code, for example, when you depends of user or data to know if you repeat some code (think about to display a menu and repeat operations).

Syntax — Simple loop while <condition>: <block>

You can change values inside your block to update the condition

Syntax — Indeterminate loop while True: <block> ... if <condition>: break

You can create a indeterminate loop and break it manually

Functions

A function is like a box that receives inputs and generate some output. You can use the functions to automatize your code and tasks. Imagine define one function to solve one problem, for example, a function that solves the price average of a list of products called averagePriceProducts(<products>) or a function that download some image from a url called downloadImage(<url>). You can abstract and generalize your code by functions.

Syntax — Simple function def <name>(): <block>

A simple function, without input and without output

Syntax — Complete functiondef <name>(<param_1>, <param_2>, ..., <param_n>): <block> ... return <output>

Functions can be used to compute results and solve tasks

Conclusions

In this guide part we can understand some python’s syntax and concepts to start to create code that solve tasks. Try to solve some problems to grow up into programming.

Exercise — Print two fruits

Define two variables with two fruit names and print both in the same line with format I like {} fruit, but I don't like {} fruit.

Exercise — Player 1 vs Player 2

Define two variables called player_1 and player_2 with some numbers (the skill level). Print if player_1 is better than player_2, if player_1 is worst than player_2 or player_1 has the same skill level that player_2.

Exercise — Find batman

Iterate the list ["robin", "batman", "superman"] and print “It is not my hero” if hero in list is not equals to “batman” for each hero in the list, but if hero is equals to “batman” print “Batman is my hero :D”

Exercise — Grow by middle

Create a variable called s = 1 and create a loop with the condition 2-s > 0.001, update the value of s inside the loop with s = s + s / 2, print each value of s.

Thank you

Follow me to let you know when the second part is ready.

--

--

Dragon Nomada
nomadacode

I love maths, algorithms, artificial intelligence, data science and zen's philosophy