Basics of Python
Tags: machinelearning, ai, python, learnwithmitul
Published: 2022-11-23 17:11:17

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5sxkgnrg6dh08ukxf45w.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yt9bbe6jv5gczttbkg7u.png)
But you can concatenate a string with another string value. That's called string concatenation.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0gtwamt62wvtwqf4eal.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyubtozhrvrj1bm6ewbz.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obefdi4ayrrj2thx33rw.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp8bvjaw5fk258hq2yh1.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z45r1thbjstvbhextje3.png)

**Dictionary**

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1nxiackx0wc4n75h5y9.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gm72vwwtg2gmykbsk174.png)

**set**
Removes all the duplicate items from the list.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8k69exygkb4oxknj7wr.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlqdm8m355mjzh1e2qqk.png)
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh5j4izybqk6hrrjuzd9.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hf2kw3fenmegifx9ckz.png)
**List Compression**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkui3pwhiqs47uealhsl.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60e3qft127rptd0xqjum.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvbmn7a3z0ohgg11w7aq.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5z3v2rmapmedjk5uay6.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjsnvu05lgmy6r8h1ykf.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oz7rryf4j7kan3il8153.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0viz5jgoqhk5vfj34jyx.png) 

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rx40k6f8ogpf0i5s0mv6.png)