19
#Python Basics, Python 101!
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Python language is incredibly easy to use and learn for new beginners and newcomers. The python language is one of the most accessible programming languages available because it has simplified syntax and is not complicated, which gives more emphasis on natural language.
- It is a high-level language
- It is interpreted
- It is both object-oriented and functional
- It is portable
- It is extensible and embeddable
- It comes with a vast collection of libraries
- Web Development
- Game Development
- Data science
- Scientific and Numeric Applications
- Artificial Intelligence and Machine Learning
- Automation
- Desktop GUI
- Enterprise-level/Business Applications
The syntax of the Python programming language is the set of rules which defines how a Python program will be written.
A Python program is divided into a number of logical lines and every logical line is terminated by the token NEWLINE. A logical line is created from one or more physical lines.
A line contains only spaces, tabs, form feeds possibly a comment, is known as a blank line, and Python interpreter ignores it.
A physical line is a sequence of characters terminated by an end-of-line sequence
name = "Jane" # this variable stores the string Jane
print("Hello", name)
Python uses whitespace (spaces and tabs) to define program blocks whereas other languages like C, C++ use braces ({}) to indicate blocks of codes for class, functions or flow control.
- Use 4 spaces per indentation and no tabs
- Do not mix tabs and spaces
- Tabs create confusion and it is recommended to use only spaces
- Maximum line length : 79 characters which help users with a small display
- Use blank lines to separate top-level function and class definitions and single blank line to separate methods definitions inside a class and larger blocks of code inside functions
- When possible, put inline comments (should be complete sentences) *Use spaces around expressions and statements.
A comment begins with a hash character(#) which is not a part of the string literal and ends at the end of the physical line. All characters after the # character up to the end of the line are part of the comment and the Python interpreter ignores them.
name = input("enter your name")
# comment
print("hello", name)
- Keyword --- Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language. All the keywords except True , False and None are in lowercase and they must be written as they are.
- Identifiers --- is a name used to identify a variable, function, class, module or other object.
- Literals --- is a succinct and easily visible way to write a value. There are four type of literals in python. --- String literals ("string") --- numeric literals (1,2,3...) --- Boolean literals(True, False) --- Special Literal (none)
- String
- Integer
- Floats
- Complex
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].
fruits = ["mangoes", "bananas", "orange"]
print(fruits)
to add to a list we use the append function.
fruits = ["mangoes", "bananas", "orange"]
fruits.append("lemon")
print(fruits)
To remove items from a list the .remove function is used.
Python tuples are a data structure that store an ordered sequence of values. Tuples are immutable. This means you cannot change the values in a tuple. They let you store an ordered sequence of items. For example, you may use a tuple to store a list of employee names.
fruits = ("mangoes", "bananas", "orange")
print(fruits)
Dictionary in Python is an ordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized.
chris = {
"name": "Chris Murimi",
"age": 23
}
print(chris)
Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is both unordered and unindexed.
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. For example: >>> 2+3 5. Here, + is the operator that performs addition.
Operators type in python:
- Arithmetic operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.
- Relational Operators: Relational operators compares the values. It either returns True or False according to the condition.
- Logical operators: Logical operators perform Logical AND, Logical OR and Logical NOT operations.
- Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation.
- Assignment operators: Assignment operators are used to assign values to the variables.
- Special operators: There are some special type of operators like- Identity operators- is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal does not imply that they are identical. Membership operators- in and not in are the membership operators; used to test whether a value or variable is in a sequence.
- Precedence and Associativity of Operators: Operator precedence and associativity as these determine the priorities of the operator.
19