Introduction to Python
What is introduction to python?
In the past few years, introduction to python has evolved a lot and has become an industry standard for many domains, especially AI. The ease of syntax and community support made it possible for Python to grow at an exponential rate. It is said to be the best programming language to start from, as it is relatively easy, and the syntax is simple enough for a beginner to understand. Now, let us start03 from the basics of Python
Introduction to python basic data types
Just like other programming languages, Python has three basic data types
INT
INT is used to store integers.
Float
Float is used to store decimal values.
String
String or ‘str’ is used to store characters or words. Unlike other languages, Python has no data type for a single character.
Storing Values in the Variables
In order to store value in a variable, we use the assignment statement. This statement consists of a variable name, equals sign, and the value that you need to assign. You don’t need to define the variable type along with it; Python does it for you. For example,
var = 14
This syntax will assign the value 14 to the variable ‘var’. Python will automatically assign the ‘var’ as an int data type. If we change the value of the variable the data type will change accordingly, i.e.
var = ‘Hello’
In this case, var will have a string as the data type. Below are some things that you need to avoid while naming the variable.
Hyphens are not allowed
The name cannot start from a number
There should be no space in the name
Some special characters are also not allowed like $, ‘, @, etc.
String operations in introduction to python
In Python, there are a lot of string operations that make string manipulation quite easy. Here we will discuss a few of them
Concatenation: Concatenation means combining two or more strings together to form a new string. Unlike most languages, Python has a straightforward method of concatenating the string, and that is via using the ‘+’ operator. Two or more strings can be joined using the following syntax
‘String 1’ + ‘String 2’
The output of the following piece of code will look something like this
‘String 1String 2’
Note that you cannot concatenate two different data types together using the ‘+’ operator. For example, the following line of code will result in an error
‘String’ + 2
As the data types of both are not the same (one is a string, and the other is int) so this line will show an error. We can use an assignment statement to store the concatenated string to another string variable.
Replication: To replicate a string in Python, we use the ‘*’ operator. This operator is also used for the simple multiplication of integers. But to use it as a string replication operator we input it a string and an integer as shown below
‘Hello’ * 3
This piece of above code will generate the following results
‘HelloHelloHello’
bear in mind this operator does not fit for any other combination of data types. Even if you try to pass a float value instead of an integer, it will give an error.
Print () Function
The print function is used to print out any given variable or string on the terminal. It takes in the argument in the form of variables or sometimes even the raw strings. If no parameters/arguments are passed, then it will print out a single empty line. The line of the function code is as follows
Print (‘Hello World’)
It will generate the following output
Hello World
The ‘end’ represents the ending character of the line. For example, if you want to end a line with a full-stop you can use the following syntax
Print (‘Hello all’ , end = ‘.’ )
By default, the end argument is set to be ‘\n’ which represents a new line.
Input () Function
This function is used to take an input value from the user. When this function is executed, the program waits for the user to type something and press enter. The entered value is stored in the variable that is used in the assignment statement. For example,
Name = input ()
This line will take the input from the user and store it in the ‘Name’ variable. You can also pass a message as an argument, that you want to display when asking for value i.e.
Name = input (‘Enter some value : ’)
Introduction to python List
A list is the data type in Python that stores data elements in an order. It is like an array that can store data of multiple types. Square brackets are used to initialize the list, and the values of the list are typed inside the square brackets as shown below
a_list = [‘aString’, 12, 3.14]
Note that a single list can contain multiple types of data. This data can be addressed by using numbers as shown in the figure
So if you want to change or display the integer from the list, you can use the following syntax
a_list [1]
You can also use negative indexing to access the elements. Negative indexing starts from -1, which points to the last element in the list. For example, if you want to access the integer value using the negative indexing, the syntax will be
a_list [-2]
Dictionary
Dictionary is also a collection of data, but unlike lists, it works on the key-value pair rather than indexing. In a dictionary, there is a key that has a specific value assigned to it, and that value can only be accessed using its particular key. It is declared using the square brackets, and the syntax is as follows
a_dict = {‘key1’ : 12, ‘key2’: ‘itsValue’, ‘key3’ : 3.14}
In order to access the value, we use the following syntax
a_dict [‘key3’]
This will give us the following output
3.14
Tuples
Tuples and lists are nearly the same things; the only difference being that tuples are immutable. You cannot change any value in a tuple, and they’re represented by curve brackets. The syntax is also similar to list, and it supports all types of data, just like lists. For example, we can define a tuple using following syntax
a_tuple = (12, ‘aString’, 3.14)
To access the elements, we use the same indexing methods. For example, you can access the float value from the tuple using the following syntax
a_tuple [2]
Read more about this