This tutorial aims to introduce you to the basics of Python programming. By the end of this tutorial, you will have a solid foundation in Python, and you will be comfortable with the Python development environment and essential Python syntax.
In this tutorial, you will learn:
No prior programming knowledge is needed for this tutorial. All you need is a computer and an internet connection to download the necessary software.
To start coding in Python, you need to set up the Python development environment on your computer. This involves installing Python and a code editor or Integrated Development Environment (IDE).
You can download Python from the official Python website.
For the IDE, there are many options available, but for beginners, Thonny or PyCharm is recommended.
Python syntax refers to the set of rules that define how a Python program will be written. Python was designed to be very readable, which makes it a great language for beginners.
Variables in Python are created the moment you assign a value to it. For example:
name = "John"
age = 25
In this example, name
and age
are variables.
Python has two types of loops - for
and while
.
A for
loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
A while
loop can execute a set of statements as long as a condition is true.
i = 1
while i < 6:
print(i)
i += 1
Python supports the usual logical conditions in mathematics:
a == b
a != b
a < b
a <= b
a > b
a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
# This is a comment
# Comments in Python start with the hash symbol
# creating variables in Python
name = "John" # a string variable
age = 25 # an integer variable
# printing variables
print(name) # outputs: John
print(age) # outputs: 25
# Python for loop example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# outputs:
# apple
# banana
# cherry
# Python while loop example
i = 1
while i < 6:
print(i)
i += 1
# outputs:
# 1
# 2
# 3
# 4
# 5
# Python if statement example
a = 33
b = 200
if b > a:
print("b is greater than a")
# outputs: b is greater than a
In this tutorial, you have learned how to set up the Python development environment, the basics of Python syntax, how to work with variables, loops, and conditional statements in Python.
For further learning, you can explore more complex Python concepts such as functions, error handling, and file I/O.
# program to add two numbers
num1 = 10
num2 = 20
sum = num1 + num2
print("The sum is", sum) # outputs: The sum is 30
# program to print numbers from 0 to 10
for i in range(11):
print(i)
# program to print the factorial of a number
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)