-
Python is a widely used general-purpose, high level programming
language. It was created by Guido van Rossum in 1991 and further
developed by the Python Software Foundation. It was designed with
an emphasis on code readability, and its syntax allows programmers
to express their concepts in fewer lines of code.Python is an
interpreted language.
-
Learn about variables in Python. Variables are used to store
values in memory. Understanding data types is essential for
efficient memory management and creating robust programs.
To define the variables we simply have to give variable name then
"=" sign the some value.
a = 5 (Integer)
b = 0.5 (double/float)
c = "hello python" (Strings)
d = True (Boolean)
e = 'C'
-
Learn about statements in Python. Conditional statements are used
when we can have 2 or more outcomes based on certain situations,
for eg. to define a person eligible for voting or not.
So here we outcomes could be "eligible" if person is 18 or above;
otherwise "not eleigible" as age is less than 18.
Syntax:
age = int(input("Enter age"))
if(age>=18):
print("eleigible")
else:
print("not eleigible")
-
Loops are the control statements which allows a set instructions
to run for specific number of times.Lets just we want to print
hello world for 100 times , so would we be writing print("hello
world") 100 times ? definitely NO. what we can do is we can write
a loop statement with range=100 & under it write print("hello
world").
There are 2 types of loops :- "FOR LOOP" & WHILE LOOP
Syntax(FOR LOOP):
for i in range(100):
print("hello world")
OUTPUT = hello world for 100 times in terminal
Syntax(While LOOP):
i=0
while i
< 100 :
print("hello world")
OUTPUT = hello world for 100 times in terminal
-
Functions in python are the piece of code which are highly
re-usable, suppose there are some lines of instructions we want to
execute at some many different points of code, so instead of
writing the same code for everytime we can define "Functions" and
use that fuctions wherever required.
Syntax:
def hello(name):
return "hi",name
//now calling function
hello(aron)
OUTPUT = hi aron
-
Python Lists are just like the arrays, declared in other languages
which is an ordered collection of data. It is very flexible as the
items in a list do not need to be of the same type.The
implementation of Python List is similar to Vectors in C++ or
ArrayList in JAVA. The costly operation is inserting or deleting
the element from the beginning of the List as all the elements are
needed to be shifted. Insertion and deletion at the end of the
list can also become costly in the case where the preallocated
memory becomes full. We can create a list in python as shown
below.List elements can be accessed by the assigned index. In
python starting index of the list, sequence is 0 and the ending
index is (if N elements are there) N-1.
To define Lists we simply have to use square brackets and insert
multiple values in it then assign to some variables as shown
below.
list1 = ['a',1,True,5,6,9,"hello world",6.9]
print(list1)
//Output
['a',1,True,5,6,9,"hello world",6.9]
-
Python Tuple is a collection of Python objects much like a list
but Tuples are immutable in nature i.e. the elements in the tuple
cannot be added or removed once created. Just like a List, a Tuple
can also contain elements of various types. In Python, tuples are
created by placing a sequence of values separated by ‘comma’ with
or without the use of parentheses for grouping of the data
sequence. Note: Tuples can also be created with a single element,
but it is a bit tricky. Having one element in the parentheses is
not sufficient, there must be a trailing ‘comma’ to make it a
tuple.
To define Tuples we simply have to use round brackets and insert
multiple values in it then assign to some variables as shown
below.
tuple1 = ('a','b','u')
print(tuple1)
//Output
('a','b','u')
-
Python Set is an unordered collection of data that is mutable and
does not allow any duplicate element. Sets are basically used to
include membership testing and eliminating duplicate entries. The
data structure used in this is Hashing, a popular technique to
perform insertion, deletion, and traversal in O(1) on average. If
Multiple values are present at the same index position, then the
value is appended to that index position, to form a Linked List.
In, CPython Sets are implemented using a dictionary with dummy
variables, where key beings the members set with greater
optimizations to the time complexity.
To define Sets we simply have to use "set([])" and insert
multiple values in it then assign to some variables as shown
below.
set1 = set(['a','b','u','a','u',])
print(set1)
//Output
{'a','b','u'}
-
Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an
unordered collection of data values, used to store data values like a map, which, unlike other Data Types
that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in
the dictionary to make it more optimized. Indexing of Python Dictionary is done with the help of keys. These
are of any hashable type i.e. an object whose can never change like strings, numbers, tuples, etc. We can
create a dictionary by using curly braces ({}) or dictionary comprehension.
To define Dictionary we simply have to use "{"key":"values"}" and insert
multiple values in it then assign to some variables as shown
below.
dict1 = {"name1":"iphone",name2:"ipad"}
//to access some value by defined key
print(dict1['name1'])
//Output
'iphone'