Python
Python
Python is a popular programming language. It was created by Guido van Rossum, and
released in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
What can Python do?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some
other programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-orientated way or a functional way.
Python Syntax compared to other programming
languages
Python was designed for readability, and has some similarities to the English language with
influence from mathematics.
Python uses new lines to complete a command, as opposed to other programming
languages which often use semicolons or parentheses.
Python relies on indentation, using whitespace, to define scope; such as the scope of loops,
functions and classes. Other programming languages often use curly-brackets for this
purpose.
PYTHON FEATURES
Python provides lots of features that are listed below.
1) Easy to Learn and Use
Python is easy to learn and use. It is developer-friendly and high-level programming language.
2) Expressive Language
Python language is more expressive means that it is more understandable and readable.
3) Interpreted Language
Python is an interpreted language i.e. interpreter executes the code line by line at a time. This makes
debugging easy and thus suitable for beginners.
4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, Unix and Macintosh etc. So, we
can say that Python is a portable language.
5) Free and Open Source
Python language is freely available at official web address. The source-code is also available. Therefore, it
is open source.
6) Object-Oriented Language
Python supports object-oriented language and concepts of classes and objects come into existence.
Python Casting
int() - constructs an integer number from an integer literal, a float literal (by rounding down
to the previous whole number), or a string literal (providing the string represents a whole
number)
float() - constructs a float number from an integer literal, a float literal or a string literal
(providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer literals
and float literals
Example
x = int(1) # x will be 1
y = int(2.8) # y will be 2
Python Operators
There are four collection data types in the Python programming language:
List is a collection which is ordered and changeable. Allows duplicate members. In Python
lists are written with square brackets.
Example
Create a List:
thislist= ["apple", "banana", "cherry"]
print(thislist)
Tuple is a collection which is ordered and unchangeable.
Allows duplicate members. In Python tuples are written with
round brackets.
Example
Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
The key function for working with files in Python is the open () function.
The open () function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
Syntax
To open a file for reading it is enough to specify the name of the file:
f = open("demofile.txt")
Write to an Existing File
To write to an existing file, you must add a parameter to the open() function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
Example
Open the file "demofile2.txt" and append content to the file:
f = open ("demofile2.txt", "a")
f. write("Now the file has more content!")
f.close()
f = open("demofile2.txt", "r")
print(f.read())
LEARNING OUTCOMES