Python / Python Basics
Introduction to Python
Python is a widely used high-level, general-purpose programming language. It was created by Guido van Rossum in 1991. This tutorial will introduce you to the basics of Python prog…
Section overview
5 resourcesIntroduces fundamental Python concepts, including syntax, variables, and data types.
Introduction
Goal of the Tutorial
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.
What You Will Learn
In this tutorial, you will learn:
- How to set up the Python development environment on your computer
- The basics of Python syntax
- How to work with variables, loops, and conditional statements in Python
- How to write functions in Python
- How to handle errors in Python
Prerequisites
No prior programming knowledge is needed for this tutorial. All you need is a computer and an internet connection to download the necessary software.
Step-by-Step Guide
Python Development Environment
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
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.
Python Variables
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 Loops
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 Conditional Statements
Python supports the usual logical conditions in mathematics:
- Equals:
a == b - Not Equals:
a != b - Less than:
a < b - Less than or equal to:
a <= b - Greater than:
a > b - Greater than or equal to:
a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
Code Examples
Python Variables
# 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 Loops
# 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 Conditional Statements
# Python if statement example
a = 33
b = 200
if b > a:
print("b is greater than a")
# outputs: b is greater than a
Summary
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.
Practice Exercises
- Write a Python program to add two numbers.
- Write a Python program that prints all the numbers from 0 to 10 using a for loop.
- Write a Python program that prints the factorial of a number. The number should be input from the user.
Solution
# 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)
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article