Basics of Python


Python Tutorial 0.0


Here we are with Python Tutotials ... We struggled to learn it and now we are okayish enough to write about it and help you guys learn . Starting the series of Python Tutorials, the introductory article in the series covers the basics that are "Must know" to go ahead in the journey.


So don't get scared of this, it's absolutely harmless
 !


Introduction to Spyder:







Let's now start learning the most basic coding of python



But before that few must know things about python:

1. Python is case sensitive in all respects. Here “a” and “A” are different objects.

2. Any statement starting with # is considered as non-executable comment, but it can not be of multiple line. For multiple line comment, use # in start of every line.

3. Ctrl + Enter command is used to execute a code in Script Editor window.

Assigning Values to Variables

In python assigning operator is simply equal sign(" = ").
A=10                         #  an integer assignment

Name="Hero"              # a string assignment

No need of explicit declaration; The declaration happens automatically when you assign a value to a variable.


When a variable is assigned, it references to an object





# Want to know the value of the variable, simply write

A               #and run execute command

or
print(A)      #and run execute command

# Want to know the type of the variable


type(A)


# Want to get the list of currently defined variables


who


whos        # for more details of the variables



# Want to delete the defined variable


del A              # here A is a variable name



The data stored in Python can be of various types, lets learn some standard data types mentioned below:



  • Numbers
  • String
  • List
  • Tuple
  • Dictionary
________________________________________________________

Numbers 

var1= 2

var2= 9

# simple operation can be performed on number variables.


var3=var1+var2            # addition 

var4=var2*var1            # multipilcation
var5=var2**var1          # exponential
________________________________________________________

String

It is basically the text or set of characters represented in the quotation marks. 

In string index is assigned to each character starting from 0 to n-1.
Slicing can be done using indexing method.

str = 'Hello friends, how r you'


# Do you want to get a first character of the string.

str[0]

Output : 'H'

# Do you want to get a range of characters

str[0:5]

Output : 'Hello'


# Backward indexing :

# if you want to get last characters

str[-1]

Output : 'u'

# Get last 3 character 

str[-3:]

Output : 'you'


# Concatenation : How to add strings

# if you want to add strings

str + ', All'

Output : 'Hello friends, how r you, All'


# Repetition of strings

str *2

Output :'Hello friends, how r youHello friends, how r you'
________________________________________________________

List

It is set of items(object) separated by commas and enclosed within square brackets ([ ])
In list index is assigned to each item starting from 0 to n-1.
Slicing can be done using indexing method.
Item belonging to list can be of different data types.

Emp = [ 'Analyst', 60000, 'xyz', 4]

Note : List supports all the sequence operation same as string, shown above in the string section.

Emp[0]                    # Give you first element of the list
Emp[1:3]                 # Give you first and second element(n-1) of the list
Emp[-1]                  # Give you last element of the list
Emp + ['Delhi']       # Concatenate 


There are few more operations which can be performed on the list. For example : sort, reverse, append etc. Lets understand by example.

lst=[1,5,2,7]         # Defining a list of same data type

lst.append(10)     # Output :  [1, 5, 2, 7, 10]

lst.reverse()        # Output : [10, 7, 2, 5, 1]

lst.sort()             # Output : [1, 2, 5, 7, 10]    sorting in ascending order

lst.sort(reverse=True)     # Output : [10, 7, 5, 2, 1] sorting in descending order
________________________________________________________

Tuple


Python tuple is similar to list with few differences, which are:

1. List is enclosed with [ ] and tuple is enclosed with ( ).
2. List can be updated but tuple can't.

Emp1 = [ 'Analyst'60000'xyz'4]                      # Defining List

type(Emp1)                                                           # Checking the type of Emp1


Emp2 = ( 'Senior Analyst', 80000'xyz', 6)          # Defining Tuple

type(Emp2)                                                           # Checking the type of Emp2 


Lets check the second difference.

Emp1[0]='Senior Analyst'

Emp2[0]='Manager'


Output: with error while updating tuple.



________________________________________________________


Dictionary


It is set of items(object) separated by commas and enclosed within curly brackets { }.
It is not sequence. Each element of the dictionary is not referenced by it relative position.
Each element is referenced by unique key.

It is set of  "Key : Values" pairs.

Lets take an example of a dictionary.

Emp_d = {'Designation': 'Analyst', 'Monthly_Salary' :60000, 'Company':'xyz', 'Experience': 4}

type(Emp_d)             # Output would be : dict 

In above mentioned dictionary.

Keys are : Designation, Monthly_Salary, Company, Experience

Values are : Analyst60000xyz4

We can get these values by code.

Emp_d.keys()                         # to get the keys   


Emp_d.values()                     # to get the values



Emp_d['Designation']           # calling value by referring key

Emp_d['Department']='Sales'   # adding one element with key : Department and Value : Sales


Enough for the first class !

Enjoy reading our other articles and stay tuned with us.

Kindly do provide your feedback in the 'Comments' Section and share as much as possible.