Python beginners guide
Introduction
Python is a versatile, high-level programming language known for its simplicity and readability. It's used in various domains like web development, data analysis, artificial intelligence, and more.
Variables
In Python, variables are created when you assign a value to them:
x = 5
y = 'Hello, World!'
Data Types
Python has a variety of data types such as integers, float, string, list, tuple, and dictionary:
num = 42 # Integer
flt = 42.0 # Float
str_ = 'Hello' # String
list_ = [1, 2, 3] # List
tuple_ = (1, 2, 3) # Tuple
dict_ = {'key': 'value'} # Dictionary
Functions
Functions are defined using the 'def' keyword in Python:
def greet(name):
return 'Hello, ' + name
Conditions and Loops
Python supports conditional statements and various loops:
if x > 10:
print('x is greater than 10')
elif x == 10:
print('x is 10')
else:
print('x is less than 10')
for i in range(5):
print(i)
while x > 0:
print(x)
x -= 1
List Comprehension
Python allows concise ways to create lists:
squared = [x**2 for x in range(10)]
Modules and Packages
Python has a rich ecosystem of libraries. You can import them using the 'import' statement:
import math
print(math.sqrt(16))