-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.Variables.py
More file actions
109 lines (87 loc) · 2.86 KB
/
2.Variables.py
File metadata and controls
109 lines (87 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# ==============================
# Declaring and Assigning Variables
# ==============================
# Variables store data values. You don't need to declare their type explicitly.
age = 32 # integer
height = 6.1 # float (decimal number)
name = "Krish" # string
is_True = True # boolean (True/False)
# ==============================
# Printing the Variables
# ==============================
print("age:", age)
print("height:", height)
print("name:", name)
print("is_True:", is_True)
# ==============================
# Naming Conventions
# ==============================
# ✅ Rules for naming variables:
# 1. Must start with a letter (a-z, A-Z) or underscore (_)
# 2. Can contain letters, numbers, and underscores
# 3. Case-sensitive (name, Name, NAME are all different variables)
# 4. Use descriptive names for better readability
first_name = "Nived"
last_name = "Shenoy"
# Example of case sensitivity:
name = "Nived"
Name = "Shenoy"
print(name) # Output: Nived
print(Name) # Output: Shenoy
# ==============================
# Understanding Variable Types
# ==============================
# Python is *dynamically typed*: type of a variable is determined at runtime.
age = 25 # int
salary = 25000.45 # float
name = "Nived" # str
is_student = True # bool
# Check variable types using type()
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(salary)) # <class 'float'>
print(type(is_student)) # <class 'bool'>
# ==============================
# Type Checking and Conversion
# ==============================
age = 25
print(type(age)) # <class 'int'>
# Convert int → str
age_str = str(age)
print(age_str) # '25'
print(type(age_str)) # <class 'str'>
# Convert str → int
age = '25'
print(int(age)) # 25
print(type(int(age))) # <class 'int'>
# ⚠️ Be careful: trying to convert a non-numeric string to int will cause an error
# Example: int("Krish") ❌ will throw ValueError
# Convert float → int (truncates decimal part)
height = 15.11
print(int(height)) # 15
# Convert int back to float
print(float(int(height))) # 15.0
# ==============================
# Dynamic Typing
# ==============================
# Python allows variable type to change during execution
var = 10
print(var, type(var)) # 10 <class 'int'>
var = "Hello"
print(var, type(var)) # Hello <class 'str'>
# ==============================
# Simple Calculator
# ==============================
# Taking input from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Performing basic operations
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2 # Division always returns a float
# Display results
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)