Python Tutorial 2 Data Types

Python Tutorial  Data Types

what is datatypes in python with Example?

 

 In Python, data types are a classification of values that determine the kind of operations that can be performed on them. Python has several built-in data types, and here are some common ones along with examples:

Integers (int):

Whole numbers without a decimal point.

x = 5

y = -10

Floats (float):

 

Numbers with a decimal point or numbers written in scientific notation.

a = 3.14

b = 2.0

Strings (str):

Sequence of characters enclosed in single or double quotes.

name = “John”

message = ‘Hello, World!’

Booleans (bool):

Represents truth values True or False.

is_valid = True

has_error = False

Lists (list):

Ordered and mutable collection of items.

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

names = [‘Alice’, ‘Bob’, ‘Charlie’]

Tuples (tuple):

Ordered and immutable collection of items.

coordinates = (10, 20)

colors = (‘red’, ‘green’, ‘blue’)

Dictionaries (dict):

Unordered collection of key-value pairs.

person = {‘name’: ‘Alice’, ‘age’: 25, ‘city’: ‘New York’}

Sets (set):

Unordered collection of unique elements.

fruits = {‘apple’, ‘banana’, ‘orange’}

NoneType (None):

Represents the absence of a value or a null value.

result = None

These are some of the basic data types in Python. Keep in mind that Python is dynamically typed, meaning you don’t need to explicitly declare the data type of a variable; the interpreter infers it based on the assigned value.

Python string with Example

Strings in Python are sequences of characters, and they can be created using single quotes (‘) or double quotes (“). Here are some examples of working with strings in Python:

Creating Strings:

string1 = ‘Hello, World!’

string2 = “Python is awesome!”

Accessing Characters in a String:

message = “Hello, Python!”

print(message[0])  # Output: ‘H’

print(message[7])  # Output: ‘P’

Slicing Strings:

message = “Python Programming”

print(message[0:6])   # Output: ‘Python’

print(message[7:])    # Output: ‘Programming’

print(message[:6])    # Output: ‘Python’

String Concatenation:

first_name = “John”

last_name = “Doe”

full_name = first_name + ” ” + last_name

print(full_name)  # Output: ‘John Doe’

String Length:

message = “Python”

length = len(message)

print(length)  # Output: 6

String Methods:

text = ”   Python is easy to learn.   “

print(text.lower())       # Output: ‘   python is easy to learn.   ‘

print(text.upper())       # Output: ‘   PYTHON IS EASY TO LEARN.   ‘

print(text.strip())       # Output: ‘Python is easy to learn.’

print(text.replace(‘Python’, ‘Java’))  # Output: ‘   Java is easy to learn.   ‘

String Formatting:

name = “Alice”

age = 30

message = “My name is {} and I am {} years old.”.format(name, age)

print(message)

# Output: ‘My name is Alice and I am 30 years old.’

F-Strings (formatted string literals) – Python 3.6 and later:

name = “Bob”

age = 25

message = f”My name is {name} and I am {age} years old.”

print(message)

# Output: ‘My name is Bob and I am 25 years old.’

These examples cover some of the basic operations and manipulations you can perform with strings in Python. Strings in Python are versatile, and there are many more methods and operations available for working with them.

Python Integer with example

Integers in Python are whole numbers without a decimal point. They can be positive or negative. Here are some examples of working with integers in Python:

Creating Integers:

x = 5

y = -10

Basic Arithmetic Operations:

a = 10

b = 3

# Addition

sum_result = a + b  # Result: 13

# Subtraction

difference = a – b  # Result: 7

# Multiplication

product = a * b  # Result: 30

# Division

quotient = a / b  # Result: 3.3333…

# Floor Division (rounded down to the nearest whole number)

floor_result = a // b  # Result: 3

# Modulo (remainder of the division)

remainder = a % b  # Result: 1

Exponentiation:

base = 2

exponent = 3

result = base ** exponent  # Result: 8

Type Conversion:

num_str = “25”

num_int = int(num_str)  # Convert string to integer

Integer Operations:

c = 7

# Absolute Value

absolute_value = abs(c)  # Result: 7

# Integer Division

integer_division = c // 2  # Result: 3

# Modulo (remainder of the division)

remainder = c % 2  # Result: 1

# Power

power_result = pow(c, 2)  # Result: 49

Comparisons:

p = 15

q = 20

# Equality

is_equal = p == q  # Result: False

# Inequality

not_equal = p != q  # Result: True

# Greater than

greater_than = p > q  # Result: False

# Less than or equal to

less_than_or_equal = p <= q  # Result: True

These examples cover some of the basic operations and manipulations you can perform with integers in Python. Integers are a fundamental data type used in various mathematical and programming contexts.

what is float in python with example

In Python, a float is a data type that represents decimal numbers or numbers with a fractional component. Here are some examples of working with floats in Python:

Creating Floats:

a = 3.14

b = -2.5

Basic Arithmetic Operations:

x = 5.0

y = 2.0

# Addition

sum_result = x + y  # Result: 7.0

# Subtraction

difference = x – y  # Result: 3.0

# Multiplication

product = x * y  # Result: 10.0

# Division

quotient = x / y  # Result: 2.5

Type Conversion:

num_int = 10

num_float = float(num_int)  # Convert integer to float

Scientific Notation:

scientific_notation = 2.5e3  # Represents 2.5 * 10^3

Comparison:

p = 3.5

q = 2.0

# Equality

is_equal = p == q  # Result: False

# Inequality

not_equal = p != q  # Result: True

# Greater than

greater_than = p > q  # Result: True

# Less than or equal to

less_than_or_equal = p <= q  # Result: False

Math Functions:

import math

pi_value = math.pi  # Result: 3.141592653589793

# Other mathematical functions

sqrt_result = math.sqrt(9.0)  # Result: 3.0

sin_result = math.sin(math.radians(30))  # Result: 0.49999999999999994

These examples cover some of the basic operations and manipulations you can perform with floats in Python. Floats are commonly used for representing real numbers and performing calculations involving decimal values. It’s important to be aware of potential precision issues when working with floating-point numbers due to the limitations of computer hardware in representing certain decimal values accurately.

What is complex data types in python with example

 

In Python, complex numbers are represented by the complex data type. A complex number is expressed in the form a + bj, where a and b are real numbers, and j is the imaginary unit (equal to the square root of -1). Here’s an example of working with complex numbers in Python:

Creating Complex Numbers:

z1 = 2 + 3j

z2 = -1 + 0.5j

Basic Operations:

# Addition

sum_result = z1 + z2  # Result: (1+3.5j)

# Subtraction

difference = z1 – z2  # Result: (3+2.5j)

# Multiplication

product = z1 * z2  # Result: (-1.5+1j)

# Division

quotient = z1 / z2  # Result: (-2-4j)

Accessing Real and Imaginary Parts:

real_part = z1.real  # Result: 2.0

imag_part = z1.imag  # Result: 3.0

Conjugate:

conjugate_result = z1.conjugate()  # Result: (2-3j)

Magnitude and Phase:

magnitude = abs(z1)  # Result: 3.605551275463989

phase = cmath.phase(z1)  # Result: 0.982793723247329

Using the cmath Module:

The cmath module in Python provides additional functions for complex number operations.

import cmath

sqrt_result = cmath.sqrt(-1)  # Result: 1j

Complex numbers are used in various scientific and engineering applications, especially in fields like signal processing and control systems. Python’s support for complex numbers makes it a versatile language for mathematical and scientific computing.

Leave a Comment

%d bloggers like this: