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

Basic Python programming Code Practise

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

Basic Python programming Code Practise

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

Basic Variable Assignment and applications on them:

x = 10 # Assigning an integer
y = 3.14 # Assigning a float
name = "Alice" # Assigning a string

a, b, c = 1, 2, 3 # Assigning multiple variables in a single line

x = y = z = 100 # All variables assigned the value 100

a, b = b, a # Swapping the values of `a` and `b`

x=5
y = x + 10 # y is assigned the value of `x + 10`

b1 = True # Assigning a boolean value


b2 = False # Assigning a boolean value

age = 25 # Integer variable


height = 5.9 # Float variable
name = "Python Programming" # String variable
is_student = True # Boolean variable

# Addition
a = 10
b=5
result1 = a + b
print(result1) # 15
# Subtraction
result2 = a - b
print(result2) #5

# Multiplication
result3 = a * b
print(result3) # 50

# Division
result4 = a / b
print(result4) # 2.0

# Floor Division (no decimal part)


result5 = a // b
print(result5) #2

# Modulus (remainder of division)


result6 = a % b
print(result56) #0

# Exponentiation (power)
result7 = a ** 2
print(result7) # 100

# Concatenation
greeting = "Hello" + ", " + name
print(greeting) # Python Programming
# Repetition
repeat_name = name * 3
print(repeat_name) # Python ProgrammingPython ProgrammingPython Programming

# Length of string
name_length = len(name)
print(name_length) #8

# Accessing individual characters


first_letter = name[0] # 'J'
last_letter = name[-1] # 'e'

x = 10
y = 20
# Logical AND
result8 = x > 5 and y > 15
print(result8) # True

# Logical OR
result9 = x > 15 or y > 15
print(result9) # True

# Logical NOT
result10 = not (x > 15)
print(result10) # True
# Taking a single input from the user
name = input("Enter your name: ")
print("Hello, " + name + "!")

# Taking multiple inputs in a single line and splitting them into a list
data = input("Enter three numbers separated by spaces: ").split()

# Converting strings to integers


num1, num2, num3 = int(data[0]), int(data[1]), int(data[2])

# Printing the sum of the numbers


print("Sum:", num1 + num2 + num3)

# Taking multiple inputs and converting them into integers in one line
num1, num2, num3 = map(int, input("Enter three numbers: ").split())

# Printing the result of a mathematical operation


print("Product:", num1 * num2 * num3)

# Using a comma as a delimiter


data = input("Enter values separated by commas: ").split(',')

# Limiting the number of splits (maxsplit)


data = input("Enter a sentence with multiple spaces: ").split(maxsplit=2)
print(data) # Only splits the first two spaces

You might also like