Get started with python

NB: This is NOT a course. It is just a simple guide and it is not detailed.

Hello,In this guide I will show you how to get started with Python. Do you want to build an AI, a website,be an expert in Data Science or anything else in the tech industry? Python is here to help achieve that.

Python is easy ,accessible and flexible, that is why it's very popular among many companies like NETFLIX, NASA, Google and many others. Python is easy in the aspect that it prioritizes readability, making it easy and easy to understand and use. It is flexible since it can be used in almost evevrything e.g., Building website, cybersecurity, Artificial intelligence, building android apps. You can imagine how the list is long.

Python Is a widely-used, interpreted, object-oriented and high-level language that was created in 1991 by Guido van Rosum

Prerequisite

    Install the latest version of Python which 3+,I will be using 3.10
    Install a text a editor like Visual Studio Code or you can use the default IDLE that comes with Python. VS code is easier to use

Hello World

Let us now write our first Hello World program

Using VS Code, create a file and name it hello.py or any name of your liking. Inside the file with the file extension of .py write the following line of code

print("Hello, World!!")

Now open the terminal and on the working directory which the hello.py is under do the following:

PS C:\Users\HP\Documents\projects\python> python hello.py

Hello,World!!

Congratulations!! You have written you first python program

You can also run python code just on the terminal.Ofcoure when python is installed

You simply type the word python on the terminal and you exit the new python terminal by typing exit().

Python Types

Python as a language has a data types are used which include:

I would like to find more types..

Strings In Python

String in python is any character that is wrapper in between single quotes '' or double quotes "". So using any of these will not affect anything

In our first program we used used a string "hello, world"

If you need to include an apostrophe in you sentence you can do it by escaping backslash \ before character that you need to include.

See the example below:

print("I\'m a webdeveloper.")

This will print I'm a webdeveloper. only

Newlines

It will be very hard to read texts if we had all of them in one single line

we create new lines by using \n

we use \t to represent tabs

It can be a lot pain if we are dealing with a large string and we try creating new line with \n.There is a way to do multiple line using three quotes

print(""" This is

a muliplte

line Text""")

Three lines will be printed

Concatenation

Math in python uses not only numbers but also strings using an alien called Concatenation. See the example below

print("Ethial" + "Hacking" + Python)

It also works with numbers!!

Strings containing numbers are added as strings instead of integers.

print("56+"6)

NOTE:Don't try adding a string to a number.

Though we can add strings to numbers, we can multiply by them.

print(3 * "7")

This will print 777

Python Varibles

Varibles are "container" that allow us to store values by assigning them names, the name can be used to refer to the value later on the program.

For example , In Data science, you can use a varibale to store for examples number of people in a company.

Let us assign a string "Ethical hacking" to a varible called course

courese = "Ethical hacking"

Note that to assign a varible we use one equal sign

Rules of varibles

It is pretty straight forward how to write variables: using letter, number or underscore. But you cannot start with a special character/sympol or a number

Python is a case-sensitive language therefore, Lastname is different from lastnames

Cases of writing varibles:

Snake Case

Each word is separated by underscore character.We write varibles as follows:

first_name = "Marco"

Camel Case

Each word, except the first, starts with a capital letter

myFirstName = "Doe"

Pascal Case

Each word starts with a capital letter

MyFirstName = "Doe"

Please read on how varibles casting and how they can receive user's input

A function is a block of code which which only runs when it is called

We can pass date know as paramaters into function

A function returns data as a result

Creating a Function

In python a function is defined using the def keyword

Calling a Function

Use the name of the function followed by parenthesis:

def myFunction():

print( "Hello from a function" )

Arguments

Information can be passed into functions as Arguments

Argumentsare specified after the function name, inside the parenthesis. You can add as many arguments as you want,just separate them with a comma

The following example has a function with one argument name. When the function is called,we pass along a firstname , which is used inside the function to print the full name:

def myFunction(name):

print( name + "Refsnes")

myFunction("Max")

myFunction("Simel")

I have not written everything about Functions because I just wanted to give you an overview of python Programming language

Lists are in-built data types in Python. They are used to store multiple elements which are ordered and each element correspond to an index. Lists are mutable(changeable). Lists are created with square brackets []. Look at the example below:

#List can be empty

letters = []

#List with items

myList = [“john”, “Allan”, “Ian”]

As we said earlier that ach element correspond to an index, therefore we can access each element with an index like

myList = [“john”, “Allan”, “Ian”]

print(myList[1])

You can run the code and confirm with me that the second element “Allan” will be printed. The first element in the list has an index of 0(zero).

Operations that we can perform on list:

We can use the in and not in python operators to check if an element part of the list. Look at the code below

words = ['eggs', 'spam', 'sausage', 'beaf']

print('spam' in words)

print('tomato' in words)

Note: The len() function can be used to return the number of elements in a list.

Looping through a list

You iterate through a list and check each element. I will use the for loop in our example

#lists in python

cars = ["volvo", "Toyota", "Mazda"]

for car in cars:

print(car)

Lists Functions

Lists support the following functions:

append()

we use append() function to add an item to the end of the list. For example we can add “Benz” to the end of our cars by simply doing

cars.append("Benz")

insert()

This function is used to add an item to a list at a given index. Like insert(index, item).Note that the index comes first…

cars.index(2,"V8") #this adds an item at the third position...

When you add an item to a list with existing elements , the rest are pushed to the right.

pop()

pop() removes an item at a given index

for example using our cars list:

cars = ["volvo", "Toyota", "Mazda"]

cars.pop(2)

#this removes "Mazda" which is at index 2

remove()

remove() removes a specified item like in the example below

cars = ["volvo", "Toyota", "Mazda"]

cars.remove("volvo")

count()

We can use this to returns a count of how many times an item occurs in the list. e.g., run the code below and share with me on the comments section how many times the term “python” occurs…

languages = ["python", "JS", "Go", "python", "C++", "Java", "python"] print(languages.count("python"))

Try running the code and understand how they work. Feel free to always share with me and ask questions

numbers = [2,4,6,8,20]

print(numbers.reverse())

print(numbers.sort())

print(min(numbers))

print(max(numbers))

#Note that the sort() function sorts the list in ascending order by default but you can specify by using reverse=True to sort by descending

Note: The sort() and reverse() change the list they are called on…

~Please read on Lists comprehensions.

Thank you for reading

You follow me and subscribe for more articles on python are loading... You can’t afford to miss☺️

You can use Reference

Happy Hacking!!