DEV Community

Kiran U Kamath
Kiran U Kamath

Posted on • Originally published at blog.learnwithdata.me

Peep into Python basics

This blog is written in Jupyter notebook, so you can experiment and learn by editing the notebook.

Click here for notebook.

Just change the input and check the output.

Learning by experiment and hands-on exercises is always better.

The purpose of this notebook is just to revise python basics.

Let's get started.

input is a built-in function in python

Get your name as an input and print it out on the screen

name = input("Welcome! Welcome! Welcome!, What's your name: ")
print('Hello', name)
Enter fullscreen mode Exit fullscreen mode
Welcome! Welcome! Welcome!, What's your name: Kiran
Hello Kiran
Enter fullscreen mode Exit fullscreen mode

Boolean values can be represented as one of two constant objects "False" and "True".

# Booleans behave like integers 0 and 1.
True
Enter fullscreen mode Exit fullscreen mode
True
Enter fullscreen mode Exit fullscreen mode
urMoney = 100
friendsMoney = 200
print(urMoney == friendsMoney)
Enter fullscreen mode Exit fullscreen mode
False
Enter fullscreen mode Exit fullscreen mode

List

A list is a collection which is ordered and changeable.

List allows duplicate members.

my_list = ['Hello', 'Everyone', 'and', 'Welcome', 'to', 'Python', 'basics']
my_list
Enter fullscreen mode Exit fullscreen mode
['Hello', 'Everyone', 'and', 'Welcome', 'to', 'Python', 'basics']
Enter fullscreen mode Exit fullscreen mode
# Obtain the datatype
type(my_list)
Enter fullscreen mode Exit fullscreen mode
list
Enter fullscreen mode Exit fullscreen mode
# list with mixed datatypes 
# (for example you can have strings and integers in one list)
# You can have a list inside another list (nested list)

my_list = ["GOOG", [3, 6, 7],"GOOG", [3, 6, 7],"GOOG", [3, 6, 7]]
my_list
Enter fullscreen mode Exit fullscreen mode
['GOOG', [3, 6, 7], 'GOOG', [3, 6, 7], 'GOOG', [3, 6, 7]]
Enter fullscreen mode Exit fullscreen mode
# Access specific elements in the list with Indexing
# Note that the first element in the list has an index of 0 (little confusing but you'll get used to it!)
my_list[1]
Enter fullscreen mode Exit fullscreen mode
[3, 6, 7]
Enter fullscreen mode Exit fullscreen mode
# List Slicing (getting more than one element from a list) 

# obtain elements starting from index 3 up to and not including element with index 6  
print(my_list[3:6])
Enter fullscreen mode Exit fullscreen mode
[[3, 6, 7], 'GOOG', [3, 6, 7]]
Enter fullscreen mode Exit fullscreen mode
# Obtain the length of the list (how many elements in the list) 
len(my_list)
Enter fullscreen mode Exit fullscreen mode
6
Enter fullscreen mode Exit fullscreen mode

Dictionary

my_dict = {'key1':'value1', 'key2':'value2', 'key3':'value3'}

Dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its corresponding value.

Keys are unique within a dictionary while values may not be.

List elements are accessed by their position in the list, via indexing while Dictionary elements are accessed via keys

# Define a dictionary using key-value pairs
my_dict = {'key1':'value1',
           'key2':'value2', 
           'key3':'value3'}
Enter fullscreen mode Exit fullscreen mode
# Check the data type
type(my_dict)
Enter fullscreen mode Exit fullscreen mode
dict
Enter fullscreen mode Exit fullscreen mode
# Access specific element in the dictionary using a specific key (ex: Key2)
my_dict['key2']
Enter fullscreen mode Exit fullscreen mode
'value2'
Enter fullscreen mode Exit fullscreen mode

String

A string in Python is a sequence of characters

String can be enclosed by either double or single quotes

my_string = "Hello Everyone and Welcome to Python basics"
my_string
Enter fullscreen mode Exit fullscreen mode
'Hello Everyone and Welcome to Python basics'
Enter fullscreen mode Exit fullscreen mode

Split is used to divide up the string into words

The output from split is a list



x = my_string.split()
x
Enter fullscreen mode Exit fullscreen mode
['Hello', 'Everyone', 'and', 'Welcome', 'to', 'Python', 'basics']
Enter fullscreen mode Exit fullscreen mode

Tuple

A tuple is a sequence of immutable Python objects.

Tuples are sequences, just like lists.

The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

tuple_1 = ('GOOGLE', 'APPLE', 10, 15, 1992);
tuple_2 = (450, 55, 977, 2100);
type(tuple_1)

Enter fullscreen mode Exit fullscreen mode
tuple
Enter fullscreen mode Exit fullscreen mode
# Accessing elements in a tuple
tuple_1[1]
Enter fullscreen mode Exit fullscreen mode
'APPLE'
Enter fullscreen mode Exit fullscreen mode
# Changing a vlue of a tuple does not work! 
# 'tuple' object does not support item assignment
tuple_1[1] = 0
Enter fullscreen mode Exit fullscreen mode
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-18-0d93d25c1b58> in <module>()
      1 # Changing a vlue of a tuple does not work!
      2 # 'tuple' object does not support item assignment
----> 3 tuple_1[1] = 0


TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

Set

A set is an unordered collection of items. Every element is unique (no duplicates).

A set is created by placing all the items (elements) inside curly braces {}, separated by comma or by using the built-in function set().


my_set = {'GOOGLE', 'APPLE', 'Jio'}
print(my_set)
Enter fullscreen mode Exit fullscreen mode
{'Jio', 'GOOGLE', 'APPLE'}
Enter fullscreen mode Exit fullscreen mode
# set do not have duplicates
my_set = {'GOOG', 'APPL', 'T','TSLA','T','AAPL'}
print(my_set)
Enter fullscreen mode Exit fullscreen mode
{'GOOG', 'T', 'TSLA', 'AAPL', 'APPL'}
Enter fullscreen mode Exit fullscreen mode

If Else statements

  • A simple if-else statement is written in Python as follows:
if condition:
  statement #1
else:
  statement #2
Enter fullscreen mode Exit fullscreen mode
  • If the condition is true, execute the first indented statement
  • if the condition is not true, then execute the else indented statements.
  • Note that Python uses indentation (whitespace) to indicate code sections and scope.
if 10 > 9:
    print('If condition is True')
else:
    print('If condition is False')
Enter fullscreen mode Exit fullscreen mode
If condition is True
Enter fullscreen mode Exit fullscreen mode

Loops

For loops are used for iterating over a sequence (a list, a tuple, a dictionary, a set, or a string).

An action can be executed once for each item in a list, tuple of the for loop.

For loop

for i in my_list:
    print(i)
Enter fullscreen mode Exit fullscreen mode
GOOG
[3, 6, 7]
GOOG
[3, 6, 7]
GOOG
[3, 6, 7]
Enter fullscreen mode Exit fullscreen mode

Range() generates a list of numbers, which is used to iterate over with for loops.

range() is 0-index based, meaning list indexes start at 0, not 1.

The last integer generated by range() is up to, but not including, last element.

Example: range(0, 10) generates integers from 0 up to, but not including, 10.

for i in range(6):
    print(i)
Enter fullscreen mode Exit fullscreen mode
0
1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

While loop

While loop can be used to execute a set of statements as long as a certain condition holds true.

i = 0

while i <=7:
    print(i)
    i = i + 1
Enter fullscreen mode Exit fullscreen mode
0
1
2
3
4
5
6
7
Enter fullscreen mode Exit fullscreen mode

break() is used to exit a for loop or a while loop

continue() is used to skip the current block, and return to the "for" or "while" statement.

# print out all elements in the list up until the Python (Ticker Symbol = Python) is detected
for i in my_list:
    print(i)
    if i == 'Python':
        break  
Enter fullscreen mode Exit fullscreen mode
GOOG
[3, 6, 7]
GOOG
[3, 6, 7]
GOOG
[3, 6, 7]
Enter fullscreen mode Exit fullscreen mode
# Print only odd elements and skip even numbers
No_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in No_list:
    if i%2 == 0:
        continue
    print(i)
Enter fullscreen mode Exit fullscreen mode
1
3
5
7
9
Enter fullscreen mode Exit fullscreen mode

List comprehension is an elegant tool to transform one list into another list.

Instead of using loops and append, list comprehension is used to iterate over a list, condition its elements and include them in a new list.

input_list = [1, 2, 3, 4]

[ element ** 2  for element in input_list]
Enter fullscreen mode Exit fullscreen mode
[1, 4, 9, 16]
Enter fullscreen mode Exit fullscreen mode

Lambda function

Lambda function is used to create a function without a name

Lambda functions are mainly used with filter() and map()

Lambda function can receive any number of arguments, but can only have one expression.

# We can do the same task using Lambda expression
# Note that there is no function name
y = lambda x:x**2
Enter fullscreen mode Exit fullscreen mode

The map() function takes in a function and a list.

map() performs an operation on the entire list and return the results in a new list.

# Define two lists a and b
a = [1, 4, 5, 6, 9]
b = [1, 7, 9, 12, 7]
Enter fullscreen mode Exit fullscreen mode
# Let's define a function that adds two elements together
def summation(a, b):
    return a + b
Enter fullscreen mode Exit fullscreen mode
# You can now use map() to apply a function to the entire list and generate a new list
c = list(map(summation, a, b))
c
Enter fullscreen mode Exit fullscreen mode
[2, 11, 14, 18, 16]
Enter fullscreen mode Exit fullscreen mode

filter is used to create a list of elements for which a function returns "True".

prices = [105, 5055, 40, 356, 923, 1443, 222, 62]
Enter fullscreen mode Exit fullscreen mode
# return only even numbers
out = list(filter( lambda x: (x % 2 == 0) , prices  ))
out
Enter fullscreen mode Exit fullscreen mode
[40, 356, 222, 62]
Enter fullscreen mode Exit fullscreen mode




Files

  • open() is the key function to handle files
  • open() function takes two parameters: (1) filename (2) mode.
  • Modes for files opening:

    • "r" - Read - Default value. Opens a file for reading, error if the file does not exist
    • "a" - Append - Opens a file for appending, creates the file if it does not exist
    • "w" - Write - Opens a file for writing, creates the file if it does not exist
    • "x" - Create - Creates the specified file, returns an error if the file exists
  • files can be used in a binary or text mode

    • "t" - Text - Default value. Text mode
    • "b" - Binary - Binary mode (e.g. images)

f = open('sample_file.txt', 'r')

print(f.read())

outputs the content of sample file

readline will be used to read the first line

print(f.readline())

You can open the file and append content to it

f.write('this is a new line that I added to the file!')

close the file and let's check its content

f.close()

Top comments (0)