Introduction
- Python is a high-level, interpreted, and general-purpose programming language. It is widely used in various fields, including data analysis, machine learning, artificial intelligence, web development, game development, scientific computing, and many more.
- Python was created by Guido van Rossum and first released in 1991. In this article, we will cover the basics of Python, including syntax, data types, control structures, functions, and modules.
Syntax
Python's syntax is simple and easy to learn. Here are a few basic examples of Python code:
# Printing a message to the console
print("Hello, World!")
# Assigning a value to a variable
x = 42
# Using a loop to iterate over a list of numbers
for i in range(10):
print(i)
# Defining a function
def add(x, y):
return x + y
# Using a conditional statement to check if a number is even
if x % 2 == 0:
print("x is even")
else:
print("x is odd")
- Python code is typically organized into blocks using whitespace, such as indentation. This makes Python code easy to read and understand.
Data Types
- Python supports several built-in data types, including numbers, strings, lists, tuples, sets, and dictionaries.
Numbers
- Python supports both integer and floating-point numbers. Here are a few examples:
x = 42 # integer
y = 3.14 # floating-point
z = 4 + 3j # complex number
print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
print(type(z)) # <class 'complex'>
Strings
- Strings in Python are enclosed in either single quotes or double quotes. Here are a few examples:
s1 = 'Hello, World!'
s2 = "Python is awesome"
print(s1) # Hello, World!
print(s2) # Python is awesome
Lists
- Lists in Python are ordered collections of objects. Here are a few examples:
my_list = [1, 2, 3, 4, 5] # list of integers
my_list2 = ['apple', 'banana', 'orange'] # list of strings
my_list3 = [1, 'apple', 3.14] # mixed list
print(my_list) # [1, 2, 3, 4, 5]
print(my_list2) # ['apple', 'banana', 'orange']
print(my_list3) # [1, 'apple', 3.14]
Tuples
- Tuples are similar to lists but are immutable, meaning their values cannot be changed once they are created. Here are a few examples:
my_tuple = (1, 2, 3, 4, 5) # tuple of integers
my_tuple2 = ('apple', 'banana', 'orange') # tuple of strings
my_tuple3 = (1, 'apple', 3.14) # mixed tuple
print(my_tuple) # (1, 2, 3, 4, 5)
print(my_tuple2) # ('apple', 'banana', 'orange')
print(my_tuple3) # (1, 'apple', 3.14)
Sets
- Sets in Python are unordered collections of unique objects. Here are a few examples:
- Sets in Python are collections of unique and unordered elements. They are represented using curly braces {} and each element is separated by a comma. Here's an example of creating a set:
my_set = {1, 2, 3, 4, 5}
print(my_set)
Output:
{1, 2, 3, 4, 5}
- In the above example, we created a set called my_set containing five elements 1, 2, 3, 4, and 5.
Set Operations
- Python provides several built-in operations that can be performed on sets.
Adding Elements to a Set
- You can add elements to a set using the add() method. Here's an example:
my_set = {1, 2, 3, 4, 5}
my_set.add(6)
print(my_set)
Output:
{1, 2, 3, 4, 5, 6}
- In the above example, we added an element 6 to the set my_set using the add() method.
- You can also add multiple elements to a set using the update() method. Here's an example:
my_set = {1, 2, 3, 4, 5}
my_set.update([6, 7, 8])
print(my_set)
Output:
{1, 2, 3, 4, 5, 6, 7, 8}
Removing Elements from a Set
You can remove an element from a set using the remove() method. Here's an example:
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set)
Output:
{1, 2, 4, 5}
- In the above example, we removed the element 3 from the set my_set using the remove() method.
- You can also remove an element from a set using the discard() method. The difference between remove() and discard() is that remove() raises an error if the element is not found in the set, whereas discard() does not raise an error. Here's an example:
my_set = {1, 2, 3, 4, 5}
my_set.discard(3)
print(my_set)
Output:
{1, 2, 4, 5}
- In the above example, we removed the element 3 from the set my_set using the discard() method.
- You can also remove the last element from a set using the pop() method. Since sets are unordered, the last element can be any element. Here's an example:
my_set = {1, 2, 3, 4, 5}
x = my_set.pop()
print(x)
print(my_set)
Output:
1
{2, 3, 4, 5}
- In the above example, we removed the last element from the set my_set using the pop() method.
- You can also clear all elements from a set using the clear() method. Here's an example:
my_set = {1, 2, 3, 4, 5}
my_set.clear()
print(my_set)
Output:
set()
Some examples of Python code:
Hello World
The classic "Hello, World!" program in Python:
print("Hello, World!")
Variables
Declaring and using variables in Python:
# Declare a variable
x = 5
# Print the variable
print(x)
# Change the value of the variable
x = 10
# Print the variable again
print(x)
Conditional Statements
Using if/else statements to make decisions:
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Loops
Using loops to repeat code:
For loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
While loop
i = 1
while i < 6:
print(i)
i += 1
Functions
Defining and calling functions:
def my_function(name):
print("Hello, " + name)
my_function("John")
Lists
Using lists to store multiple values:
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
print(fruits[1])
print(fruits[2])
# Change the value of an element
fruits[1] = "kiwi"
# Add an element to the list
fruits.append("orange")
# Remove an element from the list
fruits.remove("cherry")
print(fruits)
Dictionaries
Using dictionaries to store key-value pairs:
person = {
"name": "John",
"age": 30,
"country": "USA"
}
print(person["name"])
print(person["age"])
print(person["country"])
# Change the value of a key
person["age"] = 40
# Add a new key-value pair
person["city"] = "New York"
# Remove a key-value pair
del person["country"]
print(person)
- These are just a few examples of Python code. There are many more concepts and techniques you can learn in Python, depending on what you want to accomplish with the language.

0 Comments