basic python tutorial with example
Basic python tutorial with example
Python is a versatile and beginner-friendly programming language used for various purposes such as web development, data analysis, artificial intelligence, and more.
1. Print Statement:
Let's start with a simple program to print "Hello, World!" to the console.
```python
print("Hello, World!")
```
2. Variables and Data Types:
Python has several built-in data types such as integers, floats, strings, lists, tuples, and dictionaries. Here's an example of using variables and different data types:
```python
# Integer variable
num1 = 10
# Float variable
num2 = 3.14
# String variable
name = "Alice"
# List variable
my_list = [1, 2, 3, 4, 5]
# Tuple variable
my_tuple = (1, 2, 3)
# Dictionary variable
my_dict = {'key1': 'value1', 'key2': 'value2'}
print(num1)
print(num2)
print(name)
print(my_list)
print(my_tuple)
print(my_dict)
```
3. Basic Arithmetic Operations:
Python supports all basic arithmetic operations like addition, subtraction, multiplication, division, and modulo.
```python
a = 10
b = 5
# Addition
result_addition = a + b
print("Addition:", result_addition)
# Subtraction
result_subtraction = a - b
print("Subtraction:", result_subtraction)
# Multiplication
result_multiplication = a * b
print("Multiplication:", result_multiplication)
# Division
result_division = a / b
print("Division:", result_division)
# Modulo
result_modulo = a % b
print("Modulo:", result_modulo)
```
4. Conditional Statements (if, elif, else):
Conditional statements allow you to execute certain blocks of code based on certain conditions.
```python
age = 20
if age < 18:
print("You are a minor.")
elif age >= 18 and age < 65:
print("You are an adult.")
else:
print("You are a senior citizen.")
```
5. **Loops (for loop, while loop)**: Loops allow you to execute a block of code repeatedly.
```python
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
```
6. Functions:
Functions are reusable blocks of code that perform a specific task.
```python
def greet(name):
print("Hello,", name)
greet("Alice")
```
This is just a basic introduction to Python. There's a lot more to learn, including object-oriented programming, file handling, exception handling, and libraries like NumPy, Pandas, Matplotlib, and TensorFlow for specific tasks. Practice and explore more to enhance your Python skills!
Comments
Post a Comment