Coding Ground https://codinground.com Code Made Simple Sun, 11 Oct 2020 13:19:52 +0000 en-GB hourly 1 https://wordpress.org/?v=6.9.4 https://codinground.com/wp-content/uploads/2020/05/cropped-circle-cropped-1-min-32x32.png Coding Ground https://codinground.com 32 32 How to calculate the R-squared in Python with and without sklearn https://codinground.com/calculating-r-squared-python/ https://codinground.com/calculating-r-squared-python/#comments Sun, 11 Oct 2020 12:13:48 +0000 https://codinground.com/?p=877 This tutorial is about calculating the R-squared in Python with and without the sklearn package.

For an exemplary calculation we are first defining two arrays. While the y_hat is the predicted y variable out of a linear regression, the y_true are the true y values.

import numpy as np

y_hat = np.array([2,3,5,7,2,3,8,5,3,1])
y_true = np.array([5,4,2,7,4,2,1,6,5,3])

Now we are calculating the R-squared out of those two variables.

The formulas for calculating the R-squared are:

R^2 = 1 - \frac{SSE}{SST}

where SST is:

SST =\sum_i (y_i - \bar{y})^2

and SSE is:

SSE =\sum_i (y_i - \hat{y}_i)^2

To understand the SST and SSE consider the following image found on Wikipedia and created by Orzetto (Please see the credits and license below the image):

Attribution: Orzetto / CC BY-SA (https://creativecommons.org/licenses/by-sa/3.0)

On the left-hand side, you see the SST – the total sum of squares which are just the squared differences between the actual y values and the mean y.

On the right-hand side, you see the SSE – the residual sum of squares which is just the summed squared differences between the regression line (m*x+b) and the predicted y values.

You can also just use the sklearn package to calculate the R-squared.

from sklearn.metrics import r2_score

r2_score(y_true,y_hat)

For an application of the R-squared on real data, you are kindly invited to check out the video on my channel

]]>
https://codinground.com/calculating-r-squared-python/feed/ 2
Game Development Guide https://codinground.com/game-development-beginners/ https://codinground.com/game-development-beginners/#comments Sat, 26 Sep 2020 06:11:51 +0000 https://codinground.com/?p=849 Hello there, are you trying to develop a game but don’t know how?! Well, you’re in the right place. This article contains most of the information you’ll need as a beginner. With ‘beginner’ I mean downloading studio aka game engine, the perfect programming language, sound effects, assets, etc. 

 Introduction To Game Development

Creating your game is a dynamic way to show the world your creativity. It acts as a platform for the world to explore your dreams , your art and your passion. In October 1958, Physicist William Higinbotham created what is thought to be the first video game which interprets tennis games. Since then millions of games have been published and many have become successful.

There are various genre for video games as well , some of them are 

  • Action Games
  • Action-adventure games
  • Adventure games
  • Role-playing games
  • Simulation games
  • Strategy games
  • Puzzle games
  • Sports games

2D vs 3D Games?

You must be wondering whether you should go for 2D games or 3D games? Well, the answer depends on you but if you want a bit of advice, then from my experience, if you are a beginner you should go for 2D Games. There are many reasons for that,

  • 3D games are harder to develop than 2D Games, but it really depends on whether you are good at modeling 3D textures or at sprites.
  • It takes a lot of time to develop a 3D game (as compared to 2D Game) and it can be frustrating since you are just starting with game development.

Pickup easier tasks in the beginning, they will keep you interested.

Also, 2D games are preferred by most indie developers as it takes less time and budget to publish than a 3D game. Most 3D games are usually developed by teams as it has many fields to focus on than 2D games.

Things you need to get started with the Game Development

Game Engine

It makes no sense creating codes if there isn’t a character to control. Game engine just does that for you. Selecting a good game engine is one of the most important thing that you need to develop a game. A good game engine allows you to develop games with ease without worrying about the smaller details. For example, you need gravity in your game? You can add that with just a click in a good game engine.

There are a lot of game engines in the market to choose from. The two game engines that I personally recommend are:

Unity

Unity is by far the most amazing and easy to use game engine that I’ve encountered. It allows you to develop both 2D as well as 3D games but is widely used to develop 2D games. It is beginner friendly with tons of tutorials out there to help you get started with it. It provides the platform for competitive game development with all necessary facilities and it is really simpler to use than many other game engines.
Unity also comes with a Asset Marketplace which has both free as well as paid assets for you to choose from.

Unreal Engine

When it comes to 3D Games, Unreal Engine is a total beast. Most of the high end games are powered by Unreal Engine. It is free to use for learning purposes and you might need to check it’s licensing if you are thinking of selling your game. The only downside is, it is not for people who are just getting into programming i.e., not for beginners. But if you are an experienced developer who wants to make a 3D game, this is your best bet.

Programming Language 

The best programming language for game development is C#  as it is preferred more by other game developers and you will find most of the source codes for games in this programming language.

You can start programming with the download of an IDE on your PC. For C# the best IDE would be Visual Studio or Visual Studio Code. 

Visual Studio if you have a high specs PC and VS code if not. Here’s the video tutorial on how to get started with VS Code for C#.

Download Visual Studio Code: Download Visual Studio Code – Mac, Linux, Windows
Download Visual Studio: Download Visual Studio 2019 for Windows & Mac 

Sound Effects

Sound effects are important to games as heart to our body. Without sound effects, games become unplayable. And when it comes to Sound Effects, there’s no better application than sfxr.

It’s totally free and provides many needed important basic sound effects for your game. It’s easy to use and you’ll save your precious money for the betterment of your game.  

You can download it from here sfxr

Assets 

Game Assets include everything that goes into the game like bricks , leaves , grass , characters, audio files, textures, etc. Simply put, every element in a game is an asset.
There are a lot asset marketplaces from where you can download assets for your game.

You should be good to go as a very beginner.

Get your free assets from here Top free assets

Tutorials

Now that you know what you need to start working on your first game. Here’s a playlist of tutorials that can help you get started with Game Development in Unity.

Good luck with your game development journey.

]]>
https://codinground.com/game-development-beginners/feed/ 1
Data Visualization using Matplotlib – Part 1 https://codinground.com/data-visualization-matplotlib/ https://codinground.com/data-visualization-matplotlib/#respond Sat, 11 Jul 2020 13:53:38 +0000 https://codinground.com/?p=776 Matplotlib is one of the most popular library for data visualization and that’s for a reason. It has so many features to offer and can be used without any external software except python and the matplotlib library.

In this article, you will learn how to use matplotlib to visualize data that will also enable you to better understand the data, extract information, and make more effective decisions.

Before going further into matplotlib, let’s talk about Data Visualization

What is Data Visualization?

“A picture is worth a thousand words.” We are all familiar with this expression. It especially applies when trying to explain the insights obtained from the analysis of increasingly large datasets. Data visualization plays an essential role in the representation of both small and large-scale data.

Data visualization is the graphical representation of information and data. Graphical representation(in the form of charts, graphs, and maps etc) allows us to better understand the relationship in the data, data visualization tools provide an accessible way to see and understand trends, outliers, and patterns in data

One of the key skills of a data scientist is the ability to tell a compelling story, visualizing data and findings in an approachable and stimulating way.

Prerequisites: – Python and Data Cleaning with Python Pandas

Exploring Datasets With Pandas

The dataset contains annual data on the flow of international immigrants as recorded by the countries of destination. The data presents both inflows and outflows according to the place of birth, citizenship or place of previous / next residence both for foreigners and nationals. The current version presents data pertaining to 45 countries.The dataset is available here.

Downloading and Preparing Data

import pandas as pd           #For reading the data

#Read the dataset skipping top 20 rows(irrelevant) and second last row
df_canada = pd.read_excel("./data/canada.xlsx",skiprows = range(20),skipfooter=2) df_canada.head() #View top 5 rows
flow of international immigrants as recorded by the countries of destination
#Check the dimensions of our canada dataset
df_canada.shape              # (195, 43) 
#Let gets insight of our dataset
df_canada.describe()
describing dataset

Cleaning the Dataset

Remove columns that are not informative to us for visualization(eg., type, area, reg)

df_canada.drop(["AREA","REG","DEV","Type","Coverage"],axis = 1, inplace = True)

#View columns of our dataset
df_canada.columns
viewing columns of dataset

Renaming Columns

df_canada.rename(columns={"OdName":"Country","AreaName":"Continent","RegName":"Region"},inplace = True)

#View columns of our dataset 
df_canada.columns

renaming columns

Check if column labels are string

For consistency, make sure that all column labels are of type string.

# let's examine the types of the column labels
all(isinstance(column, str) for column in df_canada.columns)
#False

df_canada.columns = list(map(str,df_canada.columns))
# let's examine the types of the column labels 
all(isinstance(column, str) for column in df_canada.columns)
#True

Set the country name as index – useful for quickly looking up countries using .loc method.

df_canada.set_index("Country",inplace=True)

Add total column

#Add a column Total in our dataset containing 
#total numbers of immigrants 
df_canada["Total"] = df_canada.sum(axis=1)

#View top 5 rows 
df_canada.head()
Adding to total column

Visualizing Data using matplotlib

Now after we have cleaned the dataset, its time to draw some plots. Plotting data using Matplotlib is quite easy. Generally, while plotting they follow the same steps in each and every plot. Matplotlib has a module called pyplot which aids in plotting figure. The Jupyter notebook is used for running the plots. We import matplotlib.pyplot as plt for making it call the package module.

Installing matplotlib

Type !pip install matplotlib in the Jupyter Notebook or if it doesn’t work in cmd type conda install -c conda-forge matplotlib . This should work in most cases.

#import necessary modules
import matplotlib.pyplot as plt

# use the inline backend to generate the plots within the browser
%matplotlib inline

LINE PLOTS

What is Line Plot? When to use Line Plot?

A line chart or line plot is a type of plot which displays information as a series of data points called ‘markers’ connected by straight line segments. It is a basic type of chart common in many fields. Use line plot when you have a continuous data set. These are best suited for trend-based visualizations of data over a period of time.

Let’s Start with a Case Study

In 2010, Haiti suffered a catastrophic magnitude 7.0 earthquake. The quake caused widespread devastation and loss of life and aout three million people were affected by this natural disaster. As part of Canada’s humanitarian effort, the Government of Canada stepped up its effort in accepting refugees from Haiti. We can quickly visualize this effort using a Line plot:

#First, we will extract the data series for Haiti.
years = list(map(str, range(1980, 2014)))
haiti = df_canada.loc["Haiti",years ]

# passing in years 1980 - 2013 to exclude the 'total' column
haiti.head()

pandas automatically populated the x-axis with the index values (years), and the y-axis with the column values (population). However, notice how the years were not displayed because they are of type string. Therefore, let’s change the type of the index values to integer for plotting.

haiti.index = list(map(int,haiti.index))

Also, let’s label the x and y axis using plt.title()plt.ylabel(), and plt.xlabel() as follows:

haiti.plot(kind = "Line")     #Plotting the line
plt.title("Immigration of Haiti")
plt.xlabel("Years")
plt.ylabel("Number of immigrants")

plt.show() # need this line to show the updates made to the figure
graph of immigration of Haiti

We can clearly notice how number of immigrants from Haiti spiked up from 2010 as Canada stepped up its efforts to accept refugees from Haiti. Let’s annotate this spike in the plot by using the plt.text() method.

haiti.plot(kind = "Line")     #Plotting the line
plt.title("Immigration of Haiti")
plt.xlabel("Years")
plt.ylabel("Number of immigrants")

plt.text(2000,6000,"2010 Earthquake")
plt.show() # need this line to show the updates made to the figure

graph of immigration of Haiti highlighting 2010 earthquake

We can easily add more countries to line plot to make meaningful comparisons immigration from different countries.

Lets Compare the trend of top 5 countries that contributed the most to immigration to Canada.

#Sorting the dataset using sort_values() method
df_canada.sort_values(by = "Total",ascending  = False, inplace = True)

#Extract the data for top 5 countries 
years = list(map(str,range(1980,2014))) 
df_top = df_canada.head()

#Transpose the dataset 
df_top=df_top[years].transpose()
df_top.head()

Now create a line plot and visualize the data

df_top.plot(kind='line')            #Plotting line

plt.title('Immigration Trend of Top 5 Countries')
plt.ylabel('Number of Immigrants')
plt.xlabel('Years')

plt.show()
Trend of top 5 countries that contributed the most to immigration to Canada.

Final Words

Thank you for the read. I hope that you have enjoyed the article. If you like it, share it with your friends. Also, I have a quick task for you to see how much you have learned.
You can think of it as an assignment.

Task.
COMPARE THE TREND OF LAST 5 COUNTRIES THAT CONTRIBUTED THE MOST TO IMMIGRATION TO CANADA.

I’ll be happy to hear your feedback. If you have some questions, feel free to ask them. 😉

]]>
https://codinground.com/data-visualization-matplotlib/feed/ 0
What is Data Science? How to Become a Data Scientist? https://codinground.com/what-is-data-science/ https://codinground.com/what-is-data-science/#comments Sat, 27 Jun 2020 15:39:14 +0000 https://codinground.com/?p=743 Hey Guys, this article is all about Data Science. You will get to know what is data science, what you have to learn to be a data scientist, what skills do you need, the job opportunities, and how you can become one. It will help you decide whether the Data Science field is for you or not.

So, without wasting any time, let’s dive in.

What is Data Science?

data science

Data Science, in simple words, is a study of data. You use various tools and technologies to generate useful insights and information from the raw data. Though, it is not as simple as it sounds. To become a data scientist, you need to know your way around mathematics, programming, statistics, and patience.

What does a Data Scientist Do?

A data scientist has to identify issues use data to solve them to help the company in decision making. They typically analyze, process, and model data to interpret the results.

Skills needed to become data scientist
Source: Springboard

As a data scientist, you have to gain insights into the data, and for that, you may have to build algorithms, do statistical analysis, data mining, and data visualization so that you will be able to create solutions to increase business performance.

As a data scientist you will be making programs that include creating various machine learning-based tools or processes within the company, such as suggestion engines or lead scoring systems. You should also be able to perform statistical analysis.

The Path to Data Science

There are various things that you need to learn to become a data scientist.

Programming Languages

First, you need to learn Programming Language to communicate with the computer.

Programming Languages for Data Science
Source: Assignment Expert

Python, R, and SQL are mainly used in Data Science. You can also use other languages for this, but these are widely used languages because they have a large number of libraries especially for this purpose. Like for python, there’s NumPy, Pandas, Matplotlib, Seaborn, SciPy, SkLearn, etc. These libraries make the job ten times easier. There’s a library for python for everything you need. You need graphs to visualize data? There’s matplotlib and seaborn. You need DataFrames, there’s Pandas. You need to perform vectorized operations on a big data set really fast? There’s NumPy for that.

The same is the case with R. With other languages, there are not many libraries and hence, Python and R are preferred over other languages.

We also have free python tutorials. Learn Python

Collecting Data

To do anything with data first you need to have data.

Data can be obtained through various sources. You will have to find them yourself but gathering and converting that raw data into useful data is something that you need to need to learn how to do. Sometimes, data is directly provided to you by the company/client you are working for, but most of the times you have to gather the data yourself.

There are some python libraries which can make the process easier. For example, suppose a website is providing public data in json format and you need that data to do work, now to use that data you first need to convert that data into the format that can be used for manipulation. To do that you need requests library. It helps you read and work with Json Data.

Web Scraping
Source: OctoParse

Similarly, there are cases where the website isn’t just giving out the data for free and you have to fetch the data, this is the part where web scraping comes into play. For example, suppose you have to fetch articles from Wikipedia, now Wikipedia doesn’t just give data in JSON format or RSS feeds, so to read the data, you need libraries like Scrapy, Beautiful Soup, etc. Learning how to automate the browser can also be handy to fully automate the process of fetching and converting the data for future uses.

Data Analysis and Visualization

Once you have the data you can start analyzing the data. Convert the raw data into dataframes using Pandas library.

Data Visualization
Source: Stanford University

Use libraries like Matplotlib and Seaborn to draw graphs. Make heatmaps, bar graphs, scatter plots. These graphs will help you understand the relationships in the data. What values are relevant, what values are irrelevant, what values can help related to the output you need. There are also tools like Power Bi by Microsoft and Tableau which can make the data visualization process easier.

You can check out these articles on NumPy and Pandas to get started with Data Science.

Machine Learning

Data Science doesn’t involve hardcore machine learning. That’s the job of a machine learning engineer. But you will be expected to know the basics and be perfect with algorithms. Having a great understanding of Neural Networks is, however, a plus point.

Machine Learning
Source: DataCamp

You have the data and you have already analyzed it. Now you need to make models that can be used to make predictions or perform particular tasks based on the data. All this comes under the machine learning territory. In most cases, as a data scientist, you will be required to make models that can predict some values based on the previous data that you provided. For that, you need to learn how to identify the type of problem(what type of problem you are solving) and the working of various algorithms. There are numerous algorithms that can be used to solve a particular problem but you need to choose the best one and for that, you need to be 100% sure about the type of problem and the category to which it belongs.

There’s no particular algorithm that works for all types of problems. An algorithm that works well with one dataset may not work with other datasets even if they are of the same category. Now I can go on and on about Machine Learning and algorithms but the post is not about that. We will go into detail when we talk exclusively about machine learning.

Mathematics

Yes, you need mathematics to become a full-fledged data scientist. Some people may argue that you don’t need mathematics to be a Data Scientist. It’s halfway true. You don’t need mathematics at the start of your career but you will need it as you grow. Simply put, to just learn and apply things, you don’t need mathematics but to understand the data on a deeper level you need to know mathematics. It will help you understand the relationship between the variables, whether they are linear or not, whether they will affect the outcome or not.

You need to be good in Statistics and Multivariate Calculus & Linear Algebra.

Mathematics allows you to understand what is happening with your data once you apply an algorithm. It can help you understand the working of the algorithm better and make the algorithm selection process easier.

Career Opportunities in Data Science

The world is generating 2.5 quintillion bytes of data everyday! This simply means that the need of data scientists is only going to increase in future.

Data Science Trends
Data Science Trends

There’s a requirement of Data Scientist in almost every industry.

According to Glassdoor, a Data Scientist can make anywhere around $113,000/Year in US or ₹985K/Year in India on an average which is still one of the highest paying career paths.

Data Scientist isn’t the only job role in the data science industry. Here are some of the most popular data science job titles along with their average salary (According to Indeed and Glassdoor).

Final Thoughts

Data Science is not only the most lucrative career but also an interesting one to go after. As a data scientist, you will be able to experiment with data and machine learning techniques. It is really a fun job which also requires critical thinking skills and a business mind. Yes, it requires you to have certain skills like statistics, calculus, machine learning, etc, that may seem like a daunting task but once you perfect them, it is one of the best career paths you can take.

I guess this is at least enough to help you decide whether data science is for you or not.

]]>
https://codinground.com/what-is-data-science/feed/ 2
Statements, Comments, and Python Blocks https://codinground.com/statements-in-python/ https://codinground.com/statements-in-python/#respond Fri, 12 Jun 2020 15:35:20 +0000 https://codinground.com/?p=690 Hey guys, this is the fourth tutorial of the Python for Beginners Series. If you haven’t checked out our previous tutorials, you should go and check them for a better understanding. In this tutorial, we are going to discuss the statements and escape sequences. This tutorial is focused on statements so you will learn about statements in general and then comments, what are comments, how to write comments in Python, docstrings, escape sequences, and the python blocks.

So, without further ado, let’s start now.

Statements

Any instruction that you give to python to execute is a statement. You say

print("Hello World")

This is a print statement because you are printing some data, which in this case is a string.
Anything you write is a statement. You assign a value to a variable, that’s a declaration/assignment statement. Now some of you might have heard about if, else, for, while, etc… These all are statements.

Now statements can be of single-line or multi-line. Till now, you may have only used single line statements like print(“Hello World”) but there are multi-line statements as well.

Multi-Line Statements

You can extend a single statement to multiple lines. Try making a multi-line statement like this one.

a = "Coding
Ground"
print(a)

It will throw an error like

    a = "Coding
              ^
SyntaxError: EOL while scanning string literal

To extend the statement to multiple lines, all you have to do is put a backslash ( \ ) at the end of the statement. For example,

a = "Coding\
 Ground"
print(a)

And the output will be

Coding Ground

You can also put your statement under brackets for some data types and you won’t need to use backslash but it is not true for strings. For example

a = ['item1',
'item2']
print(a)

The output will be

['item1', 'item2']

It will work with tuples as well.

Now you might be wondering why should we even use it when we can just write it in one line. Well, it is done to make your code cleaner and increase its readability.

Comments

You must have noticed that in some code there is a text written beside it which describes what the code is for. That text is called a comment. There are two types of comments, single-line comments which are normal comments and multi-line comments which are also known as docstrings

Single Line Comments

These are written in a single line for example,

a = "Coding Ground" #Assigns value to variable a 

Single Line comments start with a hashtag. They are written to describe what the code is about, it’s working what the variable stores and so on. Comments are only for user understanding and are never executed with the code.

You can write as many comments as you would like and it’s a good practice to write comments so that others can understand your code without much hassle. Not only for other people, but it will also help you in reading your code after a long time.

Docstrings or Multi-Line Comments

There are times when you have to add a full documentation about a function or a class you create or sometimes, it’s just that you have to write multi-line comments, for that we use docstring.

Docstring is any text written under triple quotes like ”’ or “””. For example,

"""This is a 
docstring"""
'''This 
is 
also a
docstring'''

If you want to add a documentation of a function you can do this like

def codinground():
    """prints Coding Ground"""
    return ("Coding Ground")

Docstring for functions, classes, or modules is written right after their declaration before any other statement.

Now if you want to access the documentation of a function/class/module, all you have to do is call the attribute __doc__.

You can, in this case, do this by printing function.__doc__. For example,

print(codinground.__doc__)

It will print out whatever is written in the docstring of that function or module or class. It will be really helpful whether you are experienced or beginner in python.

The output in this case will be

prints Coding Ground

So, docstrings will help you write documentations in an efficient and a standard way for everyone to follow.

Now, this isn’t a norm but a personal preference that you should write docstrings when you are writing a function or a class or module and single-line comments when you are writing loops or variables.

You might be confused about functions, classes, or modules but don’t worry, we’ll be covering them in our future tutorials in this series. But if you are confused about variables, I recommend checking out our tutorial on Datatypes and Variables in Python

Coming over to our next topic which is Escape Sequences.

Escape Sequences

There are some characters in python which have a different meaning. These are called escape sequences and are written after the escape character backslash( \ ).

For example, if I told you to print out a string whose output is “Coding Ground”. How would you do it? simply using print(“”Coding Ground””) will throw a Syntax Error.

There are many such cases where we want our text to be under quotes or in a new line without actually writing it in another line etc., in these cases, we use escape sequences.

To print a text under quotes just put a backslash before the quotes.

print("\"Coding Ground\"")

The output of this will be “Coding Ground”. This is because the double quotes are written after the escape character backslash. If you try to put backslash without any other character, it will leave whitespace. The backslash defines that the character after after the escape character has a different meaning than the normal one.

TIP
If it’s just printing the text under double or single quotes you can just do it like this print(‘”Coding Ground”‘). What I have done is, I started the string with a single quote and wrote the word under double quote so python interpreter knows that ok, the main string is whatever text is under single quotes. So, it treats double quotes as an ordinary string character.
But this is not recommended

You can also use \n to make it the next line. I know this is not clear so let’s consider this example, You are making a menu for your program and you want to list multiple items in it.
Then you can do this by using the \n like

print("1. Item One\n2. Item Two\n3. Item Three")

The output of this will be

1. Item One
2. Item Two
3. Item Three

You can see that it is much more efficient than writing it in multiple lines.

Similarly, there are many such escape sequences, you can find a list of them below.

Escape SequenceMeaning
\newlineBackslash and newline ignored
\\Backslash (\)
\'Single quote (')
\"Double quote (")
\aASCII Bell (BEL)
\bASCII Backspace (BS)
\fASCII Formfeed (FF)
\nASCII Linefeed (LF)
\rASCII Carriage Return (CR)
\tASCII Horizontal Tab (TAB)
\vASCII Vertical Tab (VT)
\oooCharacter with octal value ooo
\xhhCharacter with hex value hh
List of Python supported Escape Sequences

These are all the escape sequences available in Python. Play with them and you will have a better understanding.

Python Blocks

In our previous example on docstring, we made a function and then wrote a docstring.
If you have noticed carefully then you must be wondering why we put a colon after the function definition and then we leave two spaces in the statement we wrote under that function?

This is because unlike other languages, which use curly braces {} to define a block, python uses colon and indentation which is usually two or four spaces.

According to Python’s Official Documentation,

block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block.

All conditional statements, functions, classes and modules use a python block.

Let us look at a if statement to make it more clear.

 a = 10
 if a == 10:
    print("This code executes under if block")
print("This executes anyway")

Since the condition is true, which is a is equal to 10, if block will run and the output will be

This code executes under if block
This executes anyway

If we tweak the value of variable a, suppose 1, then the if condition will become false and if block will not run and the output will simply be

This executes anyway

So basically, a block is a piece of code that executes as a unit whenever called.

Hopefully it should have given you some basic insight on loops as well.

We will discuss python blocks in detail when we cover control flow statements.

This is it for this tutorial, hope you like it. Tell me what you think of this tutorial in the comments section below. Also, if you have any doubts, comment down below and I’ll help you.

References

]]>
https://codinground.com/statements-in-python/feed/ 0
DataTypes and Variables https://codinground.com/datatypes-variables/ https://codinground.com/datatypes-variables/#respond Fri, 05 Jun 2020 14:35:09 +0000 https://codinground.com/?p=630 Hey guys, this is the third tutorial of the Python for Beginners Series. If you haven’t checked out our previous tutorials, first go and read them (especially Tokens in Python) because we have discussed keywords and identifiers in those tutorials that you should know before learning about variables.

What are variables?

Variable is a name that points to a location where your data is stored. In simple language, the variable holds your data. You can assign a value to a variable using = sign with the variable on left and value on the right.
Consider this example,

age = 10

In this example, age is a variable that points to the memory location where the data i.e., 10 is stored. Now whenever you want to use 10, you can also use variable age.

For example, if you do this

print(age)

The output will be 10

Also, it is important to know that the data inside the variable can be changed whenever you need. For example, you could store a value 20 in the same variable.

age = 20

Now the variable age holds value 20. The old value 10 is now replaced by the new value 20.

You can store all sorts of data in a variable like

name = "Coding Ground"

the variable name holds the string value Coding Ground.

It is important to remember that variables do not store values they are the pointers. They give reference to the memory location where the data is stored.

Before we dive any deeper, let us discuss the different type of data supported in python.

DataTypes

A datatype is simply the attribute of data that tells the interpreter what type of data it is going to process.

There are multiple data types supported in Python. Also, you can check the type of data using the type() function.

Python Data Types Infographic
Python Data Types

Numeric Data

The numeric data type includes integers, floating-point numbers, and complex numbers.
They are used as int, float, and complex. Consider the examples below,

Integers

These are the whole numbers which also include negative numbers. For example, 0,1,2,3,150, -200 etc are all integers. They don’t have a fraction part.

a = 10
print(type(a))

It will return

<class 'int'>

Floating Point Numbers

Floating Point numbers have a decimal part in them.

b = 10.2
print(type(b))

The output will be

<class 'float'>

Complex Numbers

These numbers are in the form of a + bj. They are as <real part> + <imaginary part>j

c = -1 + 5j
print(type(c))

The Output will be

<class 'complex'>

Now you see class because the data is an object or instance of a class.
For example, 10 is an instance of int class.

If you don’t understand it then don’t panic, We will be discussing classes and objects in future lectures.

Sequence

Sequences include lists, tuples and strings.
Basically every data type in which slicing can be done falls under this category.

Lists

Python lists contains elements within square brackets [ ].

a = ["Coding Ground",1,1.2,True]
print(type(a))

The Output would be

<class 'list'>

In the above example, the variable a holds a list of 4 elements, each of different data type.

Now each element of list has an index value and it starts from 0.

For example, the index value of the element “Coding Ground” in the above code will be 0.

We will discuss indexing and slicing in future tutorials.

Tuples

Tuples contain elements in round brackets or parenthesis ( ).

An example of a tuple would be

a = ("Coding Ground",1,1.2,True)
print(type(a))

The output would be

<class 'tuple'>

Now you might be wondering, what is the difference between list and tuple?

The answer is that Lists are mutable while tuples are not. That means you can change the elements of a list without replacing the whole variable value but you can not do that with tuple. You can not perform operations on them.

String

Anything within single or double quotes in Python is a string.

a = "Coding Ground"
print(type(a))

The output would be

<class 'str'>

Boolean

Boolean on values can only be either True or False.

a = True
print(type(a))

The Output will be

<class 'bool'>

Remember that True is a keyword and T is capital.

Mapping

There are 2 types of Data Types that fall under this category. These are Dictionary and Sets.

Dictionary

Dictionary is an unordered collection of key-value pairs. It uses curly braces.
If you’ve ever worked with JSON data then you must be familiar with Dictionary.

a = {'name' : "Coding Ground"}
print(type(a))

The Output will be

<class 'dict'>

Dictionaries provide a little more readability as compared to lists and tuples. This is because of the key-value pair. And this is the reason they are used when we have to work with a large amount of data.

Sets

Like dictionaries, Sets are also unordered collections with curly braces. But the difference is that they contain only unique values.

a = {1,2,3,4,5,1,2,3}
print(a)
print(type(a))

The output would be

{1, 2, 3, 4, 5}
<class 'set'>

Here, you can clearly see that we got only unique values as the output.

We cannot filter data from sets as sets are unordered pair. We can however use the classic set functions like union, intersection etc on two set values.

Now that we have discussed the data types, lets continue where we left the variables.

Type Casting or Type Conversion

Now that you have learnt about the different data types and how to assign a value to a variable. It is time to learn how you can convert the data type of some data stored in a variable.

Consider this example,

a = 10
print(a)
print(type(a))
a = float(a)
print(a)
print(type(a))

The output will be

10
<class 'int'>
10.0
<class 'float'>

As you can see we have just converted an integer value to a float one. But remember that you can not do this with all data. You can only do this with data having similar data type.

Now this is all common sense, I don’t need to explain this. If you are thinking how about converting complex to int or float, it won’t work because complex got j in it which isn’t used in any int value or float value.

But you can convert string into a list.

a = "Coding Ground"
a = list(a)
print(a)
print(type(a))

The output will be

['C', 'o', 'd', 'i', 'n', 'g', ' ', 'G', 'r', 'o', 'u', 'n', 'd']
<class 'list'>

So, guys, this is it for today. I hope you like it. If you have any problems or if anything is left unclear then tell us in the comment section below.

Happy Coding!

References

You can read more about additional data types at official python documentation

]]>
https://codinground.com/datatypes-variables/feed/ 0
Challenge: #30DaysofCode https://codinground.com/30-days-of-code/ https://codinground.com/30-days-of-code/#respond Mon, 01 Jun 2020 08:38:55 +0000 https://codinground.com/?p=600 Introduction

Hey Guys, We are kicking off a #30DaysofCode Challenge starting today.
In this, we will code every day for 30 days i.e., basically a whole month for creating something.

This will be the epic time especially for those who had a project in mind but weren’t able to start it. You can start now along with 100s of new people. We also have a support group on Facebook.

You will submit the project here on our site so people can vote for the projects. You can also ask questions here.

Theme: Your project can be a game, a resource to help society, or to raise awareness or something entertaining or maybe an AI.

The main aim of this challenge is to push yourself to create something new, to expand your horizon so that you can learn new skills.

Rules

  • You can participate either solo or in group.
  • You cannot copy each other’s work.
  • You cannot copy project from google.
  • You will not use any abusive language towards any other member.
  • If you don’t want to answer a question simply because it is too easy, then just leave it and don’t call the other person dumb.

Ok Cool, So what do I have to do?

You will select a project theme and you will write code everyday starting from today itself.

If you had a project in mind, then it’s cool, you can start it now but remember that you have to implement theme in any way you can. You have 1 month, do whatever you can to do but remember, you have to code every day. However, if you do not have anything in mind but want to create something, there’s a recommended project part at the end of this post.

You will be sharing what you have done throughout the day on your project by the end of the day. You can also see what others have done so that you can get new ideas to implement in your project.

I’m stuck, I don’t know what to do!

If you are facing any problems you can post them on facebook group or you can also ask a question here on this site. Just log in or register, if you haven’t already. Then click on create a post.

If you have a question, select category as question and proceed.

Someone would definitely help you out.

I want to submit my project for voting!

Same way, go over to create post and choose category as 30 Days of Code.

  • Make a game like Hangman, Tic Tac Toe.
  • Develop an AI (Tensorflow/wit.ai/keras etc).
  • Make a service program to raise awareness.

Tutorials and References

The recommended language is Python, but you can use any language you want.

If you are using Python you can look over at our tutorials or repl.it tutorials:

We know designing a game in a month is no easy feat. Here are a few tutorials to help get you started:

More will be added soon.

]]>
https://codinground.com/30-days-of-code/feed/ 0
Python Pandas: Overview of DataFrames and Series https://codinground.com/pandas-introduction/ https://codinground.com/pandas-introduction/#comments Sat, 30 May 2020 15:42:31 +0000 https://codinground.com/?p=510 Introduction

Hey Guys, this article will give you a quick introduction of pandas as to what is Pandas, why you should use Pandas, what can you do with Pandas, and the supported Pandas data types. It can help you make sure if Pandas is what you are looking for or you should learn pandas or not.

If you’re looking for data science then you must have heard of this library at least once. Pandas is the most famous/downloaded library for data science.
Why? Because it is super fast and makes working with datasets so much easier. We’re gonna talk about everything from series to DataFrames, from creating a new DataFrame to reading an old dataset everything.

Prerequisites

You should have a basic understanding of Python especially dictionaries, lists, and tuples. Some basic knowledge of NumPy will also be helpful as arrays are often used in Pandas Series and DataFrames along with the dictionaries.
If you want to learn NumPy then check out our amazing tutorial on NumPy Arrays which covers everything that you need to know about NumPy.

What is Pandas?

Pandas is a high-performance open-source library for data analysis in Python developed by Wes McKinney in 2008. Over the years, it has become the de-facto standard library for data analysis using Python.

Why Pandas?

The benefits of pandas over using the languages such as C/C++ or Java for data analysis are manifold:

  • Data representation : It can easily represent data in a form naturally suited for data analysis via its DataFrame and Series data structures in a concise manner. Doing the equivalent in C/C++ or Java would require many lines of custom code, as these languages were not built for data analysis but rather networking and kernel development.
  • Data sub-setting and filtering : It provides for easy sub-setting and filtering of data, procedures that are a staple of doing data analysis.

Features of Pandas

  1. It can process a variety of data sets in different formats: time series, tabular heterogeneous arrays, and matrix data.
  2. It facilitates loading and importing data from varied sources such as CSV and DB/SQL.
  3. It can handle a myriad of operations on data sets: sub-setting, slicing, filtering, merging, groupBy, re-ordering, and re-shaping.
  4. It can deal with missing data according to rules defined by the user and developer.
  5. It can be used for parsing and managing (conversion) of data as well as modeling and statistical analysis.
  6. It integrates well with other Python libraries such as stats models, SciPy, and Scikit-learn.
  7. It delivers fast performance and can be speeded up even more by making use of Cython (C extensions to Python).

Setting Up

Now, I’m not a big fan of Jupyter Notebook but it makes the data science easier to understand because you know exactly which block is executing which code.

Since most people find it difficult to use Jupyter Notebook standalone without Anaconda, We’ll stick to our old favorite – Default IDLE

Installing Pandas and JupyterLab

Now for those who do want to use Jupyter Notebook, if you have anaconda installed, it’s fine to skip the whole setting up section because Pandas comes pre-installed with Anaconda. If you don’t have Anaconda, continue reading

Open your pip and install Jupyter lab as:

pip install jupyterlab

This is it, it will install Jupyter Notebook in your system. Also, you may also be aware that there’s a jupyter library too. We aren’t going to install that because it is no longer updated and this just gets the job done fairly well.

Check out this video. It might help you set up Jupyter Lab

Now, I won’t be explaining how to use Jupyter Notebook here in this tutorial because this is out of the scope of this tutorial. So, we’ll just get to the point. Open your terminal, cd to the path where you want to access files using Jupyter, and open Jupyter Notebook there. I will make a video on that in future tutorials but this article is about Pandas so we’re gonna skip that.

Now all you have to do is install Pandas. It is fairly easy to do so. Just open pip and type

pip install pandas

This will install pandas in your computer.

Syntax

The general convention is that you import pandas with an alias name pd. It is not necessary that you import it with this name but it is the recommended way to do it and you’ll find it this way in most of the places. So, it will improve readability of your code.

import pandas as pd

Pandas Data Structures

Pandas supports two main type of Data Structures. Series and DataFrames!

Series

Pandas Series is the one-dimensional labeled array just like the NumPy Arrays. The difference between these two is that Series is mutable and supports heterogeneous data. So Series is used when you have to create an array with multiple data types. Imagine a table, the columns in that table are Series and the table is a DataFrame.

Take a look at the image below. It will help you visualize better.

Series Example

Creating Series

Syntax for creating Pandas Series is:

import pandas as pd
s = pd.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)

NOTE: ‘S’ of the pd.Series is capital. People tend to forget that.

This syntax may seem a little overwhelming but you do not need to focus on all the parameters. Most of the time you will only be using the data and index parameters but we will be discussing all the parameters here. But let’s first create example Series here.

import pandas as pd
s = pd.Series(["Coding Ground",1,5.8,True])
print(s)

Output will be:

0    Coding Ground
1                1
2              5.8
3             True
dtype: object

Now that we have a Series of consisting data of multiple datatypes, we can proceed further.

data: Sequence, most preferably list but can also be dictionary, tuple, or an array.
It contains data stored in Series.

index: This is optional. By default, it takes values from 0 to n but you can define your own index values. Now there are two ways to define index,

 s = pd.Series(["Coding Ground",1,5.8,True],index=["String","Integer","Float","Boolean"])

The output will be

String     Coding Ground
Integer                1
Float                5.8
Boolean             True
dtype: object

You can also set index using .index method as

s.index = [1,2,3,4]

And it will work same as defining index in parameters. Output will be

1    Coding Ground
2                1
3              5.8
4             True
dtype: object

dtype: It is the datatype of the Series. If not defined, it will take values from the series itself. If it’s the same for all element, it will show a specific data type such as int else it will show Object.

name: It is the name given to your pandas series

 s = pd.Series(["Coding Ground",1,5.8,True],index=["String","Integer","Float","Boolean"],name="Pandas Series")

It will add a name “Pandas Series” to your Series. The output will be

String     Coding Ground
Integer                1
Float                5.8
Boolean             True
Name: Pandas Series, dtype: object

You can also do the same using s.name = “Pandas Series”

copy: It creates a copy of the same data in variable(s). By default it is set to False i.e., if you change the data in one variable, it’ll change in all variables wherever the data is stored. Change it to True and all the locations where the data is store will be independent of each other. For example,

s = pd.Series(["Coding Ground",1,5.8,True],index=["String","Integer","Float","Boolean"],name="Pandas Series")
ss = s
ss[1] = 2
print(ss)
print(s)

The output would be:

#SS
String     Coding Ground
Integer                2
Float                5.8
Boolean             True
Name: Pandas Series, dtype: object

#S
String     Coding Ground
Integer                2
Float                5.8
Boolean             True
Name: Pandas Series, dtype: object

You can clearly see that even though you changed the value of the second element in ss, it automatically got changed in s. This is because copy by default is False. Set it to True

ss = s.copy()
ss[1] = 3
print(ss)
print(s)

This will create a new copy in ss and they will not share same data location point anymore.
So, changing one will not change another.

#SS
String     Coding Ground
Integer                3
Float                5.8
Boolean             True
Name: Pandas Series, dtype: object

#S
String     Coding Ground
Integer                2
Float                5.8
Boolean             True
Name: Pandas Series, dtype: object

fastpath: Fastpath is an internal parameter. It cannot be modified. It is not described in pandas documentation so you may have to take a look here.

DataFrames

DataFrame is the most commonly used data structure in pandas. DataFrame is a two-dimensional labeled array i.e., Its column types can be heterogeneous i.e. of varying types. It is similar to structured arrays in NumPy with mutability added.
It has the following properties:

  • Similar to a NumPy ndarray but not a subclass of np.ndarray.
  • Columns can be of heterogeneous types e.g char, float, int, bool, and so on.
  • A DataFrame column is a Series structure.
  • It can be thought of as a dictionary of Series structures where both the columns and the rows are indexed, denoted as ‘index’ in the case of rows and ‘columns’ in the case of columns.
  • It is size mutable that means columns can be inserted and deleted

Creating DataFrames

Syntax for creating Pandas DataFrames is:

pandas.DataFrame(data=None, index: Optional[Collection] = None, columns: Optional[Collection] = None, dtype: Union[str, numpy.dtype, ExtensionDtype, None] = None, copy: bool = False)

data: You can input several types of data as below:

  • Dictionary of 1D ndarrays, lists, dictionaries, or Series structures.
  • 2D NumPy array
  • Structured or record ndarray
  • Series structures
  • Another DataFrame structure

Now for example, you can create a dataframe like this:

import pandas as pd
ls = ["item 1","item 2","item 3","item 4"]
df = pd.DataFrame(ls)
print(df)

And the output will be

        0
0  item 1
1  item 2
2  item 3
3  item 4

But yeah, there’s no point in creating a DataFrame like that. You could just create a series for that. Now moving onto a full tabular dataframe.

Now for the sake of simplicity, we’re going to use this data.

import pandas as pd
data = {"Student":["Student1","Student2","Student3"],"Age":[18,19,20]}
df = pd.DataFrame(data)
print(df)

The output of this code would be

    Student  Age
0  Student1   18
1  Student2   19
2  Student3   20

See how keys of dictionary became the columns.

index: index in DataFrame is rows.

df.index = ["row1","row2","row3"]

This will result in

       Student  Age
row1  Student1   18
row2  Student2   19
row3  Student3   20

columns: It is the same as keys in a dictionary.

Suppose now you want to change the columns from “student” to “name of student” and from “Age” to “Age of Student”. You could do it easily as:

df.columns = ["Name of Student","Age of Student"]

And the output will be

           Name of Student  Age of Student
row1        Student1              18
row2        Student2              19
row3        Student3              20

So, this is it, we have covered all the parameters of DataFrames along with an example.

dtype: The datatype of Data in the DataFrame

copy: Same usage as Series, allows different location of same data.

We aren’t going to discuss the copy parameter here because that would require a lot of examples and new functions which are out of scope of this tutorial. We will be discussing these things in details in the future lessons.

Conclusion

Congratulations on completing this tutorial. It was your first step in deciding whether you should learn Pandas or not and since you’re reading this, you made the right choice.

Personally, I recommend learning Pandas because why not? It is the most powerful library and if data science is what you’re aiming for then you’ll see this library a lot.

You should set up Jupyter Notebook or preferably Anaconda. Use IDLE only if you can’t have Jupyter Notebook or Anaconda. IDLE will be a little bit slower when processing data compared to Anaconda but that’s fine. I mean you don’t necessarily need it but it will help you understand better. Also one more thing, remember that S of pd.Series and D of pd.DataFrame is in caps. People make that mistake a lot and their program fails.

So, now I want to ask you, can you make a DataFrame using Series?
If yes, how?
Comment down the answer below!

So, guys, this is it for this tutorial, we’ll be looking at more advanced topics in the future.

Happy Coding!

]]>
https://codinground.com/pandas-introduction/feed/ 2
Tokens in Python https://codinground.com/tokens-python/ https://codinground.com/tokens-python/#comments Mon, 25 May 2020 13:35:53 +0000 https://codinground.com/?p=484 Hey guys, this is the second tutorial of the Python for Beginners Series. If you haven’t read the first one i.e, Getting Started with Python yet then I suggest checking that out first. Also, this is going to be mostly theoretical with almost no coding involved but is very important.
Do not skip this if you don’t know about tokens in Python.

The Python Character Set

It is the set of valid characters that python understands. Python uses the Unicode character set.
It includes numbers from numbers like
Digits: 0-9,
Letters: a-z, A-Z
Operators: +,-,/,*,//,**
Punctuators like : (colon), (), {}, []
Whitespaces like space, tabs, etc

What are tokens?

Tokens are building blocks of a language. They are the smallest individual unit of a program. There are five types of tokens in Python and we are going to discuss them one by one.

Types of Tokens

So the five types of tokens supported in Python are Keywords, Identifiers, Literals, Punctuators, and Operators. Coming over to the first one we have

Keywords

Keywords are the pre-defined set of words in a language that perform their specific function. You cannot assign a new value or task to them other than the pre-defined one.

You cannot use them as a variable, class, function, object or any other identifier.

For example: if, elif, while, True, False, None, break etc

These have their special task that you cannot change. For example break will only end the loop you cannot make it start the loop. (We’ll be covering loops in future lectures).

Identifiers

Now identifiers are the names that you can assign a value to. An identifier can be anything for example,

a = 10

Here, a is a valid identifier name. Any name you give your variable, function, or class is an identifier of that particular thing. Now there are certain rules that you have to follow to define a valid identifier name.

Rules for valid identifier name

  • A valid identifier name can have letters, digits, and underscore sign.
  • It can start with an alphabet or underscore but can never start with a digit.
  • It can never be a keyword name.
  • An identifier name can be of variable length.
  • The only special symbol that can be used in identifier name is underscore( _ ).

One more thing that you should remember that python is case sensitive i.e.,

a = 10
A = 5

These two hold two different values. a holds the value 10 and A holds the value 5.

Examples of valid identifier names include: a, _a, a12, etc
Examples of invalid identifier names include: 1a, $a, elif, print.
If you don’t understand why they are valid/invalid, read the rules again.

Literals

Literals are the fixed or constant values. They can either be string, numeric or boolean.

For example, anything within single or double quotes is classified as a string and is literal because it is a fixed value i.e, “Coding Ground” is literal because it is a string.

Another example is, 10. It is a simple number but is a fixed value. It is constant and it will remain constant. You can perform operations like addition or subtraction but the value of these two characters 1 and 0 put together in a correct order gives them a value equal to ten and that cannot be changed.

Boolean only consist of 2 values, True and False. Remember that “T” of True is capital. Python is case sensitive and if you write True with small “t” like true it will hold a different meaning. It will act as another variable.

It might seem a bit confusing for now but you’ll definitely understand it in our future lectures on boolean and other data types.

Punctuators or Separators

Punctuators, also known as separators give a structure to code. They are [mostly] used to define blocks in a program. We will be covering code blocks in control flow statements when we discuss how to apply conditions in Python.
Some examples of punctuators include
single quotes – ‘ ‘ , double quote – ” ” , parenthesis – ( ), brackets – [ ], Braces – { },
colon – ( : ) , comma ( , ), etc.
Punctuators and operators go hand in hand are used everywhere. For example,

name = "coding ground"

Here, an assignment operator ( = ) and punctuator, (” “) is used.

And now for the last type of token is Operators

Operators

Operators are the symbols which are used to perform operations between operands.

Unary Operators: Operators having single operand.
Eg. +8, -7, etc
Binary Operators: Operators working on 2 operands.
Eg. 2+2, 4-3, 8*9, etc
Similarly, there are Ternary Operators that work on 3 operands and so on.
These are just basics and not so important to know but the operators listed below are very important

  • Arithmetic operators ( +, -, /, * etc)
  • Assignment operators ( = )
  • Comparison operators ( >, <, >=, <=, ==, !=)
  • Logical operators ( and, or, not)
  • Identity operators ( is, is not)
  • Membership operators ( in, not in)
  • Bitwise operators ( &, |, ^ etc)

There’s so much about operators that it cannot be written here as it will be out of the scope of this topic. A new detailed article on tokens will be there very soon.

Reference to Punctuators:
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzarg/punctuators.htm
https://download.mikroe.com/documents/compilers/mikroc/pic/help/punctuators.htm

]]>
https://codinground.com/tokens-python/feed/ 5
Getting Started with Python https://codinground.com/intro-python/ https://codinground.com/intro-python/#comments Fri, 22 May 2020 11:58:34 +0000 https://codinground.com/?p=405 Hey Guys, if you ever wanted to learn python but you don’t have any past experience then continue reading. This is the first tutorial of Python for Beginners series. In this tutorial we will cover the introduction of python following with its uses and applications. You will install and set up python on your computer and write your first program in Python.

Introduction to Python

Python is an interpreted*, high-level, object-oriented, and open-source language developed by  Guido van Rossum and first released in 1991. According to a survey conducted by Stackoverflow, Python is the most sought after programming language by developers.

Compiler – It converts the source code into a machine-dependent code completely all at once.
Interpreter – Python runs and checks the code line by line. So if an error is detected at suppose line 2, then it won’t run all the way to the end of the script. Unlike Java, which compiles the whole code at once.

Why Python?

  1. Python is an interpreted language, not a compiled language. Due to this, python is easy to debug.
  2. Python is a cross-platform language, it can run on a variety of platforms.
  3. Python is open-source and free.
  4. Python is best for Artificial Intelligence and Machine Learning.

Setting Up

Installing Python is extremely easy, it’s the same as you install any other software. Just go over to the official Python Website and download the latest version or click here.

installing python

Remember to check “Add Python 3.X to PATH“. It adds PIP to environment path variables.

PIP is a Python Package installer that helps in installing libraries and modules. You will need it one day or another, so it’s better to install it now.

Selecting the Python IDE(Integrated Development Environment)

Different people suggest different IDE, it all comes down to the personal preferences.

In this series we will be using the default python idle because a large number of people use that and it is generally advised to start with that.

What I recommend is IDLE or Sublime Text. These are more than enough or you can try VS Code. Though there are many other good IDE’s but I believe an IDE should be lightweight and fast because oftentimes you have to use a web browser to search for the problems. So these 3 are the best in terms of speed and efficiency.

Out of these I recommend Sublime Text because it has a large number of color schemes which look fantastic while coding and some people prefer a darker theme which is really great in Sublime.

Working with IDLE

Writing Your First Program

After installation open up Python Shell and write this code.

print("Hello World")

print() function tells Python to print/echo whatever data is in the parenthesis.

In python, string values are written under double quotes. We will be learning more about that in the future lessons.

Hello World

Congratulations, you wrote your first program in python!

As an assignment, Try printing your name in python.

Working with Files

Most beginners make this mistake of continue writing their whole program in Python Shell. You shouldn’t do it unless you want to perform a quick task.

Now in your Python Shell click on File and select New File

Creating new python file

Now type the same code in the file and save it. The default extension of python files is .py.

After Saving the file press F5 or click on Run and select Run Module.

Working with files in Python

It should look something like this. If this is what you got then congrats, you successfully learned how to use python files.

]]>
https://codinground.com/intro-python/feed/ 1