Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Basics of Python Programming – Part II

Basics of Python Programming – Part II

Posted August 19, 2022 at 9:00 am
Chainika Thakar
QuantInsti

See Part I for an overview of the benefits and applications of Python in trading.

What is syntax?

Syntax implies the set of rules which define how the program will be written and interpreted.

For example, this is the Python syntax:

print (“Welcome, John!”)

In the above example, syntax implies the characters such as opening and closing brackets () and the inverted commas “” as well as the keyword “print”.

Output:

Welcome, John!

Next, let us find out what indentation is.

What is indentation?

Python uses indentation to identify the different blocks of code. The white space indicates the indentation. All the codes with the same spaces on the left belong to the same block of code. Usually, the number of spaces on the left are four.

Take a look at the example below:

Output:

64

In the example above, since the second line of code is a separate expression, the indentation is not correct. Hence, when you run the code it throws an error. But when you remove the space/indentation, the correct output is shown.

Next, we will learn about the basic coding requirements which are variables and operators.

What are variables and operators?

Variables

Variables are characters or words that are given a user-defined value. Variables, as the name suggests can be varied during the execution of the code. They are allocated a space in the computer memory based on their data type. Python doesn’t require explicit declaration of the data type for a variable.

Example 1

Output:

45

1456.8

Alex

Example 2

Output:

100

Changing the value of a variable

Output:

Original value:  100

Updated value: 120.3

Operators

Operators are used on the values and variables in Python for performing certain operations.

Take a look at different types of operators:

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Operator precedence

Arithmetic operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, modulus, exponentiation and floor division.

For example:

Output:

12

4

32

Relational operators

Relational operators compare the values of two expressions and return true or false.

For example:

Output:

Is a greater than b?: False

Is a smaller than b?: True

Is a equal to b?: False

Is a not equal to b?: True

Is a greater than or equal to b?: False

Is a smaller than or equal to b?: True

Logical operators

Logical operators perform Logical AND, Logical OR and Logical NOT operations.

For example:

If A, B and the combinations are as follows:

CaseABA and B
print(A and B)
A or B
print(A or B)
Not A
print(Not A)
Not B
print(Not B)
1TrueFalseFalseTrueFalseTrue
2TrueTrueTrueTrueFalseFalse
3FalseTrueFalseTrueTrueFalse
4FalseFalseFalseFalseTrueTrue

In the table above, you can see how AND, OR and NOT are giving output.

Operator precedence

This is used in an expression with more than one operator to find out as to which operation to perform first.

Take a look at the table below to find out all the operators of different precedence:

Operator in charactersName of the operator
()Parentheses
**Exponent
* , /, %Multiplication, division, modulus
+-Addition, subtraction
<<=>>=Relational less than/less than or equal toRelational greater than/greater than or equal to
is, is notin, in notIdentityMembership operators
notLogical NOT
andLogical AND
orLogical OR

For example:

Output:

Hello! Welcome.

Next comes the section explaining conditional statements and loops.

Stay tuned for the next installment, in which Chainika Thakar will go over the conditional statements and loops.

Visit QuantInsti Blog to learn more about this topic: https://blog.quantinsti.com/python-programming/.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.