Financial Talks

Why financial talks are necessary? Have we ever thought of this…………? The answer to this is simple, if we want money to work for us instead of working for money then these talks are necessary. We…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




ALL YOU NEED to Get started with Python

Python is a high-level, general-purpose programming language. It was created by Guido van Rossum and first released in the year 1991. Python is a very powerful and efficient programming language. It is being used more and more with each passing days, from software development to data science. It is being used in every field. To get started with a journey in python, these are the basics of the language that you must learn.

This article will help to learn the basics of Python in less than 10 minutes.

Points covered are different data types, taking input and giving output, strings and their manipulation, lists, sets, dictionaries, data type conversion, conditional statements, functions definition and calling, and iterative statements(loops).

We have used Python 3.7 in this article.

Major Data Types in Python:

A great advantage in python is that there is no size limit to any data type like it is in C++ or Java. Numbers and strings can be as large as needed.

Taking inputs and Giving outputs:

To take input, use the input keyword. By default in python, data is stored in strings data type.

Here the input data is stored in the variable a

To display some text before taking input, enter the text inside the parentheses (“…”).

As the data inputted is by default stored in string data type, if you want to store an integer, write int(input()).

Now a will store a number, if a character is inputted, then an error will be shown

To display some data, use print() data type.

This will display the data stored in variable a. Note a is a variable, not a character ‘a’

To display a string or a character use single or double quotes.

To display two or more variables along with some message in a single statement.

The integer values stored in a and b will be displayed

To format the output of a floating number.

This will give the output 23.45

Data Type Conversion:

As every input you take is stored as a string by default in Python. So, if you want to input an integer or a floating number then you have to perform data type conversion right after taking input. Also, there is no size limit on the value that is stored on the variable as python takes care of that, this is one of the advantages of Python over other programming languages.

There are two types of data type conversion:
1. Implicit type conversion: This means that the compiler converts data type on its own, without any external trigger from the user.
2. Explicit type conversion: User casts a data type into another data type as per his/her need.

To convert a variable from one data type into another, the syntax is:

As your default data type is string, if you want to input a number you will write int(variable) after taking input or you can also write int(input()) to convert the data type while taking input.

In the above example, if you enter a floating number, int() will convert it into an integer.

If you want to convert a number to a string, we will use str().

Variable will store the value “12”

Note: chr() cannot be used to convert a variable to a character as it is used to convert ASCII values to characters.

When you convert a float number into an integer, then all the digits after the decimal will be ignored.

What will happen if you convert an integer into a floating number?

Strings and their manipulation in Python:

By default, any input you take it is stored as a string in Python. So, it is quite important to study strings separately. To convert any data type to string, we use str() as mentioned earlier.

In Python, you cannot propagate through the string as in C++ or Java.
What this means is that if you have a string “Python” stored in the variable s, in C++, if we write s[1], we will get ‘y’ (as indexing starts from 0) which is not the case in Python. If you write s[1], this will generate an error.
To propagate through the string, we need to convert the string into a list, which will be discussed in the later section.

To concatenate two or more strings, we simply put ‘+’ between the strings.

To count the number of occurrences of a character in the string, we use count() function.

To concatenate the same string n times.

Question: What will be output of “abc” + str(3)*3 + “4”*2?
Answer: “abc33344”

Lists:

Lists are just like the arrays, declared in other languages. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are also very useful for implementing stacks and queues. Lists are denoted by [].

To convert any data type into a list, we use list().

This will give output [‘a’, ’b’, ’c’]

To add an element to a list, use append().

The output will be [1, 2, 3]

To access any element from a list, simply use the index of the element.

Output is 2
The output is [2,3]
The output is [1,3]
The output is [2], as 1 and 3 get sliced form the list.
The output is [1, 2, 3, 4]

Sets:

A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Sets are denoted by {}.

To convert a list or any data to a set, we use set().

This will give output {‘a, ’b’, ’c’}
The output is {1, 2, 3}

To perform the union of two sets:

To perform the intersection of two sets:

The output is {2}

Dictionary:

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds the key: value pair.

The output is {‘a’:1, ‘c’:3}

Conditional Statements:

In programming languages, most of the time we have to control the flow of execution of your program, you want to execute some set of statements only if the given condition is satisfied, and a different set of statements when it’s not satisfying.

Types of conditional statements:
1. If statements: If the condition is True, then a set of statements inside the block are executed.

The output is Zero. If a is not equal 0 then, nothing would have been printed.
As 19%2 is not equal to 0, so the statements in ELSE block are executed, so the output is Odd.
As -5<0, the statement in the ELIF block are executed, the output is Negative.

Functions:

A function is a set of statements that take inputs, do some specific computation and produces output.

In general, functions are of two types:
1. Built-in functions: Those functions that are already present in the libraries of the compilers.
2. User-defined functions: Those functions which are created by the user to support and fulfill the need of the user.

Some important built-in functions:

User-defined functions:

In the function defined above, n is the argument that is passed while calling the function. Unlike C++ or Java, in Python, the data type of the argument is not necessary to be given. The compiler assumes the same data type as the value that the function calling statement has.

Here evenodd() is the function that is defined earlier. As 24 is an integer, so, n in the definition of the function is also an integer.

You can pass more than one arguments and they can be of different data types.

One more example,

The output is 3 does not divide 4. See explanation below.

In this example, the function divisibility() is defined from line 1 to line 7. Function divisibility() is called on line 9 with two arguments which are both integers, 4 and 3.
While defining the function, we have written n=3 and a=1, this is called default arguments. A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument with a default value.
It means that is we have not passed a sufficient number of arguments while calling the function, then the function divisibility() would have used these value, which is n=3 and a=1. If the values are passed then n and a will use the values that are passed.
On line 2, we have checked if a==0, because if a is equal to 0 and n%a is performed, this will mean n%0 and an error will be shown known as divisible by zero.
In our example n=4, a=3, so on line 4 when n%a is checked, we know that 4%3==1, so the function divisibility() returns False. If the function return False, then the statements inside the else statement are executed which are starting from line 12. So the output is 3 does not divide 4.

Iterative Statements (Loops)

In Python, we have three ways of writing loops:
1. While loops
2. For in loops
3. For loops using indices

While loops:

In python, while loop is used to execute a block of statements repeatedly until a given a condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed.

For in loops:

For loops are used for sequential traversal. These are used to traverse through a list, a set or a dictionary.

For loops using indices:

Here, for loops can be used to traverse through a list, a set or a dictionary using index values.

The output is 1 2 3

We can also write one loop inside another loop, which is known as nested loops. We can also write conditional statements inside loops. Try to solve the example below and find out what the output will be?

We have covered almost everything to get you started with python. Now, all you need is practice and by practicing you will much more stuff than just reading. Can think of someone right now who should read this? The ‘Share’ button is all yours. Bookmark this link, comment your ideas and don’t forget to Clap.

Add a comment

Related posts:

Target Marketing in the Era of Data New Deal Policy

IT industry has been earning a huge amount of money through the business model of building a big data based on customer information and utilizing it. In the fourth industrial revolution, data has…

Buy Negative Google Reviews

Negative reviews have become a major part of the Internet. And Google is taking this to a new level by showing. How many Negative reviews exist for different websites. Negative reviews are a problem…

Stacie Whisonant

Stacie is the Founder and CEO of a FinTech startup called Pay Your Tuition Funds in Washington, D.C. PYT Funds focuses on providing students with the opportunity to utilize crowd-giving and…