Python Basics, and How They Add Up

Emiko N-P.
2 min readSep 27, 2021

--

This afternoon I completed a few lessons of Python. I’ve been slowly getting into the language for a while, but really dug into lessons today. It felt good to really start learning a new coding language. For this blog, I wanted to talk about some Python basics and how it feels starting out in Python as a Rubyist.

The first and possibly most important thing to learn in Python is the print function. This function prints it’s input to the console, so you can see your output and make sure your code is working. As a Rubyist, print is a familiar function, albeit not as common as puts, so it’s not hard to grasp its use in Python. In Python, functions are called with parentheses, so if you wanted to print ‘hello world’ it would look like this:

print(‘hello world’)
=> hello world

Now we can print out our results, let’s move on to another very important concept: variables. Like in other programming languages, variables in Python are used to store data to be referred to later. In Python variables are named in snake case, and names cannot start with a number or include any reserved words.

ex.

Hello

A_good_name

Best_variable_ever

To define a variable in Python, you simply use the assignment operator (=), to set the variable name equal to its value. As a Rubyist, I’m well acquainted with this format, which makes it easy to pick up and use quickly.

my_first_variable = “I’m so excited”

Lastly let’s take a look at Python’s arithmetic operators. Python has seven main arithmetic operators: addition(+), subtraction(-), multiplication(*), division(/), modulo(%), exponentiation(**), and rounded division(//). The first four do exactly what is says on the box. Modulo gives the remainder after division. Exponentiation allows you to set an exponent like 42. Rounded division divides then rounds the quotient down to the nearest integer. These operators are used the same way that you would use any of the corresponding symbols in math.

print(4+4)=> 8print(9-2)=> 7print(3*5)=> 15print(9/3)=> 3print(7%3)=> 1print(5//2)=> 1

They are also all the same as those in Ruby, except rounded division. This is because Ruby automatically rounds down the quotient on division, while Python allows for more precision in calculations by returning the precise quotient including decimals. Python also does something else that makes it better for math than Ruby: it knows order of operations. This makes it much easier to write complex mathematical expressions without needing tons of parentheses to ensure operations occur in the correct order.

print(3 + 1 + 2*4)=> 12

Now we’ve gone over some Python basics, you can go and try them for yourself in your favorite IDE and see how Python compares to your favorite coding language!

--

--

Emiko N-P.
Emiko N-P.

Written by Emiko N-P.

0 Followers

Hello, my name is Emiko. I am an aspiring Software Engineer and student at Flatiron School.

No responses yet