Uploaded by Vimlendra Singh

PYTHON Introduction notes

advertisement
PYTHON: DATA TYPES AND DATA STRUCTURES
Intro:
Python is widely renowned for its simplicity and exceptional readability, positioning itself as
a versatile and powerful programming language. Its broad range of applications
encompasses web development, data analysis, and machine learning, among others. The
extensive collection of libraries and the strong support from the Python community make it
an excellent choice for both novice and seasoned programmers.
Basic Variable Types
The basic types of variables in Python are: strings, integers, floating point numbers and
booleans.
Strings in python are identified as a contiguous set of characters represented in either single
quotes (' ') or double quotes (" ").
Example:
my_string1 = 'Welcome to'
my_string2 = "Study Pool"
print(my_string1 + ' ' + my_string2)
[out]: Welcome to Study Pool
Integers in Python are whole numbers without decimal places. They can be stored in
variables and used for counting, indexing, and mathematical operations. Example:
my_int = 10
print(my_int)
[out]: 10
print(type(my_int))
[out]: type 'int'
The built-in function int() can convert a string into an integer.
my_string = "100"
print(type(my_string))
[out]: type 'str'
my_int = int(my_string)
print(type(my_int))
[out]: type 'int'
A floating point number, or a float, is a real number in mathematics. In Python we need to
include a value after a decimal point to define it as a float.
my_float = 1.0
print(type(my_float))
[out]: type 'float'
my_int = 1
print(type(my_int))
[out]: type 'int'
As you can see above, if we don't include a decimal value, the variable would be defined as an
integer. The built-in function float() can convert a string or an integer into a float.
my_string = "100"
my_float = float(my_string)
print(type(my_float))
[out]: type 'float'
A boolean, or bool, is a binary variable. Its value can only be True or False. It is useful when
we do some logic operations, which would be covered in our next chapter.
my_bool = False
print(my_bool)
[out]: False
print(type(my_bool))
[out]: type 'bool'
Basic Math Operations
The basic math operators in python are demonstrated below:
print(f"Addition {1+1}")
print(f"Subtraction {5-2}")
print(f"Multiplication {2*3}")
print(f"Division {10/2}")
print(f"Exponent {2**3}")
[out]:
Addition 2
Subtraction 3
Multiplication 6
Division 5
Exponent 8
Data Collections
Lists in Python are an ordered and mutable data structure used to store multiple items in a
single variable. They can contain elements with different data types. Lists allow elements to
be easily modified, added, and removed.
fruits = ["apple", "banana", "orange", "grape"]
print(fruits)
# Output: ['apple', 'banana', 'orange', 'grape']
print(fruits[0])
# Output: apple
print(fruits[2])
# Output: orange
fruits.append("kiwi")
print(fruits)
# Output: ['apple', 'banana', 'orange', 'grape', 'kiwi']
If you wish to add or remove an element from a list, you can use
the append() and remove() methods for lists as follows:
# Create an empty list
numbers = []
# Append elements to the list
numbers.append(10)
numbers.append(20)
numbers.append(30)
numbers.append(40)
print(numbers)
# Output: [10, 20, 30, 40]
# Remove an element from the list
numbers.remove(30)
print(numbers)
# Output: [10, 20, 40]
In the example above, we start by creating an empty list called numbers. We use the
append() method to add elements (10, 20, 30, and 40) to the list. Then, we print the list to
verify its contents. Next, we use the remove() method to remove the element 30 from the list.
After removing it, we print the modified list, which now contains [10, 20, 40].
Tuple: Ordered and immutable collection of elements. Defined with parentheses ( ).
Example: person = ("John", 25, "USA")
Set: Unordered collection of unique elements. Defined with curly braces { } or set().
Example:
fruits = {"apple", "banana", "orange"}
Dictionary: Unordered collection of key-value pairs. Defined with curly braces { } and colons
:.
Example:
student = {"name": "John", "age": 20, "country": "USA"}
Common String Operations
Concatenation (+): Combines two or more strings together. Example:
greeting = "Hello" name = "John" message = greeting + " " + name print(message)
#Output: Hello John
Length (len()): Returns the number of characters in a string. Example:
text = "Hello, world!" length = len(text) print(length)
#Output: 13
Accessing Characters by Index: Allows you to access individual characters in a string using
their index positions. Example:
text = "Python" first_char = text[0] print(first_char)
#Output: P
Slicing: Extracts a portion of a string by specifying start and end indices. Example:
text = "Hello, world!" sliced_text = text[7:12] print(sliced_text)
# Output: world
Changing Case: Converts the case of a string, either to uppercase or lowercase. Example:
message = "Hello, World!" lowercase_message = message.lower()
uppercase_message = message.upper() print(lowercase_message)
# Output: hello, world!
print(uppercase_message)
# Output: HELLO, WORLD!
Finding Substrings (find()): Searches for a specific substring within a string and returns the
index of the first occurrence. Example:
text = "Hello, world!" index = text.find("world") print(index)
# Output: 7
Download