Simple Syntax
Simple Syntax
Programs
A program is a sequence of instructions that specifies how to perform a
computation. The computation might be something mathematical, such as
solving a system of equations or finding the roots of a polynomial, but it can
also be a symbolic computation, such as searching and replacing text in a
document or (strangely enough) compiling a program.
The details look different in different languages, but a few basic instructions
appear in just about every language:
input: Get data from the keyboard, a file, or some other device.
output: Display data on the screen or send data to a file or other
device.
math: Perform basic mathematical operations like addition and
multiplication.
conditional execution: Check for certain conditions and execute the
appropriate sequence of statements.
repetition: Perform some action repeatedly, usually with some
variation.
Python Objects
Each Python object has three defining properties: identity, value, and type.
1. Identity: A unique identifier that describes the object.
2. Value: A value such as "20", "abcdef", or 55.
3. Type: The type of the object, such as integer or string.
The value of an object is the data associated with the object. For example,
evaluating the expression 2 + 2 creates a new object whose value is 4. The
value of an object can generally be examined by printing that object.
The type of an object determines the object's supported behavior. For
example, integers can be added and multiplied, while strings can be
appended with additional text or concatenated together. An object's type
never changes once created. The built-in function type() prints the type of an
object.
Python identifiers
A programmer gives names to various items, such as variables (and also
functions, described later). For example, x = 5 uses the name **x** to refer
to the value 5. A name, also called an identifier, is a sequence of letters (a-z,
A-Z, _) and digits (0-9). An identifier must start with a letter. Note that "_",
called an underscore, is considered to be a letter.
Upper and lower case letters differ. That is, Python is case sensitive,
so cat and Cat are different.
The following are valid names: c, cat, Cat, n1m1,
short1, and _hello.
The following are invalid names: 42c (doesn't start with a letter), hi
there (has a space), and cat$ (has a symbol other than a letter or
digit).
Names that start and end with double underscores (for example, __init__) are
allowed but should be avoided, because Python has special use for double
underscore names.
A good variable name should describe the purpose of the variable, such as
"temperature" or "age", rather than just "t" or "A".
A good practice when naming variables is to use all lowercase letters and to
place underscores between words (e.g., "last_name", "birth_place").
Python does not allow punctuation characters such as @, $, and % within
identifiers. Python is a case sensitive programming language.
Thus, Age and age are two different identifiers in Python.
Reserved Words
Reserved words are keywords for which Python has specific purposes. These
are words that you cannot use as variable, function, or any other identifier
names. All the Python keywords contain lowercase letters only. Many
language editors will automatically color a program's reserved words.
The following list shows the Python keywords. These are python
keywords:
keyboard_arrow_down
Statements
A statement is an instruction that the Python interpreter can execute.
When you type a statement on the command line, Python executes it.
keyboard_arrow_down
Output
print("Hello World!")
[]
print("Hello World")
account_circle
Hello World!
[]
print("hello world")
account_circle
hello world
[]
print('Hello World')
account_circle
Hello World
[]
print("Anything you write in between the quotes is a string, and you can print it")
account_circle
Anything you write in between the quotes is a string, and you can print it
[]
print("Anything you write in between the "" is a string, and you can print it")
account_circle
Anything you write in between the is a string, and you can print it
[]
print("Anything you write in between the \" is a string, and you can print it")
account_circle
Anything you write in between the " is a string, and you can print it
[]
print("Anything you write in between the \"\" is a string, and you can print it")
account_circle
Anything you write in between the "" is a string, and you can print it
keyboard_arrow_down
Comments
A hash sign (#) that is not inside a string literal begins a comment. All
characters after the # and up to the end of the physical line are part of the
comment and the Python interpreter ignores them.
# This is your first comment
[]
# This is your first comment
# see how this Colab interpreter colors comments in green?
# other interpreters may color them differently (e.g., in gray)
# also, when you execute the code, all the comments are ignored
keyboard_arrow_down
Variables
Variables are nothing but reserved memory locations to store values. This
means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and
decides what can be stored in the reserved memory. Therefore, by assigning
different data types to variables, you can store integers, decimals or
characters in these variables.
Here, 100, 1000.0 and "John" are the values assigned to counter, miles, and
name, respectively.
[]
Name = "John Smith"
name = "Jane Doe"
a = 2+3
print(a)
age = 11
print(age)
age = 12
print(age)
account_circle
10
11
12
keyboard_arrow_down
[]
a = 500 # this will be represented as an integer in memory
print(a)
print(type(a))
keyboard_arrow_down
[]
variable_word = 'word'
print(variable_word)
keyboard_arrow_down
[]
while False:
print("hello world")
print("nothing happened")
keyboard_arrow_down
[]
x = 50
print(x)
print(type(x))
y = float(x)
print(y)
print(type(y))
z = str(y)
print(z)
print(type(z))
keyboard_arrow_down
Input
Let's see another function. Input receives something from the standard
input, in this case your keyboard, and it assigns it to a variable. By default
the received input is a string.
a = input()
[]
a = input()
print(type(a))
print(a)
account_circle
4.7
<class 'str'>
4.7
[]
a = input("Enter some text ")
print(type(a))
print(a)
account_circle
Enter some text enter some text
<class 'str'>
enter some text
[]
a = input("Enter a number ")
print(type(a))
print(a)
account_circle
Enter a number 6
<class 'str'>
6
[]
a = int(input("Enter a number "))
print(type(a))
print(a)
account_circle
[]
a = float(input("Enter a number "))
print(type(a))
print(a)
[]
a = complex(input("Enter a complex number "))
print(type(a))
print(a)
account_circle
Enter a complex number 6
<class 'complex'>
(6+0j)
arrow_upwardarrow_downward
link
settings
delete
more_vert
[]
max_number = max(3,6)
min_number = min(5,7)
print("this is the max number: ",str(max_number))
print(min_number)
a = 45
a = my_function()
account_circle
this is the max number: 6