Get fresh content from StatelyWorld

If you’re just stepping into the world of Python programming, this simple code walkthrough will help
you understand some of the most common — and important — Python concepts.
We’ll go over printing, variables, conditions, data types, loops, and even random numbers — all in one place!


Hello, World!

print("-------------")
print("Hello, World!")
print("-------------")

Output:

-------------
Hello, World!
-------------

You can also check your Python version using the sys module:

import sys
print(sys.version)

Comparison & F-Strings

input1 = 5
input2 = 2

if input1 > input2:
    print(f"{input1} is greater than {input2}!")  # F-String Example

F-Strings let you embed variables directly into strings using curly braces {}. They’re
clean, readable, and fast.


Single vs Double Quotes

print("My Name is Gaurav J")
print('My Name is Gaurav J')

You can use triple quotes for multi-line strings or comments:

"""
This is a multi-line comment or string.
Written in more than one line.
"""

Type Conversion

x = str(3)    # '3'
y = int(3)    # 3
z = float(3)  # 3.0

print(type(x))
print(type(y))
print(type(z))

Working with Lists and Unpacking

fruits = ["apple", "mango", "Kaphal"]
x, y, x = fruits
print(fruits)
print(x)
print(y)
print(x)

Notice that the variable x is overwritten at the end — the last assignment wins.


Global vs Local Variables

def myfunc():
    global g
    g = "Your scope is local"
    print("Python is " + g)

myfunc()
print(g)

The global keyword makes a variable accessible outside of its function.


Python Data Types Overview

Category Example Types
Text str
Numeric int, float, complex
Sequence list, tuple, range
Mapping dict
Set set, frozenset
Boolean bool
Binary bytes, bytearray, memoryview
None NoneType
print(25j)  # Complex number

Generating Random Numbers

import random
print(random.randrange(1, 10))

This gives a random number between 1 and 9.


Strings and Loops

a = "Hello, World!"
print(a[1])

for x in "banana":
    print(x)

Python Collections

Python provides several collection data types:

Type Ordered Changeable Duplicates Allowed
List
Tuple
Set
Dictionary
list1 = ["apple", "banana", "cherry", 25]
print(list1)

thislist = ["apple", "banana", "cherry"]
print(thislist[-2])  # Output: banana

While Loop Example

i = 1
while i < 6:
    print(i)
    i += 1

Summary

  • Printing and string formatting
  • Variables and type conversion
  • Conditional statements
  • Data types and lists
  • Global and local variables
  • Random numbers
  • Loops and collections

Final Thought

Python is loved for its simplicity and readability.
Even small examples like these show how expressive the language can be.
Keep experimenting — play around with variables, loops, and functions — and you’ll soon feel right at home in Python 🐍.

Prompt Engineering with ChatGPT: What It Is, Why It Matters, and Top Techniques

Share This Post !

Leave A Comment