Python Basics
Overview
Python is a high-level, interpreted programming language known for its simplicity and readability. This document covers the basics of Python programming, including syntax, data types, and control structures.
Hello World
The classic first program in any language is to print "Hello, World!" to the console.
Variables and Data Types
Variables
Variables store data that can be used and manipulated in your programs.
Example
Data Types
Python supports several data types, including integers, floats, strings, and booleans.
- Integer:
int
- Float:
float
- String:
str
- Boolean:
bool
Example
integer_num = 10
float_num = 10.5
string_value = "Hello"
boolean_value = True
print(type(integer_num)) # Output: <class 'int'>
print(type(float_num)) # Output: <class 'float'>
print(type(string_value)) # Output: <class 'str'>
print(type(boolean_value))# Output: <class 'bool'>
Operators
Arithmetic Operators
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Modulus:
%
- Exponentiation:
**
- Floor Division:
//
Example
a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333...
print(a % b) # Output: 1
print(a ** b) # Output: 1000
print(a // b) # Output: 3
Comparison Operators
- Equal to:
==
- Not equal to:
!=
- Greater than:
>
- Less than:
<
- Greater than or equal to:
>=
- Less than or equal to:
<=
Example
x = 10
y = 20
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: False
print(x < y) # Output: True
print(x >= y) # Output: False
print(x <= y) # Output: True
Control Structures
Conditional Statements
Use if
, elif
, and else
for decision-making.
x = 10
if x > 0:
print("x is positive")
elif x == 0:
print("x is zero")
else:
print("x is negative")
Example
Loops
for
Loop
Iterate over a sequence (e.g., list, tuple, string).
Example
while
Loop
Repeat while a condition is true.
Example
Functions
Functions group reusable code. Use the def
keyword to define a function.
Example
Lists
Lists store multiple items in a single variable. Lists are ordered and mutable.
Example
Dictionaries
Dictionaries store key-value pairs. They are unordered and mutable.
Example
Summary
This document covers fundamental Python concepts, including variables, data types, operators, control structures, functions, lists, and dictionaries. Python’s simplicity and readability make it a great choice for both beginners and experienced developers. For more in-depth information, refer to the official Python documentation.