0% found this document useful (0 votes)
0 views

Python Lab1 Extension

The document provides an overview of Python basics, including built-in data types such as integers, floats, complex numbers, booleans, and strings. It also covers various operators, including arithmetic, relational, logical, bitwise, membership, identity, and the ternary operator, along with examples of their usage. Additionally, it explains functions like id() and type() for object identification and type checking, as well as base/type conversion functions.

Uploaded by

nahemi5518
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Lab1 Extension

The document provides an overview of Python basics, including built-in data types such as integers, floats, complex numbers, booleans, and strings. It also covers various operators, including arithmetic, relational, logical, bitwise, membership, identity, and the ternary operator, along with examples of their usage. Additionally, it explains functions like id() and type() for object identification and type checking, as well as base/type conversion functions.

Uploaded by

nahemi5518
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Python Basics
*Data Types*
Python has several built-in data types, including:

- *Integers* (`int`): whole numbers, e.g., 1, 2, 3, etc.


- *Floating Point Numbers* (`float`): decimal numbers, e.g., 3.14, -0.5, etc.
- *Complex Numbers* (`complex`): numbers with real and imaginary parts, e.g., 3 + 4j, etc.
- *Boolean* (`bool`): true or false values
- *Strings* (`str`): sequences of characters, e.g., "hello", 'hello', etc.

```
Examples of data types
my_int = 10
my_float = 3.14
my_complex = 3 + 4j
my_bool = True
my_str = "Hello, World!"

print(type(my_int)) # Output: <class 'int'>


print(type(my_float)) # Output: <class 'float'>
print(type(my_complex)) # Output: <class 'complex'>
print(type(my_bool)) # Output: <class 'bool'>
print(type(my_str)) # Output: <class 'str'>
```

*id() and type()*


- `id()`: returns the unique identifier for an object
- `type()`: returns the type of an object

```
my_var = 10
print(id(my_var)) # Output: unique identifier
print(type(my_var)) # Output: <class 'int'>
```

*range()*
- `range()`: generates a sequence of numbers

```
for i in range(5):
print(i) # Output: 0, 1, 2, 3, 4
```

*Base/Type Conversion*
- `int()`: converts a value to an integer
- `float()`: converts a value to a floating point number
- `str()`: converts a value to a string

```
my_str = "10"
my_int = int(my_str)
print(my_int) # Output: 10

my_float = 3.14
my_str = str(my_float)
print(my_str) # Output: "3.14"
```

2. Operators
*Arithmetic Operators*
- `+`: addition
- `-`: subtraction
- `*`: multiplication
- `/`: division
- `//`: floor division
- `%`: modulus
- `**`: exponentiation

```
a = 10
b=3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
```

*Relational Operators*
- `==`: equal to
- `!=`: not equal to
- `>`: greater than
- `<`: less than
- `>=`: greater than or equal to
- `<=`: less than or equal to

```
a = 10
b=5
print(a > b) # Output: True
print(a < b) # Output: False
```

*Logical Operators*
- `and`: logical and
- `or`: logical or
- `not`: logical not

```
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
```

*Bitwise Operators*
- `&`: bitwise and
- `|`: bitwise or
- `^`: bitwise xor
- `~`: bitwise not

```
a = 5 # 101
b = 3 # 011
print(a & b) # Output: 1 (001)
print(a | b) # Output: 7 (111)
```

*Membership Operators*
- `in`: membership test
- `not in`: non-membership test

```
my_list = [1, 2, 3]
print(2 in my_list) # Output: True
print(4 in my_list) # Output: False
```

*Identity Operators*
- `is`: identity test
- `is not`: non-identity test
```
a = 10
b = 10
print(a is b) # Output: True
```

*Ternary Operator*
- `value_if_true if condition else value_if_false`

```
age = 25
status = "adult" if age >= 18 else
```

You might also like