# Variables and Data Types

### What's Variable&#x20;

Variables are containers for storing data values.

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

Variables do not need to be declared with any particular *type*, and can even change type after they have been set.

**Types are only associated with objects of variables**

### Variable Types

**Strings**

```
# define variable
name = "Ayman"
print(name)        
Output: Ayman
```

```
# new line
name = "Ayman\nAyman" 
print(name)        
Output: Ayman
          Ayman
```

```
# print \n in output -raw string-
name = r'Ayman\nAyman'  
print(name)        
Output: Ayman\nAyman
```

```
# unicode string
name = u'Ayman'
# convert unicode to string  
str(name)
# convert string to unicode
unicode(name) 
```

```
# add formating
name="""
Ayman
        Ayman
"""
print(name)
Output:
Ayman
	Ayman
```

#### Numbers

```
x = 1    # int
y = 2.8  # float
z = 1j   # complex
```

### Operation on Variables

#### String Operations

* Strings are immutable&#x20;
* Concatenation\
  `var1 + var2 = 'var1var2'`\
  `var1 + '' +var2 = 'var1 var2'`
* Repeated sequence\
  `buffer = "A"*100`
* Slicing \
  `string[start:end:step]`
* int to string\
  `int("100")`\
  `Output: 100`
* string.find(*value*)
* string.replace(*old\_value, new\_value*)
* and a lot more ... check [manual](https://docs.python.org/3/library/stdtypes.html)

#### Numbers Operations

normal as other languages

`x**y (x power y)`

### String Formatting

#### Option #1: %-formatting

```
name = "Ayman"
age = 22
print("Hello, %s." % name)
Output: 'Hello, Ayman.'
print("Hello, %s. You are %s." % (name, age))
Output: 'Hello Ayman. You are 22.'
```

#### Option #2: str.format()

```
name = "Ayman"
age = 22
print("Hello, {}. You are {}.".format(name, age))
Output: 'Hello, Ayman. You are 22.'
print("Hello, {1}. You are {0}.".format(age, name))
Output: 'Hello, Ayman. You are 22.'
```

### Memory Location

```
x = 10
id(x)                  # Output: 9789280
hex(id(x))             # Output: '0x955f60'
name.__repr__          # Output: <method-wrapper '__repr__' of int object at 0x955f60>

```

### Casting

If you want to specify the data type of a variable, this can be done with casting.

```
x = str(10)    # x will be '10'
y = int(10)    # y will be 10
z = float(10)  # z will be 10.0
```

### Get the Type

You can get the data type of a variable with the `type()` function.

```
x = 10
y = "Ayman"        
print(type(x))        # <class 'int'>
print(type(y))        # <class 'str'>
```

### Single and Double Quotes

both are the same

### Case-Sensitive

Variable names are case-sensitive.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://0xa1mn.gitbook.io/cyber-explained/programming/python/hello-world/variables-and-data-types.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
