0% found this document useful (0 votes)
15 views7 pages

All Python in One

This document provides a comprehensive overview of Python commands and fundamentals, including how to run scripts, declare variables, and utilize data types. It covers control flow, functions, file handling, error handling, and various operations on strings and lists. Additionally, it highlights important concepts such as indentation, dynamic typing, and the use of the Python standard library.

Uploaded by

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

All Python in One

This document provides a comprehensive overview of Python commands and fundamentals, including how to run scripts, declare variables, and utilize data types. It covers control flow, functions, file handling, error handling, and various operations on strings and lists. Additionally, it highlights important concepts such as indentation, dynamic typing, and the use of the Python standard library.

Uploaded by

hamdanmkhanuae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

ALL PYTHON COMMANDS AND FUNDEMENTALS

### Commands:

1. **Running Python:**

- To run a Python script: `python script.py`

- For interactive mode: `python`

2. **Variables:**

- Declare a variable: `variable_name = value`

3. **Data Types:**

- Integer: `42`

- Float: `3.14`

- String: `"Hello"`

- List: `my_list = [1, 2, 3]`

- Tuple: `my_tuple = (1, 2, 3)`

- Dictionary: `my_dict = {'key': 'value'}`

4. **Print:**

- Print to the console: `print("Hello, Python!")`

5. **Control Flow:**

- If statement:

```python

if condition:

# code

```

- For loop:

```python

for item in iterable:

# code

```
6. **Functions:**

- Define a function:

```python

def my_function(parameters):

# code

```

7. **File Handling:**

- Open and read a file:

```python

with open('filename.txt', 'r') as file:

content = file.read()

```

8. **Importing Modules:**

- Import a module:

```python

import module_name

```

9. **Error Handling:**

- Try-except block:

```python

try:

# code

except Exception as e:

print(f"An error occurred: {e}")

```

10. **Basic Math Operations:**

- Addition: `+`
- Subtraction: `-`

- Multiplication: `*`

- Division: `/`

- Modulus: `%`

### Facts and Concepts:

1. **Indentation:**

- Python uses indentation for block structure, not braces.

2. **Dynamic Typing:**

- Python is dynamically typed, meaning you don't need to declare variable types explicitly.

3. **Comments:**

- Single-line comment: `#`

- Multi-line comment: `''' This is a multi-line comment '''`

4. **Whitespace Matters:**

- Be mindful of indentation and whitespace; it affects code blocks.

5. **Python Standard Library:**

- Python comes with a rich standard library; explore it for additional functionality.

6. **Virtual Environments:**

- Create a virtual environment to manage project dependencies:

```bash

python -m venv venv

```

7. **List Comprehensions:**

- A concise way to create lists:

```python
squares = [x**2 for x in range(10)]

```

8. **Slicing:**

- Efficiently extract portions of lists or strings:

```python

my_list[1:4] # Returns elements at index 1 to 3

```

9. **Duck Typing:**

- "If it looks like a duck and quacks like a duck, it's a duck." Python emphasizes object behavior over types.

10. **Community and Resources:**

- Engage with the Python community on platforms like Stack Overflow. Explore resources like the Python
documentation and online tutorials.

1. **Input:**

- Get user input and assign it to a variable:

```python

user_input = input("Enter something: ")

```

### String Operations:

2. **String Concatenation:**

- Combine strings:

```python

string1 = "Hello"

string2 = "World"

result = string1 + " " + string2

```
3. **String Formatting:**

- Format strings using placeholders:

```python

name = "John"

age = 25

message = f"My name is {name} and I am {age} years old."

```

4. **String Methods:**

- Manipulate strings using methods:

```python

my_string = "Hello, World!"

length = len(my_string)

uppercase = my_string.upper()

```

### Lists:

5. **List Operations:**

- Append to a list:

```python

my_list = [1, 2, 3]

my_list.append(4)

```

6. **List Methods:**

- Use list methods:

```python

numbers = [3, 1, 4, 1, 5, 9, 2]

numbers.sort()

```
### Loops:

7. **While Loop:**

- Execute code while a condition is true:

```python

i=0

while i < 5:

print(i)

i += 1

```

8. **Break and Continue:**

- Control loop execution with `break` and `continue`:

```python

for i in range(10):

if i == 3:

break

if i == 5:

continue

print(i)

```

### Miscellaneous:

9. **Boolean Operations:**

- Combine conditions using `and`, `or`, `not`:

```python

if x > 0 and y < 10:

# code

```

10. **Ternary Operator:**


- Concise conditional expressions:

```python

result = x if x > 0 else -x

```

11. **Type Conversion:**

- Convert between data types:

```python

num_str = "42"

num = int(num_str)

```

12. **Random Module:**

- Generate random numbers:

```python

import random

random_number = random.randint(1, 100)

```

Remember to adapt these commands to the specific requirements of your programs. Experimenting with these
commands and concepts will help solidify your understanding of Python. Happy coding!

You might also like