<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Faith K</title>
    <description>The latest articles on DEV Community by Faith K (@k_faithk).</description>
    <link>https://dev.to/k_faithk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F814025%2F6d7e549d-3f09-460a-9ba6-532ea0b0e366.png</url>
      <title>DEV Community: Faith K</title>
      <link>https://dev.to/k_faithk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/k_faithk"/>
    <language>en</language>
    <item>
      <title>Introduction to Data Structures and Algorithms in Python.</title>
      <dc:creator>Faith K</dc:creator>
      <pubDate>Mon, 21 Feb 2022 19:39:09 +0000</pubDate>
      <link>https://dev.to/k_faithk/introduction-to-data-structures-and-algorithms-in-python-54jk</link>
      <guid>https://dev.to/k_faithk/introduction-to-data-structures-and-algorithms-in-python-54jk</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Introduction&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Data structures are simply the different ways of organizing and storing data in our computers so as to perform operations on the data.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bult-in Data Structures
&lt;/h2&gt;

&lt;p&gt;Data structures allow us to organize, store, and manage data for efficient access and modification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List&lt;/strong&gt;&lt;br&gt;
A list is defined using square brackets and holds data that is separated by commas. It is a type of data used to represent/ store a list of objects in a single variable . It can contain a mix of different data types. We use print()function to print the output on the terminal.&lt;br&gt;
Lists are indexed from 0&lt;br&gt;
Example of a list in Python is as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mylist=["Faith",32,"Kenya"]
print (mylist)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[‘Faith’,32,’Kenya’]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The append() method adds an item to the end of the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mylist.append(3)
print(mylist)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The insert() method adds an item at a specific place in the list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mylist.insert(0, 38)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pop() method removes last element from list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers.pop()
print(mylist)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access an element on the list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(numbers[0])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To check index of an item&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits = ['apple', 'banana', 'orange', 'grape']

index = fruits.index('banana')

print(index)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Tuple&lt;/strong&gt;&lt;br&gt;
A tuple is an ordered collection of items. It is a data type for immutable ordered sequences of elements I.e&lt;br&gt;
you can’t add and remove elements from tuples, or sort them in place&lt;br&gt;
A tuple is used to store a sequence of objects.&lt;br&gt;
Tuples are indexed from 0.&lt;br&gt;
We can create tuples using the method below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;positions = (4,9,18)
print(positions)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access tuples using their index we use the method shown below:&lt;br&gt;
&lt;code&gt;print(positions [2])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To create a list of tuples we use the method shown below:&lt;br&gt;
&lt;code&gt;positions = [(4,9,18), (30,15, 19)]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Set is a mutable and unordered collection of unique elements. It can permit us to remove duplicate quickly from a list.&lt;br&gt;
A set is mutable, it can be changed or modified once created.&lt;/p&gt;

&lt;p&gt;Removing duplicates from a list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [9, 16, 3, 4, 9, 3]
unique_numbers = set(numbers)
print(unique_numbers)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Union of sets&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;first_set = {3, 4}
second_set = {5, 6}
print(first | second)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Intersection of sets&lt;br&gt;
&lt;code&gt;print(first_set &amp;amp; second_set)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Difference between two sets&lt;br&gt;
&lt;code&gt;print(first_set - second_set)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Symmetric difference&lt;br&gt;
&lt;code&gt;print(first_set ^ second_set)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Iterating over elements in a set&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = {3, 7}
for x in numbers:
    print(x)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3
7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dictionary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dictionary is a mutable and unordered data structure. It permits storing a pair of items (i.e. keys and values).&lt;br&gt;
We can use strings, numbers or tuples as keys.&lt;br&gt;
The value can be any data type.&lt;br&gt;
We can assign one value to multiple keys.&lt;br&gt;
We can create dictionaries using dict() method.&lt;/p&gt;

&lt;p&gt;Creating a dictionary&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user = {"name": "Faith", "age":32, "hobbies": ["cooking", "singing"]}
print(user)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Accessing a specific key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(user["name"])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Specifying a default value for keys that do not exist&lt;br&gt;
&lt;code&gt;print(user.get("email", "Invalid key"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Adding a key to the dictionary&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user["email"] = "lobbystar@gmail.com"
print(user.get("email"))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing value of a key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user[name] = "Liam"
print(user)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To modify several or all values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.update({"name": "David", "age": 21, "email": "kingdavid@gmail.com"})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To delete a key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;del user["hobbies"]
print(user)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looping through keys and values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for key, value in user.items():
    print(key, value)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  User-defined data structures
&lt;/h2&gt;

&lt;p&gt;These are data structures that are not supported by Python, but can be programmed by the user to function similarly to Python built-in data structures.&lt;br&gt;
We will look at stacks, queues and linked lists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A stack is a linear data structure that follows the LIFO(Last In First Out) principle.. So, the last element inserted will be removed as the first. The operations are:&lt;br&gt;
    • Push → inserting an element into the stack&lt;br&gt;
    • Pop → deleting an element from the stack&lt;br&gt;
The conditions to check:&lt;br&gt;
    • overflow condition → this condition occurs when we try to put one more element into a stack that is already having maximum elements.&lt;br&gt;
    • underflow condition →this condition occurs when we try to delete an element from an empty stack.&lt;/p&gt;

&lt;p&gt;Stacks in Python can be implemented using lists, Collections.deque, queue.LifoQueue&lt;/p&gt;

&lt;p&gt;Functions we can perform on stacks&lt;/p&gt;

&lt;p&gt;append/ push() -to add an item on top of the stack.&lt;br&gt;
pop() -to remove an item on top of the stack.&lt;br&gt;
peek/ top() - to view the top element of the stack.&lt;br&gt;
size()-returns the size of the stack.&lt;br&gt;
[-1] -to get the item that is on top of the stack. It only works if our stack is not empty.&lt;br&gt;
empty()-to return whether the stack is empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Queue&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A queue is a linear data structure that follows FIFO(First In First Out) principle.&lt;br&gt;
Insertion and deletion in queues happens on the rear-end and front-end.&lt;br&gt;
In Python, queues can be implemented using lists, collection.deque, queue.Queue&lt;br&gt;
Two ends:&lt;br&gt;
    • front → points to starting element&lt;br&gt;
    • rear → points to the last element&lt;br&gt;
There are two operations:&lt;br&gt;
    • enqueue → inserting an element into the queue. It will be done at the rear.&lt;br&gt;
    • dequeue → deleting an element from the queue. It will be done at the front.&lt;br&gt;
There are two conditions:&lt;br&gt;
    • overflow → insertion into a queue that is full&lt;br&gt;
    • underflow → deletion from the empty queue&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linked list&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A linked list is a data structure for storing a collection of items.&lt;br&gt;
A linked list can be visualized as several boxes connected to each other.&lt;br&gt;
The data used in linked lists can be strings, numbers, characters, etc.&lt;br&gt;
Linked lists can be used to implement stacks, queues and graphs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithms
&lt;/h2&gt;

&lt;p&gt;Algorithms are instructions that are formulated in a finite and sequential order to solve problems.&lt;br&gt;
When we write an algorithm, we have to know what is the exact problem, determine where we need to start and stop and formulate the intermediate steps.&lt;br&gt;
Algorithms are also the operations that we can perform on different data structures and the set of instructions for executing them.&lt;/p&gt;

&lt;p&gt;To learn more about algorithms click &lt;a href="https://www.tutorialspoint.com/python_data_structure/python_algorithm_design.htm#:~:text=Algorithm%20is%20a%20step%2Dby,more%20than%20one%20programming%20language."&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also read this &lt;a href="https://www.geeksforgeeks.org/data-structures/"&gt;article&lt;/a&gt; to learn more about data structures.&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Introduction to modern python.</title>
      <dc:creator>Faith K</dc:creator>
      <pubDate>Mon, 14 Feb 2022 19:46:10 +0000</pubDate>
      <link>https://dev.to/k_faithk/introduction-to-modern-python-46a7</link>
      <guid>https://dev.to/k_faithk/introduction-to-modern-python-46a7</guid>
      <description>&lt;p&gt;Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages of Python&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Easy to use and learn: For beginners, Python is straightforward to use. It is a high-level programming language, and its syntax is like the English language. These reasons make the language easy to learn and adapt to. Compared to Java and C, in Python, the same task can be performed using fewer lines of code. As a result of its easy learning, the principles in Python can be executed faster compared to other languages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increased productivity: Python is a very productive language. The simple nature of Python helps the developers to concentrate on solving the issues in it. To understand the syntax and behavior of the programming language, the users do not have to spend hours, so more work is done.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flexibility: This language is very flexible, and hence it allows the user to try new things. The users can develop new sorts of the application using Python programming language. The language does not restrict the user from trying something different. Other programming languages do not provide this type of flexibility and freedom, and hence Python is more preferred in these matters.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Uses and application of Python programming language.&lt;/strong&gt; &lt;br&gt;
Python can be used for:&lt;br&gt;
    • AI and machine learning.&lt;br&gt;
    • Data analytics.&lt;br&gt;
    • Data visualisation. &lt;br&gt;
    • Programming applications. &lt;br&gt;
    • Web development. &lt;br&gt;
    • Game development. &lt;br&gt;
    • Language development. &lt;br&gt;
    • Finance.&lt;/p&gt;

&lt;p&gt;THE PYTHON ENVIRONMENT&lt;br&gt;
1. Download &lt;a href="https://www.python.org/downloads/"&gt;Python&lt;/a&gt;&lt;br&gt;
2.Select your Operating system(Windows,Linux/UNIX,MacOS,Other&lt;br&gt;
3.Select the release or version, click on it to download.&lt;br&gt;
4.Double click on the file to execute and install.&lt;br&gt;
5.For window mark “add to system paths”&lt;/p&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;p&gt;If you are using Ubuntu 16.10 or newer, then you can easily install Python 3.6 with the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update
$ sudo apt-get install python3.6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to install Pipenv, so you can install dependencies and manage virtual environments.&lt;/p&gt;

&lt;p&gt;A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.&lt;/p&gt;

&lt;p&gt;More : &lt;a href="https://pipenv.pypa.io/en/latest/"&gt;Pipenv &amp;amp; Virtual Environments docs!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python fundamentals.&lt;br&gt;
Functions&lt;br&gt;
A program could be long and complicated but it is built of simple parts.&lt;br&gt;
Variable: Helps keep track of the information we need to successfully execute a program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_name = 'Faith'
my_age = 32
print('My name is', my_name)
print('My age is', my_age)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;String: Is a sequence of characters.&lt;br&gt;&lt;br&gt;
Numbers - There are three numeric types in python;&lt;br&gt;
    • Integer - is a whole number, positive or negative, without decimal e.g 21&lt;br&gt;
    • Complex - these are written with a "j" as the imaginary part e.g 67j&lt;br&gt;
    • Booleans - These store an output of either True or False&lt;br&gt;
    • Float - is a number, negative or positive containing one or more decimal points e.g 213.8&lt;/p&gt;

&lt;p&gt;Functions: Allow us to define a task we would like the computer to carry out based on input. We define functions using def. .A function is a block of code that only runs when it's called.&lt;br&gt;
Functions enable the resuse of repetitive parts of the program.&lt;br&gt;
Below is a function to print a user's name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Def say-hello():
my_name= ”Faith Kavesu”
print(‘Hello’,my_name)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Data Structures (Lists, Tuples, Dictionary &amp;amp; Sets)&lt;br&gt;
List: Python lists are used to store data in array format. Different data types can be stored in the same list.Ordered collection of items using square bracket. Let’s try creating a list of to-do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;todo= ['read', 'cleaning', ‘colour']
todo.append('eat')
print(todo)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tuples: It is similar to a list with major difference that it is immutable. We create using parentheses ()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;todoo= ('cook','clean','code','sleep')
print(todoo)
print(todoo[0:2])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dictionary: Involves a key associated with a value in a key-value pair. The key-value pair are in {} syntax to create a dict. Each key-value pair is separated by a comma, and within a pair the key and value are separated by a colon :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user = { 
    "name" : 'Faith', 
    "age" : 32,
    "height" : "156"
    }
print(user)
print(user['age'])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set:They are used to store multiple items in a single variable. Sets contain unique items and there cannot be any duplicates or else they will be eliminated.Similar to a list except that it is unordered. It can store heterogenous data and it is mutable. Creating a set enclosed in a curly bracket {}.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set1 = {"Faith", "32", "173"}
print(my_set)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
