DEV Community

Cover image for Ultimate Python Beginner's Guide.
Mercy wanyonyi
Mercy wanyonyi

Posted on • Updated on

Ultimate Python Beginner's Guide.

Everything you need to write your first python code.

Image description

Looking at lines of code as a begginer can really scare one off into thinking that programing is hard. Truth is it's neither here or there. Programing is simply the process of converting High-level language, that is easily understood by a human, to a low-level language that a computer can execute.

The good news is that Python is very easy to learn of most of the languages. Even a child can code in python. Apart from that, it has a lot of applications that you might find interesting. In this article, we will learn all the basic concepts of Python in a very simple way that will help you write your first code!

Advantages of Python

First of all you may want to know some advantages of Python:

  1. It can work on differnt platforms, ie Wimdoss, Mac, Pi Linux ect.
  2. It is object oriented
  3. It has a lot of libraries
  4. it has a wide range of application
  5. It is easy to use,

Applications of Python

You can apply Python in areas like;

  • Web Development

  • AI and Machine Learning

  • Game development

  • Data Science

  • Audiou and Visual applications

  • Software Development

Getting Started

You first need to set up a working environment for Python by following the steps below;

1.Download and install Python from their page. Make sure you dowmload a version that is compatible with your OS.

2.Choose a code editor that you will work with and install it. I recommend using Visual Studio Code. You can download it here.

3.Confirm whether your python is installed by typing the code below on your Command Prompt.

python --version
Enter fullscreen mode Exit fullscreen mode

Basics of Python

Variables

Variables store information that can be used in one's program. In technical terms, the variable is a memory location assigned some value called a literal. For instance

Variable
a=5
b="Mathew"
Enter fullscreen mode Exit fullscreen mode

In a=5 a is the varible ,5 is the literal while in b= "Mathew" b is the variable, Mathew is the literal

Data Types

In the example above, there are two types of data used, that is an integer 5, which is a whole number and a string "Mathew".
In python we have several data types;

  • Numbers

Integers: Whole numbers 1,3,5,20

Float: Numbers with decimals 0.5, 45.6

Complex : Numbers with Imaginery parts 2+ 5i

  • String

A string is an immutable collection of alphabets, characters or words usually denoted with either double or single quotation marks.

a= "python"
   Print(a)

OUTPUT
<python>
Enter fullscreen mode Exit fullscreen mode
  • Boolean

There are two boolean values, True and False. They are used to show the truth values of an expression in a code.
For example

a=10
b=5
  Print (b>a)

OUTPUT
>>> False
Enter fullscreen mode Exit fullscreen mode

Identifiyer

These are names assigned to a variable, class, function or module in python.
NB. Identifiers are case sensitive.

Amount= 122
name= "Mwaviki"
Enter fullscreen mode Exit fullscreen mode

Amount and name are identifiyers in the example above.

Data Structures

These are specific ways in which data is organized in a block of code for effective operations. We have;

  • Lists

A list in python is a group of items in [] separated by commas ,. One can manitpulate the list by adding, removing or displaying specific items on the list using the commands shown below.

MyList= [1,2,3,4,5,6,7]

#Adding to the beginning of thelist
MyList[0]= 0                         OUTPUT 
                                      [0,1,2,3,4,5,6,7]
#Adding at the end
MyList.append(8)                      OUTPUT
                                      [1,2,3,4,5,6,7,8]
#Removing an item
MyList.remove(3)                       OUTPUT
                                      [1,2,4,5,6,7]

Enter fullscreen mode Exit fullscreen mode
  • Tuples

These are ordered 'immutable lists'. The contents are inside a () unlike in a list[]. With Tuples, one can only display items or delete the whole tuple.

MyTuple= (" Joy"," Grace","Jon"," Macy")
  Print (MyTuple[0:2])

#OUTPUT
("Joy","Grace")

Enter fullscreen mode Exit fullscreen mode
  • Sets

In Python a set is an unordered and unchangable collection of data. It can contain several items repreating themselves but will be omitted on printing the set. The items are enclosed in a {}

Myset= {20,20,35,50,45,80,50,95}
Print(Myset)

OUTPUT
{45,35,50,20,80,95}
Enter fullscreen mode Exit fullscreen mode

NB: One can add or remove items from a set.

  • Dictionary

Dictionaries are used to store data values which are paired with key values as shown below

MyDict={
           "Name":'James'
           "Age":34
           "Location":"Kenya"
           }
Print(MyDict["Age"])

OUTPUT
34
Enter fullscreen mode Exit fullscreen mode

Operator

  • Arithmetic

Addition 2+5___________7

Substruction 3-1
____________ 2
Multiplication 2*3_____________6
Division 10/5
_________2
Exponential 2**3
__________16
Floor division 11//4
__________2
Modulous 11%5
______________1

Assignment: Used to assign values to variables. They include:
= 5=5
+= x+=3_______________ x+3
-= x-=2_______________ x-2
*= x*=3_______________x*3
/= x/=1_______________x/1
**= x**=2______________ x**2
//= x//=4______________x//4
%=
x%=5_______________x%5
&=

Comparison: used to compare values
<____________less than
>
_________greater than
>=
________greater or equal to
<=
________less than or equal to
==
___________ same/equals ro
!=______________ not same/equal to

Bitwise:Used to write numbers in binary
`&_____________and
|
___________OR
<<
_____________ a<<b shit bits of a to the left by b bits

_____________a>>b shift bits of a to the right by b units
~
___________NOT
^`
______________XOR

Identity: used to determine whether two objects are the same.
is, is not

Logical: Used to combine conditional statements.
and
or
not

Functions

There are two types of functions in Python

  • Built-in Functions These are functions that aready exist in Python and have a specific purpose. There are more that 50 built in functions with the mostly used beind such as Print,Input etc.
Print("Hello World")

OUTPUT
Hello World

name= Input(" What is your name?: )
Print("Hi {name}")

OUTPUT
What is your age?: John(as an input) 
Hi John

Enter fullscreen mode Exit fullscreen mode
  • Calling a function

Other functions in python are a block of code that runs only when called upon. To define a function one starts with def functionname followed with parameters in the function (parameters) as shown in the example below.
A function must return some results whenever is called upon.

#The function has nothing to return
def MyFunc()
      return           

#Function is called  
MyFunc()  


def My2Func() 
  Print( "This is my function")


#Function is called
My2Func()

#OUTPUT
This is my function

Enter fullscreen mode Exit fullscreen mode

Comments

You can write comments in your code to help you organise your work in two ways. Comments would be skipped when the code is being run.

  • Single line comment: putting a # before the line

#this is my comments

Block comment: Using three double quotation make at the start and end of code

"""a= "Commenting a block of code"
   Print(a)
"""
Enter fullscreen mode Exit fullscreen mode
  1. Loops and Condition statements
  • For loops This loop is used to iterate over a sequence once for an item according the the set statements. For instance
Numb= [1,5,10,]
For x in Numb
 Print (X)

OUTPUT
X:5
>>5

Enter fullscreen mode Exit fullscreen mode
  • While loop This loop is will run the set statements until a certain condition is met.
n=0

While n <= 10
  Print (n)
n+=1

#OUTPUT
(0,1,2,3,4,5,6,7,8,9,10)

Enter fullscreen mode Exit fullscreen mode
  • If statement
a=15
b=10

If a>b
  Print ("a is greater than b")

Enter fullscreen mode Exit fullscreen mode
  • If...else
a= "Rain"

If a
 print ("Stay")
else:
  Print("Go")

Enter fullscreen mode Exit fullscreen mode
  • elif

If the prevision condition is not met then move to the next one.

a=15
b=10

if b>a
  print ("b is greater than a")
elif b<a
  print ("b is less than a")

Enter fullscreen mode Exit fullscreen mode

Conclusion

By now you shoul be able to write your first Python Code and so much more. You can go through the PEP-8 guide to get more guidance on how best to write your python code.

Top comments (0)