Basics of Python
Basics of Python
1. Hello, World!
The classic first program. In Python, you can print text to the console with the
print() function:
python
Copy code
print("Hello, World!")
In Python, you don’t need to declare the data type of a variable. You can assign values
directly.
python
Copy code
name = "Alice" # String
age = 25 # Integer
height = 5.8 # Float
is_student = True # Boolean
4. Operators
python
Copy code
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Adds "orange" to the list
6. Conditionals
python
Copy code
if age >= 18:
print("You are an adult.")
elif age > 13:
print("You are a teenager.")
else:
print("You are a child.")
7. Loops
python
Copy code
for fruit in fruits:
print(fruit)
python
Copy code
count = 0
while count < 5:
print(count)
count += 1
8. Functions
python
Copy code
def greet(name):
return f"Hello, {name}!"
Python is an object-oriented language, so you can create your own data types
(classes).
python
Copy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"My name is {self.name} and I am {self.age} years
old."
Python has many built-in modules that you can import, such as math for mathematical
functions.
python
Copy code
import math
print(math.sqrt(16)) # Output: 4.0
These are the core fundamentals of Python, and mastering these will give you a solid
foundation. From here, you can start working on small projects to practice your skills. Let me
know if you want more details on any specific topic!