DEV Community

Cover image for Basics of Python
Zaynul Abedin Miah
Zaynul Abedin Miah

Posted on • Updated on

Basics of Python

Numbers
When taking numbers in Python, you must be careful to determine whether they are floating or integer. In Python, an input function is normally in strings, so you must convert it to an integer to accept an integer number or a float to accept both float and integer numbers. If the input has integer parenthesis outside of it, you must use an integer as input rather than a float or string. Otherwise, it will display an error message.

Python Arithmatic Operators

Python has the following basic arithmetic operators: floor division, addition, multiplication, division, and subtraction. Floor division returns an integer, while normal division returns a floating value.

Image description
Some of the basic operators

Strings
Strings can be a letter, word, sentences or a number. if the string is in number it has single/double inverted comma or it is converted to string using str() function. normally an input takes string value. The difference between strings and numbers is that you cannot add a string value with an integer value or integer to string.

Image description
But you can concatenate a string with another string value. That's called string concatenation.

Image description

Print Formatting
There are multiple ways of print formatting but I would suggest you to use .format method as it is little more clear when you go back and you don't have to worry about messing up the format inside of the format parenthesis.

Image description

Lists
List is a collection of data like letters, word or number. List is mutable means you can change the value inside it or add a value inside it.

Image description
You can also use list slicing where you can limit the values of list and you can also reverse a list using negetive indexing. Negetive indexing starts from where the list has ended.

Image description
when you want to store a specific index from a nested list inside another list you can use multiple list indexing to grab that specific item. (Remember list index always starts from 0 and a nested list inside another list is considered a single item on that list.

Image description

Dictionary

A {key: value} pair is called a dictionary.

Image description

Tuple
A very similar concept of list but it uses parenthesis bracket instead of square bracket and it is Immutable where list is mutable means you can change a value of list but in tuple you can't do the same.

Image description

set
Removes all the duplicate items from the list.

Image description

for loop
You can use for loop when you have to print multiple values instead of writing print multiple time you can run a for loop on that list and print the variable which contains the number of iterations in this case it is i.

Image description
while loop

You can use while loop also but difference between while loop and for loop is that you have to increment the count value during each iterations or else it won't work.

Image description

range
It is similar to slicing concept. Difference is it has parenthesis bracket and comma and also range. Range objects are not iterators but are iterables. So slicing a range() function does not return an iterator but returns an iterable instead.

Image description
List Compression
Image description

Instead of writing so many lines you can just use list compression to get an output by just using a single line.

Image description

Image description

Functions
Instead of writing multiple repeated lines of code you can just use a def function to define a function and use that function to avoid repeatations.

Image description

Image description

Map function
when you have created a defined a function you can just use map function on a specific list and covert it into a list to return the mapped list.

Image description

Lambda
It is similar to functions instead of using multiple lines to return a value you can just use lambda function and than store it on a variable. More traditional functions in Python are created for use at a later point. Lambda functions are created for use just at the moment. And whereas a list comprehension outputs only lists, Lambda functions can be used to output lists or values. One can even use the more normal functions (where the function is defined) within Lambda functions.

Image description

filter
It is same as lamba but it returns a boolean value than you can pass it onto a iterable object and when you call a list on it it returns values which satisfy the condition.

Image description

Top comments (0)