With the massive popularity of personal computers and their usage by people of all areas, its inevitable that people who aren’t primarily programmers will sooner or later need to do at least some programming. With that in mind, python was made: A language with focus on being easy to understand, human-friendly to read and easily integrated into anything!
What is Python?
Python is a high-level, versatile, and easy-to-learn programming language. It was created by Guido van Rossum and first released in 1991. Python is designed to be human-readable and focuses on code readability, which means its syntax is designed to be clear and understandable, resembling natural language as much as possible.
Python is widely used for various purposes such as web development, scientific research, data analysis, artificial intelligence, automation, and more. It has a large and active community that contributes to its growth and development, resulting in a wealth of libraries, frameworks, and tools that make it even more powerful and versatile.
The language’s simplicity and elegance make it a popular choice for beginners learning to program, while its capabilities and community support also make it a top choice for experienced developers working on complex projects.
How does it work?
Imagine Python as a friendly assistant who can understand and follow your instructions, just like a recipe. You give Python a recipe (your code), and it uses its “interpreter” to read and execute each step one by one. The interpreter is like a chef who reads the recipe and prepares the dish. It reads your code line by line and carries out the tasks you’ve described. If it finds any mistakes in the recipe, it stops and tells you what’s wrong so you can fix it before the cooking begins.
Now, the cool thing is that you don’t need to turn the whole recipe (your entire code) into a final dish (a complete program) all at once. Python’s interpreter lets you try out parts of the recipe in a special kitchen (the interactive mode). You can experiment with small steps before cooking the entire meal. This makes it easier to test and learn as you go. So, Python’s interpreter is like your cooking buddy, understanding and following your instructions to create programs step by step.
Installing Python:
To use Python, you need to install its interpreter on your computer.
If you’re on Windows, you can download the latest version from the official Python website (https://www.python.org/downloads/). It’s as simple as Download > Next Next Next > Finish. On Linux, you can either download it from your distributions App Store or by running sudo apt-get install python
. On MacOS, you can install it the same way you would on Windows(Same link).
Your First Python Program:
Open a text editor (like Notepad or VS Code) and type the following:
print("Hello, Python!")
Save the file with a .py
extension, like hello.py
. Open your terminal or command prompt, navigate to the folder where you saved the file, and type python hello.py
. You’ll see “Hello, Python!” printed on the screen.
Now, to explain: Python programs are basically scripts that are run by the python interpreter. By default, they have no self-running package(like a .exe) nor are they compiled(unless you use something like py2exe). Python scripts are saved using the extension .py as a way to tell your computers, code editor and the interpreter that its dealing with such.
Variables and Data Types:
Variables are like containers for storing data. Python has different data types:
int
for integers (whole numbers)float
for floating-point numbers (decimal numbers)str
for strings (text)bool
for booleans (True or False)
This however, doesn’t mean you need to tell the interpreter that. In python, variables are dynamic, which means once you assign a value to them, that becomes their type(until you assign something else). Example:
age = 25 # Age is an INT
height = 5.8 # Height is a Float
name = "Alice" # Name is an String
is_student = True # Is_Student is a Boolean
Basic Operations:
Python can perform math operations:
+
addition-
subtraction*
multiplication/
division%
modulus (remainder after division)
Example:
x = 10
y = 3
result = x + y # result will be 13
The +
operation also works with strings, as a way to put two or more strings together. For example:
begin="I am "
age="18"
end=" years old"
print(begin + age + end) # prints "I am 18 years old"
User Input:
You can get input(From a terminal/command prompt) from the user using the input()
function:
name = input("What's your name? ")
print("Hello, " + name + "!")
Conditional Statements:
You can make decisions in your code using if
, elif
(else if), and else
statements:
age = int(input("How old are you? "))
if age < 18:
print("You're a minor.")
elif age >= 18 and age < 65:
print("You're an adult.")
else:
print("You're a senior citizen.")
As you may notice here, in python(in contrast with most other languages) you don’t use { }
to begin and end a block, you use tabs.
Loops:
Loops help you repeat actions, iterate through lists, etc. for
and while
loops are the most common.
for i in range(5):
print(i) # This will print numbers 0 to 4
count = 0
while count < 5:
print(count)
count += 1
Lists:
A list is a collection of items(Other languages sometimes call this an Array). It’s created with square brackets.
fruits = ["apple", "banana", "orange"]
print(fruits[0]) # This prints "apple"
Functions:
Functions are reusable blocks of code. You can define your own functions.
def greet(name):
print("Hello, " + name + "!")
greet("Bob") # This will print "Hello, Bob!"
These are the very basics of Python programming. From here, you can explore more advanced topics like classes, file handling, and libraries, depending on what you want to achieve. Happy coding!