In this blog post we will be going to discuss about python basics
Python is multi-purpose programming language developed by Guido van Rossum.
The language has easy-to-use syntax, it's a perfect language for someone trying to learn any programming for the first time.
This is a comprehensive guide on Basic fundamentals concepts which we are keep in mind before writing code.
print()
function:¶print('hello world')
lst = list(a=c(3,5,1,-8,8,90),b=matrix(1:12,4,3))
print(lst) # print list
$a [1] 3 5 1 -8 8 90 $b [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12
hello world
print("hello", 'world')
hello world
print("hello world" + "!")
hello world!
print("welcome", "to", "python", sep="_", end=";")
welcome_to_python;
print("welcome", "to", "python", sep="_", end=";")
print("data science")
welcome_to_python;data science
print("welcome", "to", "python", sep="_", end="\n") # end with new line(by default)
print("data science")
welcome_to_python
data science
#
symbol:¶# this statement will print the statement inside in it
print("welcome to data science")
welcome to data science
Python has many native datatypes. Here are the important ones:
Operators (Decreasing order of precedence) | Operators (Decreasing order of precedence) |
---|---|
** |
Exponent |
+, -, *, /, //, % |
Addition, Subtraction, Multiplication, Division, Floor division, Modulus |
< > <= >= == != |
Comparison operators |
= %= /= //= -= += *= **= |
Assignment Operators |
is is not |
Identity operators |
in not in |
Membership operators |
not or and |
Logical operators |
Continue...https://www.guru99.com/python-operators-complete-tutorial.html
t = True
type(t)
bool
False
i = 4
type(i)
int
f = 3.45
type(f)
float
cmp = 3+4j
type(cmp)
complex
cmp.real
3.0
cmp.imag
4.0
1 + 1
2
1 * 3
3
1 / 2
0.5
print(7+8)
print(4*6)
2 ** 4
15
24
16
4 % 2
0
5 % 2
1
(2 + 3) * (5 + 5)
50
Number Calculations
(Operator Precedence)
3+3/3
4.0
(3+3)/3
2.0
((((13+5)*2)-4)/2)-13
# How did that happen? Let’s work it out.
# 13+5 gives us 18
# 18*2 gives us 36
# 36-4 gives us 32
# 32/2 gives us 16.0 #Note that division gives us floats!
# 16-13 gives us 3.0
3.0
https://data-flair.training/blogs/python-operator-precedence/
st = 'data science and machine learning with python'
type(st)
str
Indexing:
# positive indexing
print(st[0])
print(st[1])
print(st[2])
d
a
t
# negative indexing
print(st[-1])
print(st[-2])
print(st[-3])
n
o
h
Slicing:
print(st[0:4])
print(st[:4])
print(st[4:])
print(st[5:9])
print(st[::2]) # step 2
print(st[::3]) # step 2
print(st[2:12:4]) #
print(st[::-1]) # reverse the string
print(st[::-2]) # reverse the string with step 1
data
data
science and machine learning with python
scie
dt cec n ahn erigwt yhn
dacn dai ai tph
tcc
nohtyp htiw gninrael enihcam dna ecneics atad
nhy twgire nha n cec td
String Methods:
s= "hello world" # using double quotes
s= 'hello world' # using single quotes
# Multi-line Strings
to_you = """Stranger, if you passing meet me and desire to speak to me, why
should you not speak to me?
And why should I not speak to you?"""
print(to_you)
Stranger, if you passing meet me and desire to speak to me, why
should you not speak to me?
And why should I not speak to you?
s.index('d')
10
s.capitalize()
'Hello world'
s.count('o')
2
value = "cat ppicture is cat picture"
# Find first index of this string.
i = value.find("p")
print(i)
# Find first index (of this string) after previous index.
b = value.find("p", i + 1)
print(b)
b = value.find("p", i + 2)
print(b)
4
5
20
The index()
method is similar to find()
method for strings. The only difference is that find()
method returns -1 if the substring is not found, whereas index()
throws an exception.
s.title()
'Hello World'
s.upper()
'HELLO WORLD'
s.islower()
True
s= "HELLO WORLD"
s.islower()
False
s.capitalize()
'Hello world'
s.lower()
'hello world'
s = "HeLlO WoRlD"
s.swapcase()
'hElLo wOrLd'
s = " Hello world how are you "
s = "hello world how are you"
s.capitalize()
'Hello world how are you'
s.title()
'Hello World How Are You'
s.upper()
'HELLO WORLD HOW ARE YOU'
"H" in s
False
"h" in s
True
len(s)
11
s = ' hello world how are you'
s.strip()
'hello world how are you'
min(s.strip())
' '
max(s)
'y'
s
' hello world how are you'
s.isspace()
False
s.strip()
'hello world how are you'
strings immutable nature
my_string = "abc"
my_string[0] = "d"
{py}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-f26f28798517> in <module>()
1 my_string = "abc"
----> 2 my_string[0] = "d"
TypeError: 'str' object does not support item assignment
strings are immutable, which means you can't change an existing string (inplace). The best you can do is create a new string that is a variation on the original:
greeting = 'Hello, world!'
new_greeting = 'J' + greeting[1:]
print(new_greeting)
print(greeting)
Jello, world!
Hello, world!
String.format()
name = "sumendar"
greeting = "My name is {}".format(name)
greeting
'My name is sumendar'
String concatenation
print("happy" + " " + "birthday")
print("my name is " + "john")
happy birthday
my name is john
Escape Sequences in Strings
the backslash causes the subsequent character sequence to “escape”
its usual meaning.print('This string contains a single quote (') character.')
# gives a syntaxError
File "<ipython-input-8-4fb72c6c5730>", line 1
print('This string contains a single quote (') character.')
^
SyntaxError: invalid syntax
print('This string contains a single quote (\') character.')
print("This string contains a double quote (\") character.")
This string contains a single quote (') character.
This string contains a double quote (") character.
print('a,
b,
c) # if we press Enter will gives SyntaxError after each letter
File "<ipython-input-11-06aeb0cb83d6>", line 1
print('a,
^
SyntaxError: EOL while scanning string literal
print('a\
b\
c')
print('a\
b\
c')
abc
abc
print('foo\\bar') # o include a literal backslash in a string, escape it with a backslash
foo\bar
print(r'foo\bar') # o include a literal backslash in a string, escape it with a letter r
foo\bar
Applying Special Meaning to Characters
print('foo\tbar')
print("a\tb")
print("a\141\x61")
print("a\nb")
print('\u2192 \N{rightwards arrow}')
foo bar
a b
aaa
a
b
→ →
Raw Strings
print('foo\nbar')
foo
bar
print(r'foo\nbar')
foo\nbar
print('foo\\bar')
foo\bar
print(r'foo\\bar')
foo\\bar
Triple-Quoted Strings
print('''This string has a single (') and a double (") quote.''')
This string has a single (') and a double (") quote.
print("""This is a
string that spans
across several lines""")
# this also allows for multiline strings:
This is a
string that spans
across several lines
https://towardsdatascience.com/useful-string-methods-in-python-5047ea4d3f90
Python Syntax:
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-syntax/reference
A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
import datetime
x = datetime.datetime.now()
print(x)
2019-03-07 10:33:50.931781
print(x.year)
2019
# strftime is a method for formatting date objects into readable strings.
print(x.strftime("%A"))
# it takes one parameter called format
# http://strftime.org/
Thursday
# Create a date object
x = datetime.datetime(2020, 5, 17)
print(x)
2020-05-17 00:00:00
https://www.guru99.com/date-time-and-datetime-classes-in-python.html
https://dzone.com/articles/python-101-working-with-dates-and-time
(Non-primitive data structures)
Non-primitive types are the sophisticated members of the data structure family. They don't just store a value, but rather a collection of values in various formats.
Example:
- List of employees in your department
- List of stocks you want to analyze A = [ ] # This is a blank list variable B = [1, 23, 45, 67] # this list creates an initial list of 4 numbers. C = [2, 4, 'john'] # lists can contain different variable types.
[1,2,3]
[1, 2, 3]
['hi',1,[1,2]]
['hi', 1, [1, 2]]
my_list = ['a','b','c']
my_list.append('d')
my_list
['a', 'b', 'c', 'd']
my_list[0]
'a'
my_list[1]
'b'
my_list[1:]
['b', 'c', 'd']
my_list[:1]
['a']
my_list[0] = 'NEW'
my_list
['NEW', 'b', 'c', 'd']
mylist = ['Rhino', 'Grasshopper', 'Flamingo', 'Bongo']
B = len(mylist) # This will return the length of the list which is 3. The index is 0, 1, 2, 3.
print (mylist[1]) # This will return the value at index 1, which is 'Grasshopper'
print (mylist[0:2]) # This will return the first 2 elements in the list.
Grasshopper
['Rhino', 'Grasshopper']
nest = [1,2,3,[4,5,['target']]]
nest[3]
[4, 5, ['target']]
nest[3][2]
['target']
nest[3][2][0]
'target'
nest[3][2][0].upper()
# assign data to a specific element of the list using an index into the list.
mylist = [0, 1, 2, 3]
mylist[0] = 'Rhino'
mylist[1] = 'Grasshopper'
mylist[2] = 'Flamingo'
mylist[3] = 'Bongo'
print (mylist[1])
Grasshopper
The + operator concatenates lists:
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b #
print(c)
[1, 2, 3, 4, 5, 6]
Similarly, the * operator repeats a list a given number of times:
[0] * 4
[0, 0, 0, 0]
[1, 2, 3] * 3 # The first example repeats [0] four times. The second example repeats the list [1, 2, 3] three times.
[1, 2, 3, 1, 2, 3, 1, 2, 3]
The Difference Between “is” and “==” in Python
a = 'banana'
b = 'banana'
a is b
True
a = [1, 2, 3]
b = [1, 2, 3]
a is b
False
What does [::-1} do?
l = [1,2,3,4,5]
l[::-1] # reverse the order of list
[5, 4, 3, 2, 1]
follow along: https://dbader.org/blog/difference-between-is-and-equals-in-python
https://medium.com/@tyastropheus/tricky-python-i-memory-management-for-mutable-immutable-objects-21507d1e5b95
Built-in Functions with List
Function | Description |
---|---|
any() | Return True if any element of the list is true. If the list is empty, return False. |
all() | Returns True if all elements of an iterable are true |
len() | Return the length (the number of items) in the list. |
list() | Convert an iterable (tuple, string, set, dictionary) to a list. |
max() | Return the largest item in the list. |
min() | Return the smallest item in the list |
sorted() | Return a new sorted list (does not sort the list itself). |
reversed() | Returns a reverse iterator |
sum() | Return the sum of all elements in the list. |
x = [True, True, False]
if any(x):
print("At least one True")
if all(x):
print("Not one False")
if any(x) and not all(x):
print("At least one True and one False")
At least one True
At least one True and one False
Python list Methods
Methods | Functions |
---|---|
append() | to add element to the end of the list |
extend() | to extend all elements of a list to the another list |
insert() | to insert an element at the another index |
remove() | to remove an element from the list |
pop() | to remove elements return element at the given index |
clear() | to remove all elements from the list |
index() | to return the index of the first matched element |
count() | to count of number of elements passed as an argument |
sort() | to sort the elements in ascending order by default |
reverse() | to reverse order element in a list |
copy() | to return a copy of elements in a list |
copy()
method vs direct assignment
l1 = [1,2,3,4]
l2 = l1
print(id(l1))
print(id(l2))
140488771147336
140488771147336
l1.append(999)
print(l2)
[1, 2, 3, 4, 999]
l1 = [1,2,3,4]
l2 = l1.copy()
print(id(l1))
print(id(l2))
140488767397832
140488771825864
l1.append(999)
print(l2)
[1, 2, 3, 4]
https://developers.google.com/edu/python/lists
http://thepythonguru.com/python-lists/
https://www.tutorialspoint.com/python/python_lists.htm
append()
, remove()
, extend()
, insert()
and pop()
due to its immutable nature. However, there are many other built-in methods to work with tuple Example:
- List of months, or weekdays..etc
- List of top 10 sales regions in a quarter
t = (1,2,3)
t[0]
t[0] = 'NEW' # not mutable for tuples
{python}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-212-9650bd97cc9a> in <module>()
----> 1 t[0] = 'NEW' # not mutable for tuples
TypeError: 'tuple' object does not support item assignment
# real world example
# works better in spyder tool
""" Where's My Mouse? """
import tkinter
def mouse_click(event):
# retrieve XY coords as a tuple
coords = root.winfo_pointerxy()
print('coords: {}'.format(coords))
print('X: {}'.format(coords[0]))
print('Y: {}'.format(coords[1]))
print(type(coords))
root = tkinter.Tk()
root.bind('<Button>', mouse_click)
root.mainloop()
https://data-flair.training/blogs/python-tuples-syntax-examples/
http://thepythonguru.com/python-tuples/
Examples:
- Employee name associated with Employee ID
- Order status of online food
room_num = {'john': 425, 'tom': 212}
room_num['john'] = 645 # set the value associated with the 'john' key to 645
print(room_num)
{'tom': 212, 'john': 645}
room_num['james'] = 925
print(room_num)
{'james': 925, 'tom': 212, 'john': 645}
room_num.keys()
dict_keys(['james', 'tom', 'john'])
room_num.values()
dict_values([925, 212, 645])
room_num.items()
dict_items([('james', 925), ('tom', 212), ('john', 645)])
room_num.pop('james') # takes key as parameter and return corresponding value
925
room_num
{'john': 645, 'tom': 212}
room_num.popitem() # doesn't take any parameter and gives some arbitrary element as (key, value) tuple
('tom', 212)
room_num
{'john': 645}
room_num['isaac'] = 345 # Add a new key 'isaac' with the associated value
print (room_num.keys()) # print out a list of keys in the dictionary
print ('isaac' in room_num) # test to asee if 'issac' is in the dictionary. This returns true.
dict_keys(['isaac', 'john'])
True
d = {'key1':'value1','key2':'value2'}
d
{'key1': 'item1', 'key2': 'item2'}
d['key1']
'item1'
https://www.tutorialspoint.com/python/python_dictionary.htm
http://thepythonguru.com/python-dictionaries/
https://realpython.com/python-dicts/
discard()
, pop()
, clear()
, remove()
, add()
, and more. Functions like len()
and max()
also apply on sets.{1,2,3}
{1, 2, 3}
Adding Elements to a Set
add()
method to add a single element if we need to add more than one element to a set, we can use the update()
method# creating a set
a = {1, 3, 5, 7, 11, 13}
b = set([1, 3, 5, 7, 11, 13])
Set Operations on Sets
# Union
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print ( A | B)
{1, 2, 3, 4, 5, 6}
# Intersection
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print ( A & B )
{3, 4}
# Difference
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print(A - B)
{1, 2, 3}
# Symmetric Difference
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print(A ^ B)
{1, 2, 3, 6, 7, 8}
Adding element/s to a set
a.add(8)
print(a)
b.update([5,2,14,7])
print(b)
{1, 3, 5, 7, 8, 11, 13}
{1, 2, 3, 5, 7, 11, 13, 14}
# initialize my_set
my_set = {1,3}
print(my_set)
# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4,5], {1,6,8})
print(my_set)
# if you uncomment below line
# you will get an error
# TypeError: 'set' object does not support indexing
#my_set[0]
{1, 3}
{1, 3, 4, 5, 6, 8}
Remove Elements From a Set
discard()
This method takes the item to delete as an argument.
num = {13,12,21,43,16,25}
num.discard(3)
num
{12, 13, 16, 21, 25, 43}
remove()
Like the discard() method, remove() deletes an item from the set.
num.remove(5)
num
{1, 2, 4, 6}
pop()
pop()
method. In case of list, the last element of the list gets popped when the pop() method is used. num = {13,12,21,43,16,25}
num.pop()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-21-03a12495f7f5> in <module>()
----> 1 num.pop()
KeyError: 'pop from an empty set'
We can also remove all items from a set using clear()
.
clear()
num = {13,12,21,43,16,25}
num.clear()
num
set()
frozensets
(creating immutable sets)
S = frozenset([1,2,3])
print(S)
frozenset({1, 2, 3})
https://realpython.com/python-sets/
https://www.programiz.com/python-programming/set
https://stackabuse.com/sets-in-python/
""" A Queue of Groceries to Put Away """
# queue is a not built-in datatype
# create a new queue object
import queue
q = queue.Queue() # create a new object by calling new Queue() custroctor
print(q.empty())
# put bags into the queue
q.put('bag1')
print(q.empty())
q.put('bag2')
q.put('bag3')
# get bags from the queue in FIFO order
print(q.get())
print(q.get())
print(q.get())
# q.get() # causes an error: blocking method, the program will wait until method completes before continuiing execution
# (restart the shell)
# create a new queue to hold two items
q = queue.Queue(2)
print(q.empty())
# put two bags into the two-item queue
q.put('bag1')
print(q.full())
q.put('bag2')
print(q.full())
# try to put an extra bag into the queue
q.put_nowait('bag3') # causes an error
""" A Stack of Bills to Pay """
# python doesn't has built-n stack module, we can use list instead
# create a list to use as the stack
stack = list()
# add some bills to the stack
stack.append('bill1')
stack.append('bill2')
# remove the top bill to pay it
print(stack.pop())
# add two more bills to the stack
stack.append('bill3')
stack.append('bill4')
# remove bills from top to bottom
print(stack.pop())
print(stack.pop())
print(stack.pop())
stack.pop() # causes Indexerror exception
int1 = 4
float1 = int1 + 2.1 # 4 converted to float
# str1 = "My int:" + int1 # Error: no implicit type conversion from int to string
int2 = 4 + True # 5: bool is implicitly converted to int
str1 = "My int:" + str(int1)
v1 = int(2.7) # 2
v2 = int(-3.9) # -3
v3 = int("2") # 2
v4 = int("11", 16) # 17, base 16
v5 = long(2) # this supports only in python 2 vesion
v6 = float(2) # 2.0
v7 = float("2.7") # 2.7
v8 = float("2.7E-2") # 0.027
v9 = float(False) # 0.0
vA = float(True) # 1.0
vB = str(4.5) # "4.5"
vC = str([1, 3, 5]) # "[1, 3, 5]"
vD = bool(0) # False; bool fn since Python 2.2.1
vE = bool(3) # True
vF = bool([]) # False - empty list
vG = bool([False]) # True - non-empty list
vH = bool({}) # False - empty dict; same for empty tuple
vI = bool("") # False - empty string
vJ = bool(" ") # True - non-empty string
vK = bool(None) # False
vL = bool(len) # True
vM = set([1, 2])
vN = list(vM)
vO = list({1: "a", 2: "b"}) # dict -> list of keys
vP = tuple(vN)
vQ = list("abc") # ['a', 'b', 'c']
print v1, v2, v3, type(v1), type(v2), type(v3)
References
https://www.programiz.com
https://realpython.com/python-data-types/#strings
Further Resources
https://realpython.com/python-variables/
https://realpython.com/python-data-types/
https://realpython.com/python-operators-expressions/
https://realpython.com/python-strings/
https://www.datacamp.com/community/tutorials/data-structures-python