A float of floating point number can be positive or negative, containing one or more decimals. Scientific numbers with the letter “e” can be used to indicate the power of 10.
A data type is a grouping of data values. Variables are created to store a data type. In Python, the assigning of a value will indicate the data type. PHP does not require a specific format in order to display the value.
Variables store information such as values or other variables.
Python variables do not need to be declared with any type and the type can be changed after been set.
Variables cannot start with a number because they must start with an underscore or letter. The second character can contain only alphanumeric characters or underscores.
Variable names are case-sensitive.
A variable name cannot be any of the Python keywords.
Variable Names
Sample Codename = ‘John’ #name is type str
age = 18 # age is type int
height = 5.3 # height is type float
myVarName = ‘Johnny’ # Camel Case is where each word, except the first, starts with a capital letter
MyVarName = ‘Johnny’ # Pascal Case is where each word starts with a capital letter
my_var_name = ‘Johnny’ # Snake Case is where each word is separated by an underscore
car = fruit = color = ‘red’ # Assign the same value to multiple variables on one line
Output Variables
Sample Code
print(name) # Outputs a single variable
print(name, age, height) # Outputs multiple variables, separated by a comma
print(name + myVarName + car) # Outputs multiple non numeric variables, separated by a plus symbol
Hash symbol # will mark all text until a newline as a comment.
Sample Code# This is a single line comment
Multiline Comments
Python does not have a syntax for multiline comments. One way to achieve a multiline comment could be starting each line with a hash symbol.
Method 1 For Multiline Comments
Sample Code# This is a comment
# that goes on
# multiple lines
The other way to achieve a multiline comment is to use a multiline string because Python ignores string literals not assigned to a variable. A multiline string has tripe quotes.
Method 2 For Multiline Comments
Sample Code“””
This is a comment
that goes on
multiple lines
“”””
Python has a simple syntax and grammar which embraces one way of achieving a task. It does not use curly brackets to delimit blocks, but semicolons after statements are allowed.
Python uses white-space indentation to delimit blocks. An increase in indentation comes after certain statements. A decrease in indentation signifies the end of the current block. The suggested indentation size is 4 spaces.
Hello World Example
Sample Codeprint(‘Hello world’) # Prints Hello world
Python is a popular general-purpose scripting language that is maintained by the Python Software Foundation and is widely used for machine learning. It is released under the open source Python Software Foundation License and its official website is https://www.python.org.
Guido van Rossum first released Python as version 0.9.0 in 1991. A new version is released annually with support for 2 years and security updates for another 3 years.
Python can run on the command line or on a web server by an interpreter. Web frameworks exist to develop complex applications. Client-side Ajax-based applications can also be created to run Python using JavaScript.
Python as a scripting language, is commonly used in artificial intelligence projects and embedded is software products such as FreeCAD, Blender, Gimp, Inkscape, Scribus including notation programs.
In this chapter, you will learn about the brief history of Python, its syntax, comments and string formatting. You can use your favorite text editor, IDE or use the built-in IDE. The built-in IDE can also be used to compile your code.
If you use your own text editor or IDE, you will need to also install Python in order for your code to be interpreted. The built-in IDE has syntax highlighting using different colors for the keywords, comments, control-flow statements, variables and other elements.
To use the built-in IDE simple type the code in the IDE and click the compile button to display the results in the output area. Otherwise follow the requirements for the text editor and Python.
Edward Ojambo has been programming in various languages since 1985. He works as a freelance programmer creating custom databases, web, desktop and mobile applications. Private one-on-one tutorials are offered for computer programming, website development, custom applications, Linux, email servers, web servers and search engine optimization.
All content was created for educational purposes and is not affiliated with official Python. The content was extracted from examples that Edward Ojambo uses during work-related activities.
The content is released under Creative Commons BY-SA 4.0, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.
Use the content present in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to OjamboShop.com.