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

Python_Refresher_3

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)
15 views8 pages

Python_Refresher_3

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
You are on page 1/ 8

# Comment

To give information about specific code. Interpreter/Compiler will skip comment.

# This is a single-line comment

"""

This is a

multi-line

comment

"""

Variable and Data type


Variable is used to store data.

Variable / Box will store what kind of data. Is represented by data type.

int num;

num is a variable, which will store integer type of data.

num =5;
# Data types: int, float, str, bool, list, tuple, dict, set

# Example of integer variable


age = 25

# Example of float variable


weight = 65.5

# Example of string variable


name = "Alice"

# Example of boolean variable


is_student = True

# Example of list variable


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

# Example of tuple variable


coordinates = (10, 20)
# Example of dictionary variable
person = {'name': 'John', 'age': 30, 'is_student': False}

# Example of set variable


unique_numbers = {1, 2, 3, 4, 5}

# Example of NoneType variable


result = None

# Function : Code reusability


1) Definition - below main()
2) Calling - inside main()

// Create a function : Definition of function


def myfunc() {
printf("hello world \n");
}

# definition
def myfunc(){
// code
}

# calling a function
myfunc();

# hint to explain more


def add(a: int, b: int) -> int:
return a + b
# Actual Parameter Vs formal Parameter

Actual Parameter : value we pass while calling the function

myCalculator( 10 , 6 );

Here 10 and 6 are actual parameter

Formal Parameter : the variable used to hold actual parameters in definition


int myFunction ( int a, int b);

int myFunction( int a, int b){

}
# Create an empty list
my_list = [ ]

# Create a list with initial values

my_list = [1, 2, 3, 4, 5 ]

# Example of list variable

numbers = [91.8, 12, 'i', 'Ram', 25]

# To count the number of items in a list

len(numbers)

Accessing Elements:

# Accessing elements by index

first_element = my_list[0]

last_element = my_list[-1]

Slicing
# Extracting a sublist

subset = my_list[1:3] # [2, 3]


Adding Element
# Append an element to the end

my_list.append(6)

# Insert an element at a specific index

my_list.insert(2, 10) # Inserts 10 at index 2

Removing Element
# Remove the first occurrence of a value

my_list.remove(3)

# Remove an element by index and return it

popped_element = my_list.pop(1) # Removes and returns the element at index 1

List Operations:

my_list1 = [1, 2, 3, 4, 5]

my_list2 = [6,7]

# Concatenation

combined_list = my_list1 + my_list2

# Repetition

repeated_list = my_list * 3
List Methods:

# Length of the list

length = len(my_list)

# Sorting the list in ascending order

my_list.sort()

# Reversing the list

my_list.reverse()

# Finding index of an element

index = my_list.index(4)

# Count occurrences of an element

count = my_list.count(2)

# Clear the list

my_list.clear()

## List to be continued

You might also like