Python Datatypes Mastering the Essentials - Complete Guide
- Numbers, Type Conversion and Mathematics In Python
- Type Conversion in Python Program
- Mathematics In Python
- Create a Python List In Python program
- Add Elements to a Python List In Python program
- Python List Length In Python program
- Create a Python Tuple In Python Program
- Tuple Length In Python
¶Numbers, Type Conversion and Mathematics In Python
The number data types are used to depot the numeric values.
Python program supports integers, floating-point numbers and complex numbers. They are defined as int
, float
, and complex
classes in Python program.
-
int
- maintains signed integers of non-limited length. -
float
- maintains floating decimal points and it’s accurate up to 15 decimal places. -
complex
- maintains complex numbers.
¶Numeric Data Type In Python
Integers and floating points are differentiated by the presence or absence of a decimal point. For example,
- 5 is an integer
- 5.42 is a floating-point number.
Complex numbers are written by the form, x + yj
, where x
is real part and y
is the fictitious part.
Now let’s see an example-
num1 = 5
print(num1, 'is of type', type(num1))
num2 = 5.42
print(num2, 'is of type', type(num2))
num3 = 8+2j
print(num3, 'is of type', type(num3))
Output :
5 is of type <class 'int'>
5.42 is of type <class 'float'>
(8+2j) is of type <class 'complex'>
In the upon example, we have maked three variables named num1
, num2
and num3
with values 5, 5.42, and 8+2j
respectively.
We have further used the type()
function to know as class a particular variable belongs to. Since,
-
5 is an integer value,
type()
exchange int as like the class ofnum1
i.e<class 'int'>
-
5.42 is a floating value,
type()
exchange float as like the class ofnum2
i.e<class 'float'>
-
1 + 2j is a complex number,
type()
exchange complex as like the class ofnum3
i.e<class 'complex'>
¶Number Systems In Python
The numbers we have deal with every day are of the decimal (base 10) number system.
But computer programmers need to work with binary (base 2), hexadecimal (base 16) and octal (base 8) number systems.
In Python Programming, we can illustrate these numbers by appropriately placing a symptom before that number. The following table lists these symptom.
There are some examples below
print(0b1101011) # prints 107
print(0xFB + 0b10) # prints 253
print(0o15) # prints 13
¶Type Conversion in Python Program
In python programming, type transformation is the process of converting one type of number into another.
Operations as like addition, subtraction convert integers to float implicitly (automatically), if one of the operands is float.Now For example,
print(1 + 2.0) # prints 3.0
Below, we can see above that 1 (integer) is converted into **1.0 **(float) for addition and the result is also as a floating point number.
num1 = int(2.3)
print(num1) # prints 2
num2 = int(-2.8)
print(num2) # prints -2
num3 = float(5)
print(num3) # prints 5.0
num4 = complex('3+5j')
print(num4) # prints (3 + 5j)
Below, when changeable from float to integer, the number gets headless (decimal parts are removed).
Likewise when changeable from integer to float, .0 is postfixed to the number.
¶Random Module In Python
Python program offers the random
module to propagate random numbers or to pick a random item from an iterator.
First we need to arrival the random
module. For example-
import random
print(random.randrange(10, 20))
list1 = ['a', 'b', 'c', 'd', 'e']
# get random item from list1
print(random.choice(list1))
# Shuffle list1
random.shuffle(list1)
# Print the shuffled list1
print(list1)
# Print random element
print(random.random())
Output:
15
a
['d', 'b', 'c', 'e', 'a']
0.6716121217631744
¶Mathematics In Python
Python Program offers the math
module to conveyance out several mathematics like trigonometry, logarithms, probability and statistics, etc. For example-
import math
print(math.pi)
print(math.cos(math.pi))
print(math.exp(10))
print(math.log10(1000))
print(math.sinh(1))
print(math.factorial(6))
Output:
3.141592653589793
-1.0
22026.465794806718
3.0
1.1752011936438014
720
¶Create a Python List In Python program
We can create a list by placing elements under square brackets []
, differentiated by commas. For example,
# a list of three elements
ages = [19, 26, 29]
print(ages)
# Output: [19, 26, 29]
There, the ages
list has three items.
¶Access List Elements In Python
Every element in a list is connected with a number, known as a** list index**.
The index constantly starts from 0, meaning the first element of a list is at index 0, the second element is at index 1, and so on.
¶Access Elements Using Index In Python
We can use index numbers to access list elements. For example below
languages = ['Python', 'Swift', 'C++']
# access the first element
print(languages[0]) # Python
# access the third element
print(languages[2]) # C++
¶Add Elements to a Python List In Python program
fruits = ['apple', 'banana', 'orange']
print('Original List:', fruits)
# using append method
fruits.append('cherry')
print('Updated List:', fruits)
Output :
Original List: ['apple', 'banana', 'orange']
Updated List: ['apple', 'banana', 'orange', 'cherry']
¶Change List Items In Python
We can alternative the items of a list by assigning new values using the =
operator. For example-
colors = ['Red', 'Black', 'Green']
print('Original List:', colors)
# changing the third item to 'Blue'
colors[2] = 'Blue'
print('Updated List:', colors)
Output:
Original List: ['Red', 'Black', 'Green']
Updated List: ['Red', 'Black', 'Blue']
Below, we have replaced the element at index 2: 'Green'
with 'Blue'
.
¶Remove an Item From a List In Python Program
We can remove an item from a list in python using the remove() method. For example-
numbers = [2,4,7,9]
# remove 4 from the list
numbers.remove(4)
print(numbers)
# Output: [2, 7, 9]
¶Python List Length In Python program
We can use the len() method to search the number of elements present in a list. For example-
cars = ['BMW', 'Mercedes', 'Tesla']
print('Total Elements: ', len(cars))
# Output: Total Elements: 3
¶Iterating Through a List In Python
We can use a for loop to iterate upon the elements of a list. For example-
fruits = ['apple', 'banana', 'orange']
# iterate through the list
for fruit in fruits:
print(fruit)
Output:
apple
banana
orange
¶Create a Python Tuple In Python Program
We make a tuple by placing items under parentheses (). For example-
numbers = (1, 2, -5)
print(numbers)
# Output: (1, 2, -5)
¶Access Tuple Items In Python
Every item in a tuple is connected with a number, known as a tuple index.
The index constantly starts from 0, meaning the first item of a tuple is at index 0, the second item is at index 1, and so on.
¶Access Items Using Index In Python
We can use index numbers to access tuple items. For example-
languages = ('Python', 'Swift', 'C++')
# access the first item
print(languages[0]) # Python
# access the third item
print(languages[2]) # C++
¶Tuple Length In Python
We can use the** len()** function to find the number of items current in a tuple. For example,
cars = ('BMW', 'Tesla', 'Ford', 'Toyota')
print('Total Items:', len(cars))
# Output: Total Items: 4
¶Iterate Through a Tuple In Python
We can use the for loop to iterate upon the items of a tuple. For example-
fruits = ('apple','banana','orange')
# iterate through the tuple
for fruit in fruits:
print(fruit)
Output:
apple
banana
orange
¶Strings In Python
In Python Program, a string is a dilution of characters. For example, "hello"
is a string containing a alternation of characters 'h'
, 'e'
, 'l'
, 'l'
, and 'o'
.
We can use single quotes or double quotes to illustrate a string in Python. For example-
# create a string using double quotes
string1 = "Python programming"
# create a string using single quotes
string1 = 'Python programming'
¶Example: Python String In Python Program
# create string type variables
name = "Python"
print(name)
message = "I love Python."
print(message)
Output:
Python
I love Python.
Python Functions Mastery Your Complete Guide for Success
Python File Handling Essential Tips and Tricks for Mastery
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development