Info you need to get started with python
Python is one of the most accessible programming language available because of its simplified syntax. python codes can be easily written and executed much faster than other programming languages. There are two major Python versions: Python 2 and Python 3.
Understanding the basics : Keywords , identifiers, statements , comments and data types
Keywords: Python has a set of keywords that are reserved and cannot be used as variable names, function names, or any other identifiers. These are special words and give a specific meaning to the programmer.
some of the keywords are: if, elif ,for, while, and , finally, break etc
here is a list of the other keywords in python:
Identifiers : A Python identifier is a name used to identify a variable, function, class, module etc. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
some valid identifiers are: _abc , apple , Account, B etc
invalid identifiers are : 9ab, $abc etc (as it starts with a number and uses a special symbol other than underscore like $ in the example)
Statements: The set of instructions or commands a programmer writes in the source code to yield the desired output is called statements.There are different types of statements in the Python like Assignment statement, Conditional statement, Looping statements etc.
Assignment statements(assigning a value to a variable)
a=1
or
int i=10
Conditional statements (use if , elif )
#if statement synatx
if condition:
statement1
statement2
#program to illustrate If statement
i = 10
if(i > 15):
print ("I am greater than 15")
print ("I am Not in if")
output will be I am Not in if as 10 < 15#if-else statement syntax
if expression :
Statement
else
Statement#program to illustate if else
i = 10
if(i > 20):
print ("I am greater than 20")
else:
print ("I am lesser than 20")
Looping statements
for , while , do while are the looping statement used to repeat a set of instructions a number of times.
for syntax:
for iterator_var in sequence:
statements(s)
#example using for statement
n = 6
for i in range(0, n):
print(i)
Output:0
1
2
3
4
5
prints numbers from 0 to n-1
while syntax:
while expression:
statement(s)
#example using while
i=0
while(i< 3):
i=i+ 1
print("Hello world")
Nested loops syntax:
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
Comments
Comments can be used to explain Python code.Comments can be used to make the code more readable and can be used to prevent execution when testing code.There are 2 types of comments : single line and multiline comments. you can use the # symbol for single line comments and the triple double quotes for multiline comments.
#This is a single line comment
print("Hello, World!")
#This is a comment
#in multiple
#lines
print("Hello, World!")"""
This is a comment
in multiple
lines
"""
Data types:
Data types are used to describe the type of data being stored in the variable. python has the following fundamental datatypes:
Text Type:str
Numeric Types:int
, float
, complex
Sequence Types:list
, tuple
, range
Mapping Type:dict
Set Types:set
, frozenset
Boolean Type:bool
Binary Types:bytes
, bytearray
, memoryview
a #by default the string datatype
int a #variable a is an integer data type
float a #variable a is a float data type
Python I/O and import:
Python provides numerous built-in functions some of the functions like input()
and print()
are widely used for standard input and output operations respectively.
examples are:
print('This sentence is output to the screen') #for output
output :This sentence is output to the screen>>> num = input('Enter a number: ')#input using prompt
Enter a number: 10
>>> num
'10'
Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import
keyword to do this.
Operators: Operators are used to perform operations on variables and values.
These are some of the operators used in python.
Namespaces:A namespace is a system that has a unique name for each and every object in Python. An object might be a variable or a method. Python itself maintains a namespace in the form of a Python dictionary.
few useful functions in python are: min(),max(),len(),sorted(),sum(),print()etc
min : used to find the minimum value
max: used to find the maximum value
sum: used to find the sum of values etc.
Flow control statements
Python has 3types of control structures:
- Sequential — default mode
- Selection — used for decisions and branching,statements like if elif and nested ifs
- Repetition — used for looping, i.e., repeating a piece of code multiple times, looping statements as explained earlier.
Python data types or data structures
Lists : Lists are used to store multiple items in a single variable. they can hold different data types together in sq braces.
list syntax:
#list syntax
list_name=[item1,item2,item3]list = ["apple", "banana", "cherry"]
print(list)
Dictionary:
Dictionary in Python is an unordered collection of data values,Dictionary holds key:value
pair.
dictionary syntax:
#synatx
dict_name={key:value pairs}Adict = {
"brand": "Ford",
"model": "Mustang",
"year": 1800
}
Tuples:
Tuples are used to store multiple items in a single variable, tuples are immutable data types whereas lists are mutable.A tuple is created by placing all the items (elements) inside parentheses ()
, separated by commas.
Tuples syntax:
#syntax
tuple name=("statement")A_tuple = ("hello")
String :
Strings in Python are arrays of bytes representing unicode characters. Python does not have a character data type, a single character is simply a string with a length of 1.
a = "Hello, World!" #string is a collection of characters
print(a[1])#you access each element of the array using a sqbracket.