Now the python is running in your systems, lets dive into python without wasting any more time.
Now let's Write Your First program of Printing 'Hello World':
Open your idle and go to the ‘Files’ tab and open new file.
Save this file as ‘hello.py’
and write the code as following:
And go to the ‘Run’ option on taskbar and click on run module or you can directly run by pressing F5 key.
The second option is running from the terminal. The following example is given in linux terminal but it can be executed from all other operating system.
Python3 following by the path of the program file. Same command will be run on Mac but on windows use “python” instead of “python3”.
What are Variables in Python:
In python there is a strict pattern to name the variables. User cannot name a variable in any fashion. We can only use characters, numbers and an underscore.
Number should not be use at the starting of variable. Following are some example to name the variables:
This is the right way to name a variable.
Syntax error will be generated if we named a variable in a wrong way as following:
What are Strings in Python:
Unlike C, C++ and Java there is no specify data type before initializing the variable. Python will make decision on run time weather the variable is String, Integer or Float.
So, at first, we will have a look on how we can initialize strings.
How to do String Initialization:
In python string can be initialized by the following methods:
How to do String Slicing:
We can slice the strings in python, this procedure is known as string slicing in programming. String slicing accept the starting index and ending index within square brackets.
Note: The index starts from 0
Following are the examples to slice the strings:
We can also get the index of specific word in a string by running the following program:
Following are the indexes at which these words are starting from. We can also get the length of string by python built-in function len(). It is to be noted that length function starts from 1 whereas index starts from 0.
What is String Manipulation?:
String manipulation is a technique to change the string according to the need. We can remove the characters from the string, split the string into half, and search for the characters in a string.
How to String.split():
Split functions take one argument and split the string on that argument. If that argument is present in the string then that string will splinted otherwise nothing will happen. Split function returns the value as a list. We will see in later part that what is list and how to handle list.
Strip function take the argument and remove that value from that string. By default, if no parameter is passed in strip function than it removes the starting and ending spaces of string otherwise it removes that particular character from that string.
Following is the example of stripping function:
Replace function replace the value from a particular string. It takes 2 arguments, first one is the character that is to be replaced and the other character that will be the value of that replacement.
Upper and Lower:
Upper and lower function convert the string from lower case to upper case and from upper case to lower case in later scenario. These are used for string manipulation.
Why these string manipulations are used?
This string manipulation is used to clean the data. In advance topics when we will be studying data science this string manipulation will be extensively used to clean the data and organize the data in a format in which we want to see it. Moreover, these are used to see special characters whether they are present in a string or not or to make a long sentence into words as we see in the example of splitting function.
Concatenation is process to add two strings to form as 1 string. In python there are two different ways to concatenate the strings. One way of concatenating the string is to use plus (+) operator between two strings and the other way of doing so is to use coma(,) symbol between two strings. The later method is only applicable when printing the two variables.
String formatting is a technique to create a new string by adding the variable without concatenating. There are several ways of formatting the string in python. Following is the way to implement the formatting of the string.
- The first example is called c-style formatting. In this formatting we define the string value by “%s” and for integer “%d” is used. The way to implement the method is as following:
- The second example of doing so is by using. format function. In this method curly braces are made within string which is replaced by the variable when it is printed.
- The third technique is easier than above mentioned examples, we use “f” at the start of the string two specify the formatting, which is as follows:
This was all for today see you in the next lecture.