Master Python Basics: Essential Syntax, Data Types, and I/O Explained
This article provides a concise tutorial on Python's core fundamentals, covering program structure, syntax rules, variable naming, reserved keywords, primary data types, string indexing, boolean logic, statement forms, and basic input/output functions for beginners.
Part 1: Program Format Framework
Python programs use indentation to define blocks; proper indentation improves readability and maintainability. The recommended indentation is four spaces, though tabs are possible. A SyntaxError: unexpected indent indicates indentation problems.
Comments: single‑line comments start with # ; multi‑line comments are enclosed in triple quotes ( ''' ... ''' or """ ... """ ).
Line continuation: a backslash ( \ ) at the end of a line splits a long statement; no spaces may follow the backslash.
Part 2: Syntax Elements
Variables can be created and assigned at any time. Names may contain letters, digits, and underscores, must not start with a digit, and are case‑sensitive. Python 3 has 35 reserved keywords, e.g., and, as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield, async, await.
Part 3: Data Types
Numeric types: int (decimal, octal, binary, hexadecimal), float (decimal with fractional part or scientific notation), complex (a+bj, created with complex(a,b) ).
String type: defined with single or double quotes; each character occupies one position. Strings support two indexing schemes—zero‑based forward and negative backward—enabling slicing.
Boolean type: True and False . Logical operators: and (both true), or (any true), not (negation). Values such as 0, empty string, None , empty collections evaluate to False .
Numeric type conversion is supported (e.g., int() , float() ).
Part 4: Statement Elements
Assignment uses the equal sign ( = ).
Expressions consist of operators and operands and are evaluated to produce a value.
Part 5: Basic Input/Output Functions
input() reads a line from standard input and returns it as a string.
eval(str) evaluates a string containing a Python expression and returns the result.
print() outputs the given expression to standard output.
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.