0% found this document useful (0 votes)
6 views2 pages

Python Basics: Variables, Data Types, Functions

The document provides a quick overview of basic Python concepts, including variables, data types, collections, function definitions, conditionals, loops, and module importing. It explains how variables reference objects, lists, tuples, and dictionaries are used for collections, and how to define and call functions. Additionally, it covers conditional statements and the use of for and while loops, as well as how to import modules like 'random'.

Uploaded by

klejda elezaj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python Basics: Variables, Data Types, Functions

The document provides a quick overview of basic Python concepts, including variables, data types, collections, function definitions, conditionals, loops, and module importing. It explains how variables reference objects, lists, tuples, and dictionaries are used for collections, and how to define and call functions. Additionally, it covers conditional statements and the use of for and while loops, as well as how to import modules like 'random'.

Uploaded by

klejda elezaj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Quick Review of Basic Concepts in Python

----------------------------------------------------------------------------------------------------------------------

1) Variables
A variable in Python is a symbolic name that is a reference or pointer to an object. Once an
object is assigned to a variable, you can refer to the object by that name. Variables do not
require explicit declaration.

Example:
x=5 # integer
name = 'Alice' # string variable

2) Data Types
Python has several built-in data types for different kinds of values:
- int: Integer numbers (e.g., 10, -5)
- float: Floating-point numbers (e.g., 3.14, -2.7)
- str: Strings (e.g., 'hello', 'Python')
- bool: Boolean values (True or False)
- NoneType: Represents the absence of a value (None)

3) Collections
Python provides several collection data types to store multiple values in a single variable:
- List: An ordered, mutable collection of items. [1, 2, 3]
- Tuple: An ordered, immutable collection of items. (1, 2, 3)
- Dictionary: A collection of key-value pairs. {'name': 'Alice', 'age': 25}

4) Function Definition
Functions in Python are defined using the def keyword. They help organize code into
reusable blocks.

Example 1:
def greet(name):
print(f"Hello, {name}!")

Call: greet('Alice')

Example 2:

def add(a, b):


return a + b

Call: result = add(1, 2)


5) if-else Conditionals
Conditional statements are used to execute code based on certain conditions.

Example:
x = 10
"
if x > 0:
print('Positive number')
else:
print('Non-positive number')

6) for loops + while loops


- for loop: Used to iterate over a sequence (like a list or string).

Example:
for i in range(5):
print(i)

- while loop: Repeats as long as a condition is true.

Example:
count = 0
while count < 5:
print(count)
count += 1

7) Module Importing and Usage


Modules are external Python files or libraries that can be imported and used within your
program. The import keyword is used to include them.

Example with the random module:

import random
print([Link](1, 10)) # Generates a random number between 1 and 10.

----------------------------------------------------------------------------------------------------------------------

You might also like