Python – Basics


Rishi Sapra

Technical Community leader, speaker, trainer and evangelist specialising in Power BI and Azure. Formally recognised by Microsoft as a Most Valuable Professional (MVP), Fast Track Recognised Solution Architect (FTRSA) and Microsoft Certified Trainer (MCT).


print() and operators

Throughout the how-to and exercises, you will be seeing the “print” function quite often so it would be good to give some explanation about it from the start. We will be discussing functions in more detail here.

As the word “print” is followed by “()” it means that “print” is a function name. A function in Python does two things:

  • evaluates a value or more values
  • executes an action

Using the “print ()” function will evaluate the tasks within the brackets and then show the outcome on the screen. Functions take arguments. This is what we put in the brackets. The “print()” function also takes an argument in the brackets. The arguments that print takes can be different data types. You will read more about data types here.

The “end “-parameter

The print() function ends with a new line. You may wonder how to print without new line. The print() function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character/string using this parameter.

In the console try out:
print("Learn " , end ='')
print("Data Insights", end ='')

Compared to:
print("Learn ")
print("Data Insights")

print() function – the separator
When passing on more than one argument in the print() function, we can ask Python to separate them with an argument choice. See the following example:
print("We", "are", "coders", "in", "NY.", sep="-")
print("We", "are", "coders", "in", "NY.")

Numerical operators
After understanding numerical data types (integer, float), we now like to introduce Numerical operators. As a modern programming language, Python can perform the most basic numerical operations. We list them below and encourage you to try them out in the console:

Name Operation Example Output comments
+ Addition 13  + 7 20
Subtraction 13 – 7 6
* Multiplication 13 * 7 91
/ Float Division 13 / 2 6.5
// Integer Division 13 // 7 1 13 can be divided only once without the number becoming a float
33 // 7 4 33 can be divided only four times without the number becoming a float
** Exponentiation 2 **3
% Remainder 13 % 7 6 13 can be divided only once times without the number becoming a float, the remainder will be 6
33 % 7 5 33 can be divided only four times without the number becoming a float, the remainder will be 5

It is important to understand the precedence. Where there are several operators, Python has well-defined rules for specifying the order in which the operators in an expression are evaluated. As you know, multiplication and division have higher precedence than addition and subtraction. Python applies the same rules. Of course, precedence rules can be overridden by explicit parentheses.

String operators
Apart from numeric operators, Python also has string operators:
Let’s say we have to two variables called: “a” and “b”. Let us assign these variables a value:
a = hello
b = world
putting in the console
a+b  will output: “helloworld”
3 * b will give: “worldworldworld”

Try it out

Why not try the following out in the console:
X = goodbye
Y = friend
What will be the output of:
X + Y
2 * X

Special Characters

The escape and newline characters
Within Python there are certain characters the perform a special action. Suppose you want to print something on two lines. For this we use the special character “\n”. Python will create a new line instead of printing that as a string. Because of “\n” is known as a special character, a newline character.
Compare the following lines in the console
print ('This year we went to Italy for a long holiday.')
print ('This year we went to Italy \nfor a long holiday.')

empty for now

Try out in the console

# Simple concatenating
a = 'Hello '
b = 'world'
print (a + b)
X = 'goodbye'
Y = 'friend'
print (X + Y)
print (2 * X)
## 'end'-parameter
print("Learn " , end ='')
print("Data Insights", end ='')
## without 'end'-parameter
print("Learn ")
print("Data Insights")
## - the separator
print('##########the separator##########')
print("We", "are", "coders", "in", "NY.", sep="-")
print("We", "are", "coders", "in", "NY.")
print('##########special characters##########')
# Special Characters: without line break
print ('This year we went to Italy for a long holiday.')
# Special Characters: with line break
print ('This year we went to Italy \nfor a long holiday.')

Related Articles

Pandas – Replacing Values

Rishi Sapra

We have already seen that detecting missing values and filling them are important steps in the data cleaning process. Just as important is correcting certain data points by replacing them with correct values. With Pandas a user can use different techniques to replaces certain values. We will highlight a few of them in this section.… Continue reading Pandas – Replacing Values

Read More

Leave a comment