Functions in Python - An introduction

We take an example. Let’s assume you go to a new city and you need a hotel for a night's stay. You can ask the hotel manager about the room rent. You expect that if you are booking a room in a hotel, the bed with mattresses would be there. You further expect TV, carpet, washrooms, etc. You will probably not ask for the availability of these things from the hotel manager. If you have some special requirements, you will tell only those requirements to the manager like internet connectivity, room service for dinner, etc.

In the case of functions, they come with pre-defined features. They can do calculations or generate results as per your requirement but if you want, you can ask functions to customize some of the features.

Always keep in mind that inputs to the functions are provided inside the brackets and are separated by commas.

In the first program, we had a print function and the input to the print function was provided inside the bracket. Now, if we want to print multiple lines. This can be done by using multiple print functions as under

print(“Hello world”)

print(”welcome to python”)

 The output generated by the above code will be two lines

Hello world

welcome to python

 The python print function has a default feature to print the given in a single line and ends the line with a new line character i.e. it changes the line. For each print function, the output will be on a separate signal line.

 Now, if you want to print these two in a single line then you have to give some additional instruction to print() function.

print(“Hello world”, end=” “)

print(”welcome to python”)

 The output of the above code will be

Hello world welcome to python

 In the above code, we have instructed the print function to end the sentence with a space instead of a new line character. Please note that in programming language a new line is also considered as a character. In python, a new-line character is denoted by “\n”

 Try this code

print(“Hello world\nwelcome to python”)

 The output will be

Hello world

welcome to python

 The “\n” is since a new line character, the print function has printed the rest of the matter on a new line.

Try this one

print(“Hello world”,end=”apple”)

 The word “apple” will be added after “Hello world” is printed.

Try this one

print("hello","world","welcome","to","python")

 Output:

hello world welcome to python

Each word was supplied by adding commas inside the print function. The print function has added space between the words before printing them.

In short, you have various functions in python, and functions can be called by their names for executions and special instruction to each function can be provided inside the brackets. If a function can accept more than one instruction, they are given inside the bracket and each of them is separated by commas.

Index to "Basic Python for Traders"

Resources

Highest Rated Udemy Course on PineScript - Grab your Seat Now 

Udemy Discount Coupon Code : UDEMY-JAN23 (Valid upto 30th Nov 2023)

Learn more about coding on tradingview in PineScript through Books on pinescript available on amazon and kindle.


200+ pages book100 pages book200+ pages book


Point and Figure Charts : A Time-Tested Tool for Technical Analysis

In the dynamic world of financial markets, investors and traders constantly seek tools that can provide valuable insights into market trends...