Basic Python for Data Science
- Python is an interpreted, high-level, and general-purpose programming language.
- Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
- Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented, and functional programming.
- Python is often described as a “batteries included” language due to its comprehensive standard library.
Basic Commands
Now let us take a look at the most useful commands that you require everyday while coding in the Jupyter notebook.
Command Mode Shortcuts
Esc
: To go into command mode
Enter
: To go back to edit mode
M
: To convert a cell to a markdown cell
Y
: To convert a cell back to a code cell
A
: To insert new cell above
B
: To insert new cell below
D + D
: To delete cell
Z
: Undo last operation
F
: To find and replace on your code
Shift + Up/Down
: To select multiple cells
Space
: Scroll notebook downwards
Shift + Space
: Scroll notebook upwards
Edit Mode Shortcuts
Shift + Enter
: To execute the code in the current cell and go to the next cell
Alt + Enter
: To execute the code in the current cell and insert new cell below
Shift + Tab
: To get a brief documentation of the object that you have just typed in the coding cell
Ctrl + Shift + -
: To split the cell at cursor
Shift + M
: To merge selected cells
You can also use H
to open the list of keyboard shortcuts and even add new shortcuts or customize the existing shorcuts according to your personal requirements.
“Hello world” in Python
A one-line program that prints some version of the message “Hello world!” this is a simple program
Variables
A variable holds a value.
In python, variables can be declared and values can be assigned to it as follows,
Strings
Strings are sets of characters.
Single and Double quotes
Strings are contained by either single or double quotes.
Combining strings (concatenation)
It is often very useful to be able to combine strings into a message or page element that we want to display.
List Slicing
Indexing was only limited to accessing a single element, Slicing on the other hand is accessing a sequence of data inside the list. In other words “slicing” the list.
Slicing is done by defining the index values of the first element and the last element from the parent list that is required in the sliced list. It is written as parentlist[ a : b ]
where a,b are the index values from the parent list.
List Slicing
Given a non-empty list ‘names correct.
Copying a list
Most of the new python programmers commit this mistake. Consider the following,
Here, We have declared a list, lista = [2,1,4,3]. This list is copied to listb by assigning it’s value and it get’s copied as seen. Now we perform some random operations on lists.
List Casting
word[ : ] = [ ] print(word)
We are indexing all the elements of the list word to an empty list hence the final output will be an empty list. So this method can also be used to delete all the items from a list.
List Indexing
- Lists can be indexed using square brackets to retrieve the element stored in a particular position.
- Indexing is exactly similar to indexing in strings except that indexing in lists returns the entire item at that position whereas in strings, the character at that position is returned
negative indexing
Tuples
Tuples are data structures that are similar to lists in all aspects except in the way they are declared and how much they allow themselves to be modified. A tuple once created cannot be modified.
- Tuples are another way to combine different values.
- The combined values can be of different types.
- Like lists, they have a well-defined ordering and can be indexed.
- To create a tuple in Python, use round brackets instead of square brackets
As you saw in the previous segment, lists can be changed, and sometimes when you’re storing important information in a list and using it somewhere else in your code, you run the risk of accidentally modifying the list and losing data or corrupting it. Tuples help you solve this problem.
A tuple is a fixed-length, immutable sequence of Python objects. The easiest way to create one is with a comma-separated sequence of values:
Tuples are immutable
- Unlike lists, tuples are immutable. Once we have created a tuple we cannot add values to it.
Tuples are similar to lists but only big difference is the elements inside a list can be changed but in tuple it cannot be changed. Think of tuples as something which has to be True for a particular something and cannot be True for no other values.
Built In Tuple functions
count() function counts the number of specified element that is present in the tuple.
index() function returns the index of the specified element. If the elements are more than one then the index of the first element of that specified element is returned
Dictionaries
A dictionary is a collection of words along with their meanings or simpler explanations.The first thing that comes to mind when you hear about dictionaries is the Oxford dictionary, where you can look up meanings of words. So, you can imagine how dictionaries work.
Dictionaries are more used like a database because here you can index a particular sequence with your user defined string. To define a dictionary, equate a variable to { }
or dict()
It is a flexibly sized collection of key-value
pairs, where key
and value
are Python objects. One approach for creating one is to use curly braces {}
and colons
to separate keys and values:
Dictionary works somewhat like a list but with an added capability of assigning it’s own index style.
Getting a Value from a Dictionary.
Extract the company headed by Tim Cook from the dictionary
List of Values in a Dictionary.
Create a SORTED list of all values from the dictionary
Sets
Unordered collection of unique elements
Sets are a good way to get the unique elements out of a collection or to find common elements in various collections. Using sets is quick and can help solve tasks requiring deduplication.
Sets are mainly used to eliminate repeated numbers in a sequence/list. It is also used to perform some standard set operations.
A set can be created in two ways: via the set
function or via a set literal with curly braces{} Sets are declared as set()
which will initialize a empty set. Also set([sequence]) can be executed to declare a set with elements
symmetric_difference( ) function ouputs a function which contains elements that are in one of the sets.
Conclusion
Python is an interpreted, high-level, and general-purpose programming language. In this blog, I have tried to give you the basic idea about Python & Basic Command used in jupyter Notebook for better understanding . Yes, there is much more to be explored & coming soon with next blog.