If you’re new to coding, Python is the perfect language to start with. It’s simple, powerful, and easy to understand—even if you’ve never written a single line of code before.
This guide will help you understand the basic building blocks of Python, so you can start your programming journey with confidence.
1. Showing Output in Python
To display something on the screen in Python, we use the print()
function.
print('Hello World')
print(7)
print(7.7)
print(True)
You can print multiple values too:
print('Hello', 1, 4.5, True)
Change the Separator (sep
)
print('Hello', 1, 4.5, True, sep='/') # Output: Hello/1/4.5/True
Change What Happens at the End (end
)
print('hello', end='-')
print('world') # Output: hello-world
✅ Remember: Python is case-sensitive.
❌ Writing Print()
instead of print()
will cause an error.
2. Python Data Types
Python can handle many kinds of data. Here are the most common:
Core Data Types:
- Integer: Whole numbers (e.g.,
5
,100
) - Float: Decimal numbers (e.g.,
8.55
,1.7e309
→ givesinf
) - Boolean:
True
orFalse
- String: Text (e.g.,
'Hello'
) - Complex: Numbers with real and imaginary parts (
5 + 6j
)
Collection Data Types:
- List: Ordered, changeable (
[1, 2, 3]
) - Tuple: Ordered, unchangeable (
(1, 2, 3)
) - Set: Unordered, unique values (
{1, 2, 3}
) - Dictionary: Key-value pairs (
{'name': 'Nitish', 'age': 25}
)
To check the type of data, use type()
:
type(5) # int
type([1, 2, 3]) # list
3. Variables
Variables are used to store data. Python doesn’t require you to declare the type:
a = 5 # integer
a = 'hello' # now it's a string
You can also:
a, b, c = 1, 2, 3 # multiple values
x = y = z = 10 # same value to multiple variables
4. Comments in Python
Comments are lines that Python ignores. They help explain your code.
# This is a comment
print("Hello") # This is also a comment
5. Keywords and Identifiers
Keywords:
These are special words in Python like if
, else
, while
, for
. You cannot use them as variable names.
Identifiers:
These are names you give to variables or functions. Rules:
- Cannot start with a number
- Cannot use special characters like
@
,#
,$
- Can use underscore
_
- Cannot be a keyword
Valid examples:
_name = "Nitish"
name1 = "Python"
6. Getting User Input
Use input()
to get input from the user:
email = input("Enter your email: ")
All input is taken as a string. You can convert it:
a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
print(a + b)
7. Type Conversion
Implicit Conversion:
Python automatically converts one type to another:
print(5 + 5.6) # Output: 10.6
Explicit Conversion:
Use functions to convert types:
str(5) # '5'
float(4) # 4.0
int("10") # 10
TypeError:
Trying to combine incompatible types causes an error:
print(4 + '4') # TypeError
8. Literals in Python
Numeric Literals:
- Binary:
0b1010
- Octal:
0o310
- Hexadecimal:
0x12c
- Float:
10.5
,1.5e2
,1.5e-3
- Complex:
3.14j
z = 5 + 3j
print(z.real) # 5.0
print(z.imag) # 3.0
String Literals:
'hello' | "hello" | '''multi-line'''
u"\U0001f600" # Unicode emoji
r"raw \n text" # Backslash is not treated as escape
Boolean Literals:
True + 4 # 5
False + 10 # 10
Special Literal:
x = None
9. Operators in Python
Python includes:
- Arithmetic (
+
,-
,*
,/
) - Relational (
==
,!=
,<
,>
) - Logical (
and
,or
,not
) - Bitwise (
&
,|
,^
) - Assignment (
=
,+=
,-=
) - Membership (
in
,not in
)
10. If-Else Statements
Use if-else
to make decisions:
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
else:
print("Zero or Negative")
Conclusion
Congratulations! You just learned the most important basics of Python programming. With these concepts, you’re ready to write simple scripts and build a solid foundation for your coding journey. Keep practicing, and happy coding!