Variables
In Python, you can store values in variables. Variables are nothing but names given to a location in memory where a value can be stored.
- Example:
x = 5 (stores the integer value 5 in a variable named x) - Print the value using
print(x) which will output: 5
Data Types
Python has several built-in data types:
- Integers (int): whole numbers, e.g., 1, 2, 3, etc.
* Example:
x = 10
- Floats (float): decimal numbers, e.g., 3.14, -0.5, etc.
* Example:
y = 3.14
- Strings (str): sequences of characters, e.g., "hello", 'goodbye', etc.
* Example:
greeting = "Hello, World!" or
greeting = 'Hello, World!'
- Boolean (bool): true or false values
* Example:
x = True
Operators
Python has various operators for performing arithmetic, comparison, logical, and assignment operations:
- Arithmetic Operators:
* Addition:
a + b
* Subtraction:
a - b
Multiplication: a b
* Division:
a / b (returns a float)
* Floor Division:
a // b (returns an integer)
- Comparison Operators:
* Equal to:
a == b
* Not equal to:
a != b
* Greater than:
a > b
* Less than:
a < b
- Logical Operators:
* AND:
a and b
* OR:
a or b
- Assignment Operators:
* Assign:
x = 5
* Add and assign:
x += 5
Control Structures
Control structures determine the flow of a program's execution:
- Conditional statements (if-else):
* If statement:
if x > 10: print("x is greater than 10")
- Loops:
* For loop:
for i in range(5): print(i)
* While loop:
i = 0; while i < 5: print(i); i += 1
Functions
A function is a block of code that can be executed multiple times:
- Defining a function:
def greet(name): print("Hello, " + name) - Calling a function:
greet("John")
These are the basic concepts in Python programming. This summary should give you a good starting point to learn more about the language!