DEV Community

mwang-cmn
mwang-cmn

Posted on

Ultimate Introduction to Python

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language created by Guido van Rossum during 1985- 1990.
Why Python?

  1. Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly.
  2. Easy-to-read: Python code is more clearly defined and visible to the eyes.
  3. Easy-to-maintain: Python's source code is fairly easy-to-maintain.
  4. A broad standard library: Python's bulk of the library is very portable and cross platform compatible on UNIX, Windows, and Macintosh.
  5. Interactive Mode: Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.
  6. Scalable: Python provides a better structure and support for large programs than shell scripting.
  7. Databases: Python provides interfaces to all major commercial databases

Installing Python

Download the latest version for Windows, Linux and MacOS at this link
Variables
Variables store information that can be used and/or changed in your program. This information can be an integer, text, collection, etc. Variables are used to hold user inputs, local states of your program, etc. Variables have a name so that they can be referenced in the code. The fundamental concept to understand is that everything is an object in Python.
Python supports numbers, strings, sets, lists, tuples, and dictionaries. These are the standard data types.
To assign variable a value, use the equals sign (=)
Var_1= 1
var_2 = ‘dog’
var_3 = "Caroline"

Data structures and sequences

They include numbers, strings, lists, tuples, sets and dictionaries
Numbers
Include integers, decimals, floats
Strings
Textual information. Strings are sequence of letters. They are enclosed in quotation marks (single, double or triple). They are immutable. This means, once created, they cannot be changed

Tuples
A tuple is a fixed length, immutable sequence of python objects separated by commas enclosed in parathesis

{
tup1= (8,9,10) #create a tuple by enclosing a sequence in parathesis
tup2=tuple(‘string’) #convert any sequence to a tuple
tup1[2] =10 #access any item in a tuple using square brackets
}
Enter fullscreen mode Exit fullscreen mode

Lists
Lists are variable length and their contents can be modified in place (mutable). They are defined by square brackets [] or the list type function.

{
List_1= [2,3,4] #create list by enclosing sequence in square brackets
List_2=list(tup1) #use the list type function
Output= [8,9,10]
}
Enter fullscreen mode Exit fullscreen mode

Operations in lists
The following operations can be carried out in lists.

{
del(List_1[1])  #delete the second element using  the del() function
cop_list=List_1[:] #clone a list or create a copy by using a colon in square brackets
List_2.append(‘cats’) #add an element at the end of the list
Output:
 List_2
[8,9,10,’cats’]
List_2.insert(1, ‘fox’) #insert and element at index 1 of list 2
List_2
[8,’fox’,9,10,’cats’]
List_2.pop(3) #remove element at index 3
List_2
Output: [8,’fox’,9,’cats’]
List_2.extend([6, ‘dogs’]) #append multiple elements to a list using the extend function
List_2
Output: [8,’fox’,9,’cats’, 6, ‘dogs’]
Print(len(List_2)) #obtain number of elements in list
Output:6
}
Enter fullscreen mode Exit fullscreen mode

Dictionaries
A dictionary 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. Keys can be strings, numbers or tuples. Values can be any data type.

{
dict_a={‘a’:[2,3], ‘b’:[4,5], ‘c’:[8,9]} #create a dictionary by assigning values to keys
dict_a[‘a’] =[2,3] #access value of a key using square brackets

}
Enter fullscreen mode Exit fullscreen mode

Sets
A set is an unordered collection of unique elements. They are like dictionaries, but keys only, no values. A set can be created in two ways: via the set function or via a set literal with curly braces.
They do not record element position and have unique elements. They also support arithmetic operations.

{
Set1=(‘apple’, ‘banana’,’ apple’, ‘mango’, ‘kiwi’) #create a set and excludes duplicates
Set1
Output: {‘apple’, ‘banana’, ‘mango’, ‘kiwi’}
List3= [6,7,8]
Set2= set(List3) #covert a list to a set
Set2={6,7,8}
Set1.remove(‘banana’)
Set1: {‘apple’, ‘mango’, ‘kiwi’}
}
Enter fullscreen mode Exit fullscreen mode

Arithmetic operations

Python supports the following operations

{
a+b #Addition 
a-b #Subtraction 
a*b #Multiplication
a/b #Division
a%b #Modulus that returns remainder of division
a**b #Exponentiation
a//b #Floor division that rounds the result to nearest whole number
}
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)