a-peek-to-python-language
a-peek-to-python-language
A Quick peek to Python programming language
Table of Contents:
1. Introduction to Python
2. Installing Python
3. Running Python Code
4. Python Syntax Basics
Variables
Data Types
Input and Output
Comments
5. Control Flow
if Statements
Loops
6. Functions
7. Lists and Dictionaries
8. Working with Files
9. Libraries and Packages
10. Conclusion
1. Introduction to Python
Python is a powerful and easy-to-learn programming language. It is known for its simplicity and readability, making it a
great choice for beginners.
2. Installing Python
1. Windows/MacOS: Download the latest version of Python from python.org. The installation package includes
everything you need to get started.
2. Linux: Python is typically pre-installed. You can verify by running python3 --version in the terminal.
3. Running Python Code
You can run Python code in two main ways:
1. Interactive mode: Open a terminal and type python3 or python , depending on your system, and you will enter
the Python interpreter.
2. Script mode: Write Python code in a file (e.g., example.py ), and run it in the terminal with python3 example.py .
4. Python Syntax Basics
1/4
a-peek-to-python-language
Variables
In Python, you can store values in variables. You don’t need to specify the data type when creating them.
name = "John"
age = 25
pi = 3.14
Data Types
Some common data types in Python are:
int : Whole numbers (e.g., 10, -3)
float : Decimal numbers (e.g., 3.14, -1.0)
str : Strings (e.g., "hello")
bool : Boolean (True or False)
Input and Output
Use the input() function to take user input and print() to display output.
name = input("What is your name? ")
print(f"Hello, {name}!")
Comments
Use # for single-line comments and ''' ''' or """ """ for multi-line comments.
# This is a single-line comment
"""
This is a multi-line comment
that spans multiple lines.
"""
5. Control Flow
if Statements
Control the flow of your program using conditional statements.
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are not an adult.")
Loops
2/4
a-peek-to-python-language
for loop: Iterate over a sequence (like a list or string).
for i in range(5):
print(i)
while loop: Repeats as long as a condition is true.
count = 0
while count < 5:
print(count)
count += 1
6. Functions
Functions allow you to reuse code by encapsulating logic into callable blocks.
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
You can also return values from functions:
def add(a, b):
return a + b
result = add(3, 4)
print(result)
7. Lists and Dictionaries
Lists
A list is an ordered collection of items.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
fruits.append("orange") # Add an item
Dictionaries
A dictionary stores key-value pairs.
person = {"name": "John", "age": 30}
print(person["name"]) # Output: John
person["age"] = 31 # Update a value
8. Working with Files
3/4
a-peek-to-python-language
Python makes it easy to work with files. Here's how to read and write files:
Reading a file:
with open('file.txt', 'r') as file:
content = file.read()
print(content)
Writing to a file:
with open('file.txt', 'w') as file:
file.write("Hello, world!")
9. Libraries and Packages
Python has a rich ecosystem of libraries. Use pip to install packages from the Python Package Index (PyPI).
Installing a package:
pip install requests
Using the package:
import requests
response = requests.get("https://api.github.com")
print(response.status_code)
10. Conclusion
Congratulations! You've learned the basics of Python. You now know how to:
Write basic Python programs
Use variables, loops, and functions
Work with lists and dictionaries
Handle files
Use external libraries
Continue practicing and exploring more advanced topics like object-oriented programming, data science, web
development, and more!
Reference
https://maduranga.com/a-peek-to-python-language
4/4