0% found this document useful (0 votes)
2 views12 pages

3python Basics Notes

Uploaded by

bilhasara13
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)
2 views12 pages

3python Basics Notes

Uploaded by

bilhasara13
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/ 12

Python Basics

Variables
Variables in Python are symbolic names that reference or point to
objects stored in memory. They are created when you first assign a
value to them, and their type is determined by the value you assign.
Here’s a quick rundown of how variables work in Python:

 Naming: Variable names must start with a letter or an underscore


and can contain letters, numbers, and underscores. They are case-
sensitive (var, Var, and VAR are different variables).
 Assignment: Use the equals sign (=) to assign values to variables.
You can assign a single value to multiple variables or different
values to multiple variables in one line.
 Dynamic Typing: Variables in Python can be reassigned to
different data types during execution, which is known as dynamic
typing.
 Scope: The scope of a variable determines its visibility. Variables
defined inside a function are local to that function, while variables
defined outside are global.

Here’s an example of variable assignment in Python:

# Assigning an integer value

number = 100

1
# Assigning a string value

greeting = "Hello, World!"

# Assigning multiple values at once

x, y, z = 1, 2, "three"

# Chained assignment

a = b = c = "Python"

Datatypes

Python has several built-in data types that are used to define the
behavior of objects. Here’s a brief overview of the main data types:

Numeric Types:

int (Integer): Whole numbers, positive or negative, without decimals.

float: Floating-point numbers, which are numbers with a decimal point.

2
complex: Complex numbers with a real and imaginary part, denoted as
a + bj.

Sequence Types:

str (String): A sequence of Unicode characters.

list: An ordered collection of items which can be of different data types.

tuple: Similar to a list, but immutable (cannot be changed).

range: A sequence of numbers, often used in for loops.

Mapping Type:

dict (Dictionary): An unordered collection of data in a key:value pair


form.

Set Types:

set: An unordered collection of unique items.

frozenset: Similar to a set, but immutable.

Boolean Type:

bool: Represents True or False.

Binary Types:

bytes: Immutable sequence of bytes.

bytearray: Mutable sequence of bytes.

memoryview: A memory view object of the bytes.

None Type:

3
NoneType: Represents the absence of a value or a null value.

Each data type in Python is defined as a class, and variables are


instances of these classes. You can check the data type of any object
using the type() function. For example:

x = 10

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

Input Functions:

The input() function is used to take input from the user. It reads a line
from the input (usually from the user), converts it into a string , and
returns that

Eg:

user_input = input("Enter your data: ")

Output Functions:

The print() function is used to output data to the standard output


device (screen).

Eg:

4
print("Hello, World!") # Outputs: Hello, World!

Type conversions and casting


Type conversions and casting in Python allow you to convert a value from one
data type to another. This can be done implicitly or explicitly.

Implicit Type Conversion (Coercion): Python automatically converts one data type
to another without any user involvement. This usually happens during expressions
where combining types is necessary.

Eg:

# Implicit conversion of integer to float

num_int = 123

num_float = 1.23

num_new = num_int + num_float

print(num_new) # Output: 124.23

print(type(num_new)) # Output: <class 'float'>

Explicit Type Conversion (Casting): This requires the use of predefined


functions like int(), float(), and str() to convert a value’s data type.

Eg:

# Explicit conversion of float to integer

5
num_float = 5.9

num_int = int(num_float)

print(num_int) # Output: 5

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

# Explicit conversion of integer to string

num_int = 5

num_str = str(num_int)

print(num_str) # Output: '5'

print(type(num_str)) # Output: <class 'str'>

Key Points:

 Implicit conversion is done by Python to avoid data loss.


 Explicit conversion is done manually by the programmer to
convert the data type of an object to a required data type.
 Type casting can lead to data loss (e.g., when casting float to int).

Comments

Comments in Python are crucial for making your code more readable
and understandable. They are lines that the Python interpreter ignores

6
when executing the code, allowing programmers to leave notes and
explanations within the code. Here’s a quick guide with examples:

Single-Line Comments:

 Use the hash (#) symbol to start a single-line comment.


 Everything following the # on that line will be ignored by Python.
 Eg:
# This is a single-line comment
print("Hello, World!") # This comment is inline with code

Multi-Line Comments:

 Python doesn’t have a specific syntax for multi-line comments,


but you can use a # for each line.
 Alternatively, you can use triple quotes (''' or """) for multi-line
strings that aren’t assigned to a variable, which Python will ignore.
 Eg:
# This is the first line of a multi-line comment
# And this is the second line of a multi-line comment

"""
This is another form of a multi-line comment
using triple quotes. This entire string will be ignored.
"""
print("Python is fun!")

Operators

7
Python operators are used to perform operations on values and
variables. Here’s a brief overview with examples:

Arithmetic Operators:

Used for basic mathematical operations.

+, -, *, /, //, %, **

Eg:

# Addition

print(5 + 2) # Output: 7

# Subtraction

print(4 - 2) # Output: 2

# Multiplication

print(2 * 3) # Output: 6

# Division

print(4 / 2) # Output: 2.0

8
# Floor Division

print(10 // 3) # Output: 3

# Modulo

print(5 % 2) # Output: 1

# Power

print(4 ** 2) # Output: 16

Assignment Operators:

Used to assign values to variables.

=, +=, -=, *=, /=, %=, **=

Eg:

a = 10 # Assignment

a += 5 # Addition assignment, equivalent to a = a + 5

print(a) # Output: 15

Comparison Operators:

Compare two values and return a boolean result.

==, !=, >, <, >=, <=

9
Eg:

a=5

b=2

print(a > b) # Output: True

print(a == b) # Output: False

Logical Operators:

Used to combine conditional statements.

and, or, not

Eg:

a = True

b = False

print(a and b) # Output: False

print(a or b) # Output: True

print(not a) # Output: False

Identity Operators:

Compare the memory locations of two objects.

is, is not

Eg:

10
a = [1, 2, 3]

b = [1, 2, 3]

print(a is b) # Output: False

print(a is not b) # Output: True

x= [1, 2, 3]

y=x

print(x is y)

# Output: True, as both x and y point to the same list object

Membership Operators:

Test whether a value or variable is in a sequence.

in, not in

Eg:

a = [1, 2, 3]

print(1 in a) # Output: True

print(4 not in a) # Output: True

11
12

You might also like