Python Basics: Strings, Integers, Floats, Booleans
Strings
Strings in Python are used to store text. They are defined by using single (' ')
or double (" ") quotes.
You can perform many operations on strings such as concatenation, slicing, and
using built-in methods.
Examples:
name = "Swastik"
greeting = 'Hello'
Useful methods:
- name.upper() => 'SWASTIK'
- name.lower() => 'swastik'
- name[0:3] => 'Swa'
- len(name) => 7
- "Hi " + name => 'Hi Swastik'
Integers
Integers are whole numbers without a decimal point. They are used for counting,
indexing, and math operations.
Examples:
age = 21
score = 100
Operations:
- age + 5 => 26
- score * 2 => 200
Python Basics: Strings, Integers, Floats, Booleans
- score // 3 => 33 (integer division)
- score % 7 => 2 (remainder)
Floats
Floats are numbers with decimal points. They're useful in scientific and
financial calculations.
Examples:
pi = 3.14
price = 99.99
Operations:
- price + 10 => 109.99
- round(pi, 1) => 3.1
- int(price) => 99 (convert float to int)
Booleans
Booleans represent one of two values: True or False. They're often used in
decision-making and conditions.
Examples:
is_raining = True
has_passed = False
Usage in conditions:
if is_raining:
print("Take an umbrella")
else:
Python Basics: Strings, Integers, Floats, Booleans
print("No need for umbrella")
Practice Example
Putting it all together:
name = "Swastik"
age = 21
height = 5.9
is_student = True
print("Name:", name)
print("Age next year:", age + 1)
print("Height in cm:", height * 30.48)
print("Is a student?", is_student)