Basics of Python
Small announcement for upcoming tutorials: We will have a general overview of python programming language. The requirement to proceed further is to have basic knowledge about programming, if not please check out the course – Click Here. If you are much aware of the python programming language and about the basics of python you can skip all these python tutorials. The content we will be covering in python for Data Science and Machine learning:
- Data Types
In Data Types:
- Numbers
- Strings
- Printing
- Lists
- Dictionaries
- Booleans
- Tuples
- Sets
- Comparision Operators
- If, elseif, else Statements
- For Loops
- While Loops
- range()
- List Comprehension
- Functions
- Lambda Expressions
- Map and Filter
- Methods
Data Types (Python)
Basic arithematic operations-
2 + 5 # In python to add numbers just put + sign in between operands
5 #output
2 – 5 # subtract second number from first
-3 #output
10/2 # division of two numbers
5 #output
11 % 2 # this is known as modular division where remainder is returned
1 #output
5**2 # exponential manipulations ( 5 to the power of 2 )
25 #output
5 + 7 * 2 + 4 # Python will multiply the numbers first and next addition
5 + 14 + 4
23 #output
(2 + 3) * (5 + 5) # if we want to make additions first then we have to put them in parenthesis
50 #output
Assigning some value to a variable :
The variable name should follow some rules
- It shouldn’t start with a numeric digit.
- And shouldn’t have any special characters other than “_”.
- It should not consist of spaces in between, Use “_” for word separation.
Defining a String: A string (combination of alphabets and numeric digits ) must be defined in between of “string” or ‘string’.
String1= “Robotic electronics” # defining a string
String2= ‘Robotic Electronics’ #defining a string
Now once a string is defined , we will be learning how to access a string, There are two ways to access a string.
String1 # write the variable name
‘Robotic electronics’ # string output , or else the other method is
print(String1) # write the variable name inside a print statement
Robotic Electronics
There is a difference between two methods:
1. There is Out[13]: display , when we use to access a string directly and the string is displayed inside ‘’
2. accessing the strings using print statement is meant to be better way of display.
Indexing and Slicing :
Once we define some data, then the major part of our job is left which is accessing the data in different forms. Let us explore few of them.
Indexing:
Accessing one of the character or integer value from the string is known as indexing.
In python , indexing starts with “0” and from the last indexing starts from “-1”. Let’s see some codes in jupyter notebook.
String = “Robotic Electronics”
print(String)
Robotic Electronics #output
String[0]
‘R’ #output
String[7]
‘ ‘ #output
String[-1]
‘s’ #output
print(String[0])
R #output
print(String[7])
# here is a space output
print(String[-1])
s #output
Adding two strings:
Let’s assume we have multiple strings and we wanted to combine all the strings in a single string. E.g: “robotic’ and ‘electronics’ are the two strings that we wanted to add by giving a gap between them. Then simply type
‘robotic’ + ‘ ‘ + ‘electronics’
Slicing:
In Indexing, we have to learn how to access single elements. In slicing, we will learn how to access more than one element from a string. Here we will be using colon (:) to perform such actions. We will indexing one number before the colon, which means from that point to the index that is used after the colon. Few Operations that can be performed on strings. Let’s assume we are having a string
String_1= ‘Technologies we cover are “data scince” “Arduino” “IoT” “Robotics” “Electronics”‘.
>> String_1.lower( ) #this will covert all the characters to lower case alphabets.
>> String_1.upper ( ) #this will covert all the characters to lower case alphabets.
>> String_1.split ( ) #this will split the string with respect to space that means wherever there is a space between the characters.
>> String_1.split (‘o’ ) #this will split the string with respect to the character ‘o’.
String = “Robotic Electronics”
print(String)
Robotic Electronics #output
String[0:3]
‘Rob’ #output
String[-4:-1]
‘nic ‘ #output
Things that we have covered in this tutorial about the basics of python are:
- About arithmetic operations
- String creation and display
- Indexing
- Slicing
In upcoming tutorials we will learn about:
- Lists
- Dictionaries
- Tuples and sets
- And will continue our python crash to course to master in Data science and machine learning
This is all about the basics of python. If you are new to this tutorial you can check the previous tutorial for better understanding – Click Here. Any queries let us know in comment section.