Posts

Showing posts from January, 2022

Python Statements

Indentation: Indentation is very import while using python statements. It is important to keep a good understanding of how indentation works in Python to keep you code structure and in proper order to execute the code. Example : Simple print statement with if condition: if True:    print('This is a True statement') This is a True statement Try printing with Logic: if, else Statements Syntax: if case:     perform action1 else:     perform action2 Example: name='Feroz' if name == 'Feroz':     print('Hello Feroz!') else:     print('Welcome to the Group!') Hello Feroz! if, elif, else Statements Syntax: if case1:     perform action1 if case2:     perform action2 else:     perform action3 Example: sports='cricket' if sports == 'cricket':    print('Welcome to Cricket Club!') elif sports == 'Foot Ball':   print('Sorry! it is currently not available') else:    print('This game not present in ...

Comparison Operators in Python

Comparison Operators: Equals                                    ==   Dees not equal                     != Less than                              < Greater than                          > Less than or equals           <= Greater than or equals      >= Examples: 1==1 True 1!=1 False 1<2 True 1>2 False 1<=1 True 1>=1 True Logical Operators to combine the comparisons: and : Both condition should be true to TRUE or  not : Either of the condition to be true to TRUE Examples: 1==1 and 2==2 True 1==2 and 2==2 False 1==1 or 2==2 True 1==2 or 2==2 True

Files in Python

 Files? Python uses file objects to interact with external files which are available on you location computer and the files can be any sort of file like, Text file, audio file, excel file, and even Emails. Creating a File: If you are using Windows create a text file based on the location (if you are using command line method). However, if you are using Jupyter notebook you could use below command to create a text file. %%writefile firstfile.txt  This is my first text file to run the python commands. I will execute this file to test the result. It is the end of the text file. Open a File: >>> mytextfile=open('first_file.txt') Note: If your file is not at a default location so you will get the below error (if file doesn't exists)  >>> mytextfile=open('first_file1.txt') Traceback (most recent call last):   File "<stdin>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: 'first_file1.txt' The abo...

Boolean in Python

What is a Boolean? Ture or False (Make sure to use initial letter as a capital) For Example, >>> True True >>> true Traceback (most recent call last):   File "<stdin>", line 1, in <module> NameError: name 'true' is not defined Examples: >>> x= True >>> type(x) <class 'bool'> >>> 9 > 8 True >>> 8> 9 False  How to define/assign a variable without value? >>> x Traceback (most recent call last):   File "<stdin>", line 1, in <module> NameError: name 'x' is not defined Here is the way, you could define the variable without defining/assigning the value >>> x=None >>> x

Sets in Python

 What is Sets? Sets are an unordered collection of unique elements and created by using set() function.  -- Creating a Set my_set=set() -- Adding an element to set using add() method >>> myset=set() >>> myset.add(5) >>> myset {5} --- Lets try to add additional values to set >>> myset.add(6) >>> myset.add(7) >>> myset {5, 6, 7} --- Lets try to add duplicate values into the set  >>> myset {5, 6, 7} >>> myset.add(7)   - ----------- adding the same item "7" which is already present in the Set >>> myset {5, 6, 7}        -------------------- It did not add the items "7" twice but it just maintain  uniqueness   --- Let try to get the unique values from the set >>> myset=[9,9,9,8,8,8,7,7,7] >>> set(myset) {8, 9, 7} >>> set('Administration') {'a', 'd', 'A', 'r', 's', 'o', 'm', 't', 'n', ...

Tuples in Python

 Tuples Basically Tuples in Python are very similar to Lists, but, Tuples are immutable which means that they can not be modified/changed. We generally use Tuples to make sure the values are not changed like, dates, Year, and days of the week or dates on a Calendar.  Creating a Tuple: >>> my_t1=(1,2,3) >>> my_t2=('one','two','three') >>> my_t1 (1, 2, 3) >>> my_t2 ('one', 'two', 'three') -- check the length of tupple: >>> len(my_t1) 3 >>> len(my_t2) 3 -- Tuples can have mixed objects as like Lists. >>> my_t3=(1,'two',3,'four') >>> my_t3 (1, 'two', 3, 'four') -- indexing with tuples  >>> my_t3=(1,'two',3,'four') >>> my_t3[2] 3 Immutable Tuples We can not modify the value. Here is the example: >>> my_t3=(1,'two',3,'four') >>> my_t3[2]=4 Traceback (most recent call last):  ...

Dictionaries in Python

 What is Dictionary Creating a Dictionary dict1={'fruits' : 'Apple', 'vegetable' : 'tomato'} Example-1: >>> dict1={'Fruits' : 'Apple', 'Vegetables' : 'Tomato'} >>> dict1['Fruits'] 'Apple' >>> dict1['Vegetables'] 'Tomato' Note: Dictionaries are very flexible, that can hold any type of data types like integer, string, float, and etc. Example-2: Collection of different data types >>> SupperMarket={'Apple':2.50, 'Milk':1, 'Sugar':'Not Available', 'Stationary':['Pen','Pencil','Note Book']} >>> SupperMarket['Milk'] 1 >>> SupperMarket['Stationary'] ['Pen', 'Pencil', 'Note Book'] >>> SupperMarket['Sugar'] 'Not Available' -- Call index on values on Dictionary >>> SupperMarket={'Apple':2.50, 'Milk...

Jupyter Notebook Basics

How to launch the Jupyter notebook from any drive For example from D drive, use the below command: Open command prompt and run it cmd jupyter notebook --notebook-dir=D:/ Create empty text file %%wrtiefile first_file.txt     ------- create empty text file %%writefile first_file.txt This is my first text file to run the python commands. I will execute this file to test the result. It is the end of the text file. Check the default working directory: pwd

Lists in Python

  What is List? List are used to store multiple items in a single variable, which can be written as a list of commas separated values using square brackets "[]". Unlike Strings, Lists are mutable that means elements inside the list can be changes/updated/modified.  How to create list? first_list=[4,5,6] second_list=[4,'five',6]   ---- list can hold different types of objects, here we have used integer & string  Example: >>> first_list=[4,5,6] >>> first_list [4, 5, 6] >>> second_list=[4,'five',6] >>> second_list [4, 'five', 6] Can we use indexing and Slicing? Yes, Indexing and Slicing work just like in String! Create a list: third_list=[4,'five',6,'seven','eight',9] Example: >>> third_list=[4,'five',6,'seven','eight',9] >>> third_list[1]    ------------- Indexing method 'five' >>> third_list[3:]  ------------ Indexing and Slicing me...

Math in Python (Operators)

 Python Operators: Addition Subtraction Multiplication Division Floor Division Modulo Negation Absolute Value Exponent Equals Does not equal Greater than Less than Greater than or equals Less than or equals  

Python Basics

 Types of variables in Python: There are different types of variables used in Python. Integer: It is known as numbers Float: It is known as decimal numbers Long String:  List or Array Dictionary Examples:

Python Variables

Variable Assignments: Rules: Names can't star with a number, can't contain spaces (you could use _ instead), and any of these below special characters: :'",<>/?|\!@#%^&*~-+ It's a best practice to use lower case Don't use Python built-in keywords like str, list, and set  Assigning values: you assignment operator "equal sign = "  Syntax: name =object Example: a = 10 Determining variable type with type() Using python built-in type() function you can check what type of object is assigned to a given variable. List of data types: int, float, list, str, tuple, set, dict, bool  Examples: >>> type(a) <class 'int'> >>> b=12.5 >>> type(b) <class 'float'> >>> c=(1,2) >>> type(c) <class 'tuple'> >>> d=[1,2] >>> type(d) <class 'list'> >>> e='feroz' >>> type(e) <class 'str'> >>> f={1,2} ...