0% found this document useful (0 votes)
25 views4 pages

Python Basics for Reviewers

The document provides an overview of Python, highlighting its features such as simplicity, portability, and extensive libraries. It covers basic concepts including variables, data types, decision-making statements, loops, and random number generation. Additionally, it includes syntax examples for various Python constructs.

Uploaded by

Gabriel Sufrir
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)
25 views4 pages

Python Basics for Reviewers

The document provides an overview of Python, highlighting its features such as simplicity, portability, and extensive libraries. It covers basic concepts including variables, data types, decision-making statements, loops, and random number generation. Additionally, it includes syntax examples for various Python constructs.

Uploaded by

Gabriel Sufrir
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

Python Reviewer

# Python Reviewer

## Introduction to Python
- Python is a general-purpose, open-source programming language with a focus on readability.

- Created by Guido Van Rossum in 1989.

- Features:

- Simplicity - easy-to-read syntax

- Portability - runs on Windows, Mac, Linux, etc.

- Huge Libraries - extensive built-in and third-party modules

- Object-Oriented - supports classes and objects

## Python Basics
### 1. Variables & Data Types
- Variables store values and do not need explicit declaration.

- Built-in Data Types:

- Numbers: int, float, complex

- String (str) - Sequence of characters, immutable.

- List - Ordered, mutable collection ([ ]).

- Tuple - Ordered, immutable collection (( )).

- Dictionary - Key-value pairs ({ }).

- Set - Unordered, unique values.

### Type Checking


x=5

print(type(x)) # <class 'int'>

### String Manipulation


s = "Hello"

print(s[0]) # First character

print(s[1:4]) # Slicing

print(s * 2) # Repetition

print(s + " World!") # Concatenation

### List Example


my_list = [10, "hello", 3.14]

print(my_list[1]) # hello

my_list.append(100) # Add element

## 2. Decision-Making Statements
### If Statement
if x > 10:

print("Greater than 10")

### If-Else
if x > 10:

print("Greater than 10")

else:

print("Less than or equal to 10")

### If-Elif-Else
if x > 10:

print("Greater")

elif x == 10:

print("Equal")

else:

print("Smaller")
### Nested If
if x > 10:

if x % 2 == 0:

print("Even and Greater than 10")

### Ternary Operator


result = "Even" if x % 2 == 0 else "Odd"

### Switch Alternative (Using Dictionary)


def switch_case(option):

cases = {1: "One", 2: "Two", 3: "Three"}

return [Link](option, "Invalid")

print(switch_case(2)) # Output: Two

## 3. Loops in Python
### For Loop
for i in range(5): # 0 to 4

print(i)

### While Loop


x=0

while x < 5:

print(x)

x += 1

### Break & Continue


for i in range(10):

if i == 5:

break # Stops loop


print(i)

for i in range(10):

if i == 5:

continue # Skips iteration

print(i)

## 4. Random Numbers
import random

x = [Link](1, 10)

print(x) # Random integer between 1 and 10

y = round([Link](1.5, 10.5), 2)

print(y) # Random float

## Summary
| Concept | Syntax Example |

|----------------------|---------------|

| Variable Assignment | x = 5 |

| Data Types | int, float, list, tuple, dict, set |

| If-Else | if x > 10: |

| Loop (For) | for i in range(5): |

| Loop (While) | while x < 5: |

| List | my_list = [1,2,3] |

| Dictionary | my_dict = {"name": "John"} |

| Random | [Link](1, 10) |

Common questions

Powered by AI

Lists and tuples in Python provide distinct benefits. Lists are mutable, meaning their content can be altered after creation, which makes them suitable for dynamic data structures where data can change. Tuples, being immutable, ensure data integrity by preventing changes, making them ideal for fixed collections of items where the order and content need to be constant .

Python's 'if-else' construct enables branching by executing code blocks based on conditions, allowing complex logical flows. The ternary operator provides a shorthand for 'if-else', facilitating concise expressions for simple conditions, which enhances readability and reduces lines of code when handling straightforward conditional assignments .

Python's portability allows a single codebase to run on multiple platforms such as Windows, Mac, and Linux without modification. This reduces development time and effort, facilitates cross-platform applications, and increases software reach, adaptability, and compatibility, as developers do not need to rewrite code for different operating systems .

Python’s large standard library significantly enhances its usability by providing extensive modules and packages for a wide range of tasks, reducing the need for external dependencies. This comprehensive feature set makes development faster and easier, attracting a large programmer base and contributing to Python's broad adoption across diverse fields from data science to web development .

In Python, variable declaration does not require explicit data type definition as the language is dynamically typed, meaning the type is determined at runtime. This contrasts with statically-typed languages where the type must be specified at compile time. In Python, the 'type()' function can be used to check the type of a variable .

String immutability in Python implies that any alteration results in a new string object. While this ensures data integrity and simplifies memory management, it can affect performance negatively if frequent modifications are needed, as it requires creating multiple string copies. However, techniques like concatenation and slicing are optimized for readability and efficiency despite this constraint .

Python is suitable for general-purpose programming due to its simplicity, making it easy to read and write; portability, allowing it to run on various operating systems like Windows, Mac, and Linux; a huge library ecosystem with both built-in and third-party modules; and support for object-oriented programming through classes and objects .

In Python, a dictionary can substitute switch-case structures found in other languages by mapping cases to corresponding outputs. The 'get()' method can retrieve values for a given key, providing a default if the key doesn't exist, thereby handling multiple conditions in a compact form without needing extensive 'if-else' chains .

The use of 'break' in loops allows for immediate termination of the loop once a condition is met, optimizing processing by not unnecessarily iterating through the loop. 'Continue' enables skipping over certain iterations based on conditions, refining control flow by focusing only on relevant data, thus improving efficiency and logical clarity of loops .

Python implements random number generation through the 'random' module, providing functions like 'randint()' for integers and 'uniform()' for floats. These functions are used in applications like simulations, randomized testing, gaming, and cryptography where generating unpredictable or patternless numbers is essential .

You might also like