Python Basics for Beginners

Written by Manikanta|8 min read|
105 Views

What is Python?

Python is a high level object oriented programming language and it is an interpreted language in which the code is executed by line by line.

Python is very easy to learn compared to any other computer programming languages as it is easy to understand the syntax and implementing it.

Keywords in python :- 

There are some reserved words by python which cannot be used as identifiers and those words are known as keywords.

Identifiers in python:- 

Identifiers are the names given to store any value. Simply identifiers are nothing but names.

Rules for writing Identifiers:-

  1. Identifiers can have any character like (A-Z), (a-z), (0-9), and ‘_’.
  2. It cannot have any special symbols other than ‘_’
  3. It cannot be a keyword.

Variables in Python :-

Variables are the named locations to store in the memory. If we want to store anything it will be stored in the memory.

a = 100

Here is a variable. However, we can reassign the value of a variable  like redeclare variable to new value 

a = 200

Now the value of a is 200 which is redeclared again and the updated value is stored.

DataTypes in Python:- 

There are different types of data types in python like

1. Numbers :-

Python number data type is used to store integers, float and etc.

number1 = 10

number2  = 20.23

Here number1 is ‘integer’ and number2 is ‘float’

You no need to specify the datatype python implicitly converts into the particular data type based on the value assigned to it.

2. Stings :-

Strings in python are represented by single quotes or double quotes.

fruit = ‘apple’

bird = ‘parrot’

Here these both are strings as they are inside the quotes.

3. Lists :- 

Lists are used to store the collection of elements rather than the single element.

And lists can contain any type of elements.

List are mutable objects which means we can change the values in the list.

Lists are denoted by ‘[ ]’

list1 = [1, 2, 3]

Slicing in list :

Slicing is used to print the whole list or from a particular position to the particular position

print(list1[0:])

It prints all the values from 0 index

print(list1[1:])

It prints the list starting from first index.

Inbuilt functions for list :-

  1. Append :- 

We can add one item at the end of the list using append function

2. Extend :-

We can add multiple values at a time using extend function in python

3. del :-

To delete the particular element in the list

del list1[0] -> deletes the first element in the list

4. clear :-

To delete the whole list in python we use clear function.

5. sort :-

To sort the elements in the list

6. reverse :-

To reverse the elements in the list

7. max :-

To find the maximum element in the list

8. min :-

To find the minimum element in the list.

List1 is the list of integers however list elements can be any of the type.

We can access the elements of the lists using index and the index starts from 0 to n-1 where n is the length of the list.

If you want to access the first element in list1 we use

list1[0]

Remember the last index is the length of the list minus 1

4. Tuples:-

Tuples is another data type in Python which are denoted by ‘()’. And tuples are immutable, which means we can not modify the values.

Tuples can be of any types like integers, strings and any other type also.

The main difference between the lists and tuples is that lists are mutable and tuples are immutable.

Tuples are basically used when we do not want to change the data.

tuple1  = (1,2,”abc”)

tuple1 = (25, 35, 20)

print(“tuple elements: “, tuple1)

print(“type is: “, type(tuple1))

print(“max ele in tuple is: “, max(tuple1))

print(“counting of 25 in tuple: “, tuple1.count(25))

print(“finding index: “, tuple1.index(35))

tuple1  is a tuple  

5 . Dictionaries :- 

Dictionaries are another data type in Python which are in the form of key value pair where the keys should be unique and the values can be duplicated.

We can access the values through keys in dictionaries.  

We seperate the keys by ‘ , ‘.

dict1  = {“a” : 2, “b” :45}

6. Sets :-

Sets are another data type in Python in which it cannot allow duplicate elements.

set1  = set([1, 2, 3])

If else in python :-

Python supports conditional statements. These are used to check for particular conditions. 

Ex :- 

If else in python 

a = 11

If a>10:

print(“a is greater than 10)

Else:

print(“a is not greater than 10”)

Here first it checks the condition if it is correct if the case is executed else the else block is executed.

Remember only one block is executed and the other will not executed.

And indentation is very important in python It states about the block of the code.

Elif :- This is used in checking the multiple conditions in a if-else.

This should be written after if block and before the else block in the programme.

Looping Statements in Python :- 

Python supports looping statements. These are very important for executing more than once.

Suppose if you want to execute a piece of code for number of times we will use Looping statements like for loop, while loop etc.
For loop in Python :-
For loop is a kind of looping statement in python.
Ex:- 
for i in range(0,5,1):

print(i)

This loop prints the values from 0 to 4 

The first argument in the range function is the initial value for the loop to start and the second value is the end value and the third value is incremental value to increment after every iteration of a loop.

The default incremental value is 1 we can set it to our own values as for our requirements.

At the end of the loop, there should be a colon. And after there must be an indentation to say python these are inside the for a loop.

While loop :-

While loop is also used to execute a code for a certain number of times.

a = 10

while a>0:

print(a)

a = a – 1

Here it prints the values from 10 to 1 as the loop starts from 10 and ends when it should be 0

See in every iteration the value of a is decrementing by 1

Example Programme in Python to find a number is divisible by 2 or not 

num = int(input(“enter a number”))

If num % 2 == 0:

print(“It is divisible by 2”)

else:

print(“Number is not divisible by 2”)

Here we are using an input function to take the input from the user.

By default the input function will be string type so we are converting it into integer type for our requirements.

Programme to print the even numbers in the list:

list1 = [1,2,3,4,5]

for values in list1:

if values % 2 == 0:

     print(values)

Break and Continue in python :-

Break statement is used to break the programme from the certain position. If we use break the portion after the break statement will not be executed.

Example :-

numbers = [10, 20, 30, 40]

print(“Using break “)

for item in numbers:

if item == 40:

     break

print(item)

Continue is to skip for one iteration If we use continue one iteration is skipped and remaining will be executed.

print(“Using Continue”)

another_numbers = [12, 13, 45, 58, 89]

for item in another_numbers:

if item == 45:

     continue

print(item)

Here 45 will not be printed

Python Programme to check a number is prime or not :- 

t = int(input(“How many numbers to be checked: “))

while t != 0:

n = int(input(“Enter the number: “))

if n > 1:

     for i in range(2, n):

         if n % i == 0:

             print(f'{n} is not prime’)

             break

     else:

         print(f'{n} is prime’)

else:

     print(‘not prime’)

t = t-1

Python programme to print the fibonacci series :-

a = 0

b = 1

choice = int(input(“enter how many digits u want”))-2

print(a,b,end=” “)

while choice!=0:

c = a+b

print(c,end=” “)

a = b

b = c

choice = choice-1

Loading...