Site icon Console Flare Blog

Python: All you need to know about it

Python

Python is super easy for someone who knows the basic concepts of any programming language. This blog will cover everything you need to know to kickstart your learning in python.

Basics Of Python

Data Types

You must know some important Data Types that we use in Python. It is not so different from any other language.

Numeric Data Types

Integer (int) –

1,2,3,4,56,78,0,98

Float (float) –

1.1 , 2.5, 4.7,0.001

String Data Type :

String (str)

'Console flare' , "@#$#@#" , """123""" , '''23.45'''

Sequence Types:

Lists (list)

[1,2,3,4,5,6] , [1,2,3,'console flare']

Tuples (tuple)

(1,2,3,4,5,6) , (1,2,3,4,'Console flare')

Dictionary (dict)

{'name' : 'abhi' , 'Company' : 'Console flare'}

Range (range)

range(1,100)

Set Data Types :

Sets (set)

{1,2,3,'consoleflare'}}

Boolean Data Type :

Boolean (bool)

True , False

Variables

In Python, the moment we store any information of value in a variable, the variable is declared. We can use variables in the program once we declare them or store a value in them.

age = 24  #integer Variable age 
name = 'abhishek'  #string variable  name
weight = 67.5    #float variable weight
isprogrammer = True   #boolean variable isprogrammer
details = ['abhishek',24,67.5,True]  #list variable details

Naming Convention in Variable:

1age = 20
Output : 
 File "<ipython-input-3-ef3a9e7a157c>", line 1
    1age = 20
     ^
SyntaxError: invalid syntax
my_age = 24

How to print anything in python

We use print function to display output on the console.

print('Python is the most searched language')
Output : 'Python is the most searched language'

How to take input from user

We use input() function to get input from user.

name = input('what is Your name ? ')

Comments in Python

Comments in Python starts with #(Hash) and ”’ (triple quotes).

#This is a Single Line Comment

'''This
is a multiline
Comment'''

Lists In Python

A list can be defined as a collection of values or items of different Data types.

In more simpler terms, List is a collection of items enclosed between Square brackets []. Unlike other data types like Integer , Float and boolean, List stores multiple values in a single variable.

details = ['abhishek','data scientist']

Empty List

Empty list is created by square brackets only.

empty_list = []

How to add an item in a list

We use append function to add an item in a list.

details = ['abhishek']
details.append('data scientist')
print(details)
Output : ['abhishek,'data scientist']

How To remove an item in a list

We use remove method to remove an item in a list.

details = ['console','flare']
details.remove('flare')
print(details
Output : ['console']

Strings in Python

Strings are a collection or sequence of characters, enclosed within single quotes (”) or double quotes (“”) or triple quotes (”””) or (“”””””).

sentence = 'Python is a programming language'

Some Useful String Methods In Python

We will talk about some useful string methods in python in this blog. For more String methods in Python, go here.

lower() Method:

lower() method is used to return a string with all its character in lowercase. We do not pass any parameters in lower().

name = 'CoNsOle'
print(name.lower())
Output : console

upper() Method:

upper() method is used to return a string with all its characters in the Capital case. We do not pass any parameters in upper().

name = 'Console'
print(name.upper())
Output : CONSOLE

replace() Method:

replace() method returns a string where a specified value is replaced with a specified value.

Parameters to be passed in replace:

old value: The string to be replaced. It is Required

new value: The string to replace the old value with. It is Required

count: An integer value specifying how many occurrences of the old string you want to replace. The default value of the count is All Occurences. It is Optional.

replace(oldvalue,newvalue,count)

sentence = 'This is C Programming'
sentence = sentence.replace('C','Python')
print(sentence)
Output : 'This is Python Programming'

find() Method:

find() method searches the string for a specified value and returns the starting position of where it is found. It searches the whole string for specified values from left to right.

In simpler terms, it finds the string’s position.

Parameters to be passed inside find() method:

value: The values to search for. It is Required.

start: Starting index, from where to look for the value. It is Optional

end: Ending index, from where to stop the search for value. It is Optional

find(value,start,end)

sentence = "This is Python"
pos = sentence.find("Python")
print(pos)
Output : 8

Tuples In Python

Tuple is similar to a list with some differences. You can make changes in the list but not in Tuples. It is enclosed within parentheses ().

details = ("Data","Science")

Tuple Methods In Python

count Method

count method is used to count the occurrence of elements in the tuple.

thistuple = (1,2,3,4,2,2)
x = thistuple.count(2)
print(x)
Output : 3

index Method

index method is used to find position of a particular element in tuple.

thistuple = (1,2,3,4,2,2)
x = thistuple.find(1)
print(x)
Output : 0

Dictionary in Python

Python Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. It is the mutable data-structure. The dictionary is defined into element Keys and values. Keys must be an immutable element.Value can be any type such as list, tuple, integer, etc.

In other words, we can say that a dictionary is the collection of key-value pairs where the value can be any Python object. In contrast, the keys are the immutable Python object, i.e., Numbers, string, or tuple.

A Dictionary is enclosed under curly braces {}. For Example:

employee = {"name" : "Abhi",
            "role" : "Data Scientist",
}

print(employee)
Output : {'name': 'Abhi', 'role': 'Trainer'}

Empty Dictionary in Python

We use empty curly braces {} to create an empty dictionary.

empty_dict = {}

How to add an item in Dictionary

employee = {"name" : "Abhi",
            "role" : "Data Scientist",
}

employee["company"] = 'ConsoleFlare'   #This is how you add items in dictionary
print(employee)
Output : {'name': 'Abhi', 'role': 'Trainer','company':'ConsoleFlare'}


How to remove an item in Dictionary

employee = {"name" : "Abhi",
            "role" : "Data Scientist",
}

employee.pop('role')   #This is how you remove items in dictionary
print(employee)
Output : {'name':'Abhi'}

Sets in Python

A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.

varset = {"Console","Python","data","Flare"}
print(varset)
Output : {'data', 'Python', 'Console', 'Flare'}

Adding an item or a single element in a set

add Method

var_set = {1,2,3,4,5,6,'console',7,8,9}
var_set.add("flare")
print(var_set)
Output : {1,2,3,4,5,6,'console','flare',7,8,9}

Removing an item in a set

remove Method

set_a = {1,2,3,4,5}
set_a.remove(5)
print(set_a)
Output : {1,2,3,4}

Functions In Python

Function is one of the most important aspects of the Python Program. The function is a set of code that does a particular task.

The Function helps to programmer to break the program into smaller part. It organizes the code very effectively and avoids the repetition of the code. As the program grows, function makes the program more organized.

Imagine a Program where we need to do addition more than 100 times. To do addition 100 time we will have to write same code 100 times.

The function can be of huge help here. Once we make a function of addition, we will call it every time we need to add.

Creating / Defining a Function

#Defining a Function
def myfunc():
    print("hello world")

Calling a Function

myfunc()

Here I have tried to cover everything you must know when learning python. But a single blog is not able to cover all the intricacies this programming language. To become a master of this language, you can opt for Python For Data Analytics Course, where you will learn not only python but also how to implement it in real-life situations, This Course is designed for you to become a master in Python and Data Analysis to secure a great job.

a a a a a a a a

a a a a a a a a a a a

a a a a a a a a a a a

a a a a a a a a a a a

a a a a a a a a a

a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a

a a a a

a a a a a a a a a

a

a a a a a a a a

a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a

a a a a a a a a a a a a a

a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a

a a a a a a a a a

a a a a a a a a a a a

a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a

a a a a a a a a a a a a a a a a

a a a a a a a a a a a

a a a a a a a a a a a

a a a a a a a a a a a a

a a a a a a a

Exit mobile version