Python Basics: Hello World, Data Types, Control Flow, and File I/O
This tutorial introduces Python fundamentals by demonstrating a Hello World program, explaining comments, covering numeric, string, tuple, list, set, and dictionary data types, and illustrating control flow constructs, loops, functions, command‑line arguments, and basic file read/write operations with clear code examples.
This article provides a step‑by‑step introduction to Python programming for beginners.
Hello World print "Hello World!" Comments start with # and are ignored by the interpreter.
# This is a comment
print "This text will be printed because the print statement is executed."
# This line is a comment and will not run
print "But the comment finished with the end of the line."
print "So the 4th and 5th line of the code are executed again."Data Types
Numeric: integers and floats.
Text: strings.
Composite: tuples, lists, sets, dictionaries.
Integer Example
rectangle_side_a = 10
rectangle_side_b = 5
rectangle_area = rectangle_side_a * rectangle_side_b
rectangle_perimeter = 2*(rectangle_side_a + rectangle_side_b)
print "Let there be a rectangle with the sides of lengths:", rectangle_side_a, "and", rectangle_side_b, "cm."
print "Then the area of the rectangle is", rectangle_area, "cm squared."
print "The perimeter of the rectangle is", rectangle_perimeter, "cm."Float Example
pi = 3.14159
circle_radius = 10.2
circle_perimeter = 2 * pi * circle_radius
circle_area = pi * circle_radius * circle_radius
print "Let there be a circle with the radius", circle_radius, "cm."
print "Then the perimeter of the circle is", circle_perimeter, "cm."
print "The area of the circle is", circle_area, "cm squared."String Example
first_name = "Satoshi"
last_name = "Nakamoto"
full_name = first_name + " " + last_name
print "The inventor of Bitcoin is", full_name, "."Tuple Example
import math
point_a = (1.2,2.5)
point_b = (5.7,4.8)
segment_length = math.sqrt(
math.pow(point_a[0] - point_b[0], 2) +
math.pow(point_a[1] - point_b[1], 2))
print "Let the point A have the coordinates", point_a, "cm."
print "Let the point B have the coordinates", point_b, "cm."
print "Then the length of the line segment AB is", segment_length, "cm."List Example
some_primes = [2, 3]
some_primes.append(5)
some_primes.append(7)
print "The primes less than 10 are:", some_primesSet Example
from sets import Set
boys = Set(['Adam', 'Samuel', 'Benjamin'])
girls = Set(['Eva', 'Mary'])
teenagers = Set(['Samuel', 'Benjamin', 'Mary'])
print 'Adam' in boys
print 'Jane' in girls
girls.add('Jane')
print 'Jane' in girls
teenage_girls = teenagers & girls # intersection
mixed = boys | girls # union
non_teenage_girls = girls - teenage_girls # difference
print teenage_girls
print mixed
print non_teenage_girlsDictionary Example
dictionary_names_heights = {}
dictionary_names_heights['Adam'] = 180.
dictionary_names_heights['Benjamin'] = 187
dictionary_names_heights['Eva'] = 169
print 'The height of Eva is', dictionary_names_heights['Eva'], 'cm.'Control Flow
x = 10
if x == 10:
print 'The variable x is equal to 10.'
if x > 20:
print 'The variable x is greater than 20.'
else:
print 'The variable x is not greater than 20.'
if x > 10:
print 'The variable x is greater than 10.'
elif x > 5:
print 'The variable x is not greater than 10, but greater ' + 'than 5.'
else:
print 'The variable x is not greater than 5 or 10.'For‑Loop with range
print "The first 5 positive integers are:"
for i in range(1,6):
print iFor‑Loop over a list
primes = [2, 3, 5, 7, 11, 13]
print 'The first', len(primes), 'primes are:'
for prime in primes:
print primeBreak and Continue
for i in range(0,10):
if i % 2 == 1:
continue
print 'The number', i, 'is divisible by 2.'
for j in range(20,100):
print j
if j > 22:
breakFunction Definition
def rectangle_perimeter(a, b):
return 2 * (a + b)
print 'Let a rectangle have its sides 2 and 3 units long.'
print 'Then its perimeter is', rectangle_perimeter(2, 3), 'units.'
print 'Let a rectangle have its sides 4 and 5 units long.'
print 'Then its perimeter is', rectangle_perimeter(4, 5), 'units.'Command‑Line Arguments
import sys
print 'The number of the arguments given is', len(sys.argv), 'arguments.'
print 'The argument list is ', sys.argv, '.'File Read/Write
# Write to file "test.txt"
file = open("test.txt","w")
file.write("first line
")
file.write("second line")
file.close()
# Read the file
file = open("test.txt","r")
print file.read()The article concludes with a copyright notice and a reference to the original source.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
