Python is a high-level, interpreted programming language known for its simplicity and readability, making it an excellent choice for beginners and experienced programmers alike. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python's extensive standard library, combined with its comprehensive ecosystem of third-party packages, allows for powerful data analysis, web development, machine learning, and much more.
Data types are an essential concept in Python, as in any programming language. They define the operations that can be done on the data and the storage method for each of them. Understanding data types is crucial because it affects how a programmer manipulates data and how the data is stored and interacted with. Python is dynamically typed, meaning you don't need to declare the type of a variable when you create one. However, knowing the types and how to use them will enable you to write more efficient and error-free code.
Python supports various numeric data types that allow you to perform calculations and represent data in different ways. The main numeric types in Python are integers, floating-point numbers, and complex numbers.
int
): Represents whole numbers, positive or negative, without decimals. E.g., -10, 0, 255.float
): Represents real numbers, can contain fractional parts. E.g., -3.14, 0.0, 2.718.complex
): Numbers with a real and imaginary part, where the imaginary part is denoted by 'j' or 'J'. E.g., 3+4j.Python supports a variety of operations on numeric types, including but not limited to:
+
): Adds two numbers.-
): Subtracts the second number from the first.*
): Multiplies two numbers./
): Divides the first number by the second. The result is always a float
.//
): Divides the first number by the second and rounds down to the nearest whole number.%
): Returns the remainder of dividing the first number by the second.# Addition
print(10 + 5) # 15
# Subtraction
print(10 - 5) # 5
# Multiplication
print(10 * 5) # 50
# Division
print(10 / 2) # 5.0
# Floor Division
print(10 // 3) # 3
# Exponentiation
print(2 ** 3) # 8
# Modulus
print(10 % 3) # 1
# Convert float to int
print(int(10.6)) # 10
# Convert int to float
print(float(10)) # 10.0
# Convert to complex
print(complex(10)) # (10+0j)
pi = 3.14
for approximation)These examples and exercises will help solidify your understanding of numeric data types and operations in Python, laying a strong foundation for more complex programming tasks.
Strings in Python are used to store and represent text data. They are immutable sequences of characters, which means once a string is created, the characters within it cannot be changed. However, you can create new strings based on operations or methods applied to the original string.
str
Type'
) or double quotes ("
). Triple quotes ('''
or """
) can be used for multi-line strings.+
operator.*
operator..upper()
: Converts all characters in the string to uppercase..lower()
: Converts all characters in the string to lowercase..find(substring)
: Returns the lowest index in the string where the substring is found..replace(old, new)
: Replaces all occurrences of the old
substring with the new
substring..split(separator)
: Splits the string into a list of strings based on the specified separator.Example 1: Basic String Manipulation
# Creating a string
greeting = "Hello, world!"
# Indexing
first_char = greeting[0] # 'H'
# Slicing
slice_greeting = greeting[0:5] # 'Hello'
# Concatenation
full_greeting = greeting + " It's a beautiful day." # 'Hello, world! It's a beautiful day.'
# Repetition
echo = "echo " * 3 # 'echo echo echo '
print(first_char)
print(slice_greeting)
print(full_greeting)
print(echo)
Example 2: Using String Methods
message = "Welcome to Python Programming!"
# Convert to uppercase
upper_message = message.upper()
# Find substring
index_of_Python = message.find("Python")
# Replace substring
new_message = message.replace("Python", "C++")
# Split the string
words = message.split(" ")
print(upper_message)
print("Index of 'Python':", index_of_Python)
print(new_message)
print(words)
Exercises:
These exercises will help you practice string manipulation and get comfortable with string methods, enhancing your ability to handle text data in Python.
# Python Program to find the area of triangle
a = 5
b = 6
c = 7
# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)