Basics of Python — Part I

Vijay Gadre
Geek Culture
Published in
6 min readOct 19, 2021

--

VARIABLES

One of the main concept in programming is variables. They are your best friends. You will deal with them all the time. You will use them to store information. They will represent your data input.

Let’s say you want to have variable x that is equal to the value of 5 and then ask the computer to tell you the value of that variable. Type x equals 5.

Write x and then execute. And here’s the result — 5.

An alternative way to execute the instruction that will provide the value we assigned to y would be to use a print command.

If we say print(y), the machine will simply execute this command and provide the value of y as a statement.

NUMBERS AND BOOLEAN VALUES

When programming, not only in Python, if you say that a variable has a numeric value, you are being ambiguous. The reason is that numbers can be integers or floating points, also called as floats, for instance.

Integer — Positive or negative whole numbers without a decimal point. [Eg. 5, 10, -3, -1]

Floating point (float) — Real numbers. Hence, they have a decimal point. [Eg. 5.222, 6.3132, 34.312].

Boolean Value — a True or False, corresponding to the machine’s logic of understanding 1s and 0s, on or off, right or wrong, true or false.

STRINGS

Strings are text values composed of a sequence of characters.

Type single or double quotation marks around the name George. Python displays ‘George’ if you don’t use the print command. Should you use print, the output will be shown with no quotes — you’ll be able to see plain text.

To type “I’m fine” you’ll need the apostrophe in the English syntax, not for the Pythonic one.

In such situations, you can distinguish between the two symbols — put the text within double quotes and leave the apostrophe, which technically coincides with the single quote between I and M.

An alternative way to do that would be to leave the quotes on the sides and place a back slash before the apostrophe within the phrase. This backslash is called an escape character, as it changes the interpretation of characters immediately after it.

To state “Press Enter”, the outer symbols must differ from the inner ones. Put single quotes on the sides.

Say, you wish to print “Red car” on the same line. “Add” one of the strings to the other by typing in a plus sign between the two. Put a blank space before the second apostrophe of the first word.

Type “print ‘Red’”, and then put a comma, which is called a trailing comma, and Python will print the next word, ‘car’ on the same line, separating the two words with a blank space.

ARITHMETIC OPERATORS

In the equation you see here, 1 and 2 are called operands, the plus and minus signs are called operators, and given they also represent arithmetic operations, they can be called arithmetic operators.

  • + = Addition
  • — = Subtraction
  • * = Multiplication
  • ** = Power
  • / = Divide
  • % = Remainder

THE DOUBLE EQUALITY SIGN

The equals sign when programming means “assign” or “bind to”. For instance, “assign 5 to the power of 3 to the variable y”; “bind 5 to the power of 3 to y”. From that moment for the computer, y will be equal to 125.

When you mean equality between values and not assignment of values in Python, you’ll need the double equality sign. Anytime you use it, you will obtain one of the two possible outcomes — “True” or “False”.

REASSIGN VALUES

If you assign the value of 1 to a variable z, your output after executing z will be 1. After that, if you assign 3 to the same variable z, z will be equal to 3, not 1 anymore.

Python reassigns values to its objects. Therefore, remember the last command is valid, and older commands are overwritten.

ADD COMMENTS

Comments are the sentences not executed by the computer; it doesn’t read them as instructions. The trick is to put a hash (#) sign at the beginning of each line you’d like to insert as a comment.

If you would like to leave a comment on two lines, don’t forget to place the hash sign at the beginning of each line.

LINE CONTINUATION

You might prefer to send part of the code to the next line. So, 2.0 times 1.5 plus 5 could be written in two lines, and the machine could still read it as one command. This could be achieved by putting a back slash where you would like the end of the first line to be. It indicates you’ll continue the same command on a new line.

INDEXING ELEMENTS

Is it possible to extract the letter “d”?

Yes, you can do that by using a square brackets []. And within them, you should specify the position of the letter you’d like to extract.

A very important thing you should remember is that, in Python, we count from 0, not from 1! 0, 1, 2, 3, 4 and so on. That’s why I’ll ask for the 4th letter, ‘d’, by writing 3 here.

STRUCTURE YOUR CODE WITH INDENTATION

The way you apple indentation in practice is important, as this will be the only way to communicate your ideas to the machine properly.

Def and Print form two separate and, written in this way, clearly distinguishable blocks of code or blocks of commands.

Everything that regards the function is written with one indentation to the inside. Once you decide to code something else, start on a new line with no indentation. The blocks of code are more visible, and this clarifies the logic you are applying to solve your problem.

COMPARISON OPERATORS

  • == : Verifies the left and right side of an equality are equal.
  • != : Verifies the left and right side of an equality are not equal.
  • > : Greater than
  • < : Less than
  • >= : Greater than orEqual to
  • <= : Less than or Equal to

LOGICAL OPERATORS

Briefly, the logical operators Python are the words “not”, “and”, and “or”. They compare a certain amount of statements and return Boolean values — “True” or “False” — hence their second name, Boolean Operators.

“Not” = Leads to the opposite of the given statement.

“And” = Checks whether the two statements around it are “True”.

“Or” = Checks whether at least one of the statements is “True”.

You must respect the order of importance of these three operators. It is: “not” comes first, then we have “and”, and finally “or”.

IDENTITY OPERATORS

The identity operators are the words “is” and “is not”. They function similar to the double equality sign and the exclamation mark and equality sign we saw earlier.

--

--

Vijay Gadre
Geek Culture

Data Scientist | Machine Learning Engineer | Artificial Intelligence Engineer