Python for Machine Learning day-01
Hi everyone!
I’m excited to announce that I’m starting a new series on Medium called Machine Learning Days. In this series, I’ll be sharing what I’m learning about machine learning on a daily basis. I hope you’ll join me on this journey!
Today, we’re going to talk about the basics of programming in Python. Python is a powerful language that’s often used for machine learning. If you don’t have any prior experience with Python, don’t worry! I’ll be starting from the very basics.
We’ll cover the following topics:
- How to install Python
- How to write simple Python programs
- The basics of data types and variables
- Control flow statements
- Functions
How to install Python
- Go to the Python website: https://www.python.org/downloads/ and click on the Download Python button.
- Choose the version of Python that you want to install. For most people, the latest version is the best option.
- Click on the Windows installer (64-bit) or Windows installer (32-bit) button, depending on your system architecture.
- Run the installer and follow the on-screen instructions.
- Once the installation is complete, you can start using Python.
Here are some additional instructions:
- If you are using a Mac or Linux computer, you can install Python using the package manager.
- You can also install Python using a virtual environment. This is useful if you want to have different versions of Python installed on your computer.
- To verify that Python is installed correctly, open a command prompt and type
python
. You should see the Python interpreter start up.
Here are some resources that you may find helpful:
- How to Install Python: https://www.python.org/downloads/
- Python Tutorial: https://docs.python.org/3/tutorial/
- Python for Beginners: https://www.learnpython.org/
How to write simple Python programs
1. Hello, world!
This is the simplest Python program. It prints the message “Hello, world!” to the console.
print("Hello, world!")
output : Hello, world
2. Adding two numbers
This program adds two numbers and prints the result.
first_number = 10
second_number = 20
sum = first_number + second_number
print("The sum is", sum)
output : The sum is 30
3. Finding the factorial of a number
This program finds the factorial of a number. The factorial of a number is the product of all the positive integers less than or equal to that number. For example, the factorial of 5 is 120 (1 * 2 * 3 * 4 * 5).
def factorial(number):
if number == 0:
return 1
else:
return number * factorial(number - 1)
number = 5
factorial_of_number = factorial(number)
print("The factorial of", number, "is", factorial_of_number)
Output: The factorial of 5 is 120
4. Checking if a number is prime
This program checks if a number is prime. A prime number is a number that is divisible only by itself and 1.
def is_prime(number):
if number <= 1:
return False
for i in range(2, number):
if number % i == 0:
return False
return True
number = 17
is_prime_number = is_prime(number)
print("The number", number, "is", "prime" if is_prime_number else "not prime")
Output: The number 17 is prime
So, these are some basic programs that you can practice. I will also provide you with resources where you can practice more for Day 01.
Now, let’s move on to the next topic, which is basic data types and variables in Python. This is an important topic, but it is also very easy to understand. Let’s work on it!
The basics of data types and variables
1. Integers
Integers are whole numbers, such as 1, 2, 3, 4, etc. They can be positive, negative, or zero.
integer_1 = 1
integer_2 = -2
integer_3 = 0
print(integer_1)
print(integer_2)
print(integer_3)
Output : 1 -2 0
2. Floating-point numbers
Floating-point numbers are numbers with decimal points, such as 1.0, 2.5, 3.14159. They can be positive, negative, or zero.
floating_point_number_1 = 1.0
floating_point_number_2 = -2.5
floating_point_number_3 = 3.14159
print(floating_point_number_1)
print(floating_point_number_2)
print(floating_point_number_3)
Output: 1.0 | -2.5 | 3.14159
3. Strings
Strings are sequences of characters, such as “hello”, “world”, “Python”. They can be enclosed in single quotes or double quotes.
string_1 = "hello"
string_2 = "world"
string_3 = "Python"
print(string_1)
print(string_2)
print(string_3)
Output: hello | world | Python
4. Booleans
Booleans are values that can be either True or False. They are used to represent logical values.
boolean_1 = True
boolean_2 = False
print(boolean_1)
print(boolean_2)
Output: True | False
5. Variables
Variables are names that are used to store data. They can be assigned to any data type.
integer_variable = 1
floating_point_variable = 1.0
string_variable = "hello"
boolean_variable = True
print(integer_variable)
print(floating_point_variable)
print(string_variable)
print(boolean_variable)
Output: 1 | 1.0 | hello |True
6. Type conversion
Type conversion is the process of converting a value from one data type to another. For example, you can convert an integer to a floating-point number or a string to a boolean value.
integer_value = 1
floating_point_value = float(integer_value)
string_value = "1"
boolean_value = bool(string_value)
print(floating_point_value)
print(boolean_value)
Output : 1.0 | True
7. Operators
Operators are symbols that are used to perform operations on data. For example, the addition operator (+) is used to add two numbers together.
integer_1 = 1
integer_2 = 2
sum = integer_1 + integer_2
print(sum)
Output: 3
Control flow statements
Control flow statements are used to control the flow of execution of a program. For example, the if statement is used to execute code if a certain condition is met.
integer_value = 1
if integer_value == 1:
print("The value is 1")
else:
print("The value is not 1")
Output : The value is 1
Functions
Functions are reusable blocks of code that can be called from other parts of a program. They can be used to perform a variety of tasks, such as calculating a factorial or finding the maximum value in a list.
def factorial(number):
if number == 0:
return 1
else:
return number * factorial(number - 1)
number = 5
factorial_of_number = factorial(number)
print("The factorial of", number, "is", factorial_of_number)
Output: The factorial of 5 is 120
Input and output
Input and output are the processes of getting data into and out of a program. Input is the process of reading data from the user or from a file. Output is the process of writing data to the user or to a file.
integer_value = input("Enter an integer: ")
print("You entered", integer_value)
Output Enter an integer: 3 | You entered 3
Great! We’ve covered the basics of Python in Day 01. On Day 02, we’ll be covering data structures in Python. See you then!
I’ll also provide some resources for learning more about Python.
I hope you enjoy this first installment of Machine Learning Days!
Here are some additional resources that you may find helpful:
- Python for Beginners: https://www.learnpython.org/
- Python Tutorial: https://docs.python.org/3/tutorial/
- PyCharm: https://www.jetbrains.com/pycharm/
Thanks for reading!