Skip to content

Commit 627b373

Browse files
committed
Operators and Variables added
1 parent 0aac333 commit 627b373

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Operators.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Arithmetic Operators (+, -, *, /, %, **, //)
2+
a = 10
3+
b = 3
4+
print(a + b) # Output: 13 (Addition)
5+
print(a - b) # Output: 7 (Subtraction)
6+
print(a * b) # Output: 30 (Multiplication)
7+
print(a / b) # Output: 3.3333333333333335 (Division)
8+
print(a % b) # Output: 1 (Modulus - remainder after division)
9+
print(a ** b) # Output: 1000 (Exponentiation)
10+
print(a // b) # Output: 3 (Floor division - rounded down to the nearest whole number)
11+
12+
# Assignment Operators (=, +=, -=, *=, /=, %=, **=, //=)
13+
x = 5
14+
x += 3 # Equivalent to x = x + 3
15+
print(x) # Output: 8
16+
17+
y = 10
18+
y **= 2 # Equivalent to y = y ** 2
19+
print(y) # Output: 100
20+
21+
# Comparison Operators (==, >=, <=, >, <, !=)
22+
m = 15
23+
n = 20
24+
print(m == n) # Output: False (Equal to)
25+
print(m >= n) # Output: False (Greater than or equal to)
26+
print(m <= n) # Output: True (Less than or equal to)
27+
print(m > n) # Output: False (Greater than)
28+
print(m < n) # Output: True (Less than)
29+
print(m != n) # Output: True (Not equal to)
30+
31+
# Logical Operators (and, or, not)
32+
p = True
33+
q = False
34+
print(p and q) # Output: False (Logical AND)
35+
print(p or q) # Output: True (Logical OR)
36+
print(not p) # Output: False (Logical NOT)

Variables_datatypes.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
a = 71 # Identifies 'a' as a class <int>
2+
b = 88.44 # Identifies 'b' as a class <float>
3+
name = "Mahar" # Identifies 'name' as a class <str>
4+
5+
# to find datatype
6+
type(a) # Expecting output: <class 'int'> (to find and identify the data type of 'a')
7+
print(type(a)) # Output: <class 'int'>
8+
c = "31"
9+
type(c) # Expecting output: <class 'str'> (as numbers are written in string format)
10+
print(type(c)) # Output: <class 'str'>
11+
12+
# to convert one data type into another.
13+
str(31) # Output: '31' (converts integer 31 to string '31')
14+
int("32") # Output: 32 (converts string '32' to an integer)
15+
float(32) # Output: 32.0 (converts integer 32 to a floating-point number)
16+
17+
# converting string numerical to integer
18+
d = int("32") # Output: 32 (converts string '32' to an integer)
19+
print(d) # Output: 32
20+
print(type(d)) # Output: <class 'int'> (to confirm 'd' is of type integer)
21+
22+
# to take input from the keyboard as a string.
23+
name = input("Enter name: ") # Allows the user to input their name, stored in the 'name' variable as a string
24+
print("name is", name) # Prints the inputted name
25+
26+
27+

0 commit comments

Comments
 (0)