Install for Windows
python.org > Downloads > Python 3.13.5 (latest version may vary)
Run installer, next, next .. finish
Run python code on Windows cmd shell
windows + r > cmd > python > print(‘hi’)
You’ve just run your first program
Run python code on python idle
windows + r > idle > print(‘hi’)
Let’s print more…
You may print several different values including
TEXT: in ‘single’ or “double” quotes
NUMBERS
INTEGERS: 3, 5, 6, 0
DECIMALS: 3.4, 5.7, 0.12
BOOLEANS: True / False
You may print several of the values at once by separating them by commas. Space after comma is optional. python prints them by inserting a single space between them.
If you print them in separate lines, python goes to the new line after printing them.
If you want to print them all at once, you may use ; between print statements. Space after ; is optional. Last ; on the line is optional.
If you don’t want any new line or space after printing, you write ,end='' inside paranthesis.
print('a', 'b', 'c')
a b c
print('a')
a
print('b')
b
print('c')
c
print('a'); print('b'); print('c');
a
b
c
print('a',end=''); print('b',end=''); print('c',end='');
abc
Comments in python
Comments are for us humans, they are ignored by python.
Single line comments: #text after hashtag is ignored. # can be at the start of the line or in the middle of the line.
#single line comment
x = 10 #comment after the code
Multi line comments: text between two sets of triple single or triple double quotes.
It must start on new line.
Or # on every line
'''comment spanning
multiple lines '''
"""using two sets of
triple quotes """
#you can use this
#if you write that
#at every line
Syntax in python
Python is case sensitive.
Each new line separates the code from the rest.
You may group multiple statements in one line by using ; if you wish.
Level of indentation groups statements. Multiples of 2 or 4 spaces are generally used for indentation.
Use of spaces around punctuation marks , = ( ) etc. are optional.
Variable names
Variables may contain alphanumeric characters and underscore. A-Z a-z 0-9 _
Variables are case sensitive.
Variables can not start with a number.
Variables can not be a python keyword.
Python keywords
You may see the list of python keywords by typing:
import keyword
print(keyword.kwlist)
As of writing, python has 35 keywords:
'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'
Variables
We may create variables and use them instead of values later.
Single variable assignment: x = 5
Multi variables assignment: x, y = 5, ‘hi’
We may see the type of the variable by using type(x)
for i in (a,b,c,d,e): type(i)
#is same as typing
type(a); type(b); type(c); type(d); type(e);
#which we will see later
Using variables
We can now use variables instead of numbers (integer/float), text(string) and true/false values(boolean).
a,b,c,d,e = 34, 3.4, 'hi', True, None
print(a,b,c,d,e)
Calculation / Operators
We can do some calculations over numbers / text.
+ addition - subtraction * multiplication / division
// floor division % modulus ** exponent
Division always produce float, even if the modulus is zero
print(5+3, 5-3, 5*3, 5/3, 5//3, 5%3, 5**3)
8 2 15 1.6666666666666667 1 2 125
We can also use + * over strings.
'hi' * 3; 'hi'+' '+'world';
In interactive mode, _ is assigned last result.
This is same as writing 3+5; 8*2; 16**16
Special characters / Escaping
Python has a special character: \
Backslash is used to ‘escape’ character following. Escaping means it changes the way it works. We use escapes inside single/triple quotes/double quotes.
\n for newline. escapes n.
\t for tab. escapes t.
\ prints \. escapes escaping.
\ + <enter> just goes to the next line without a special meaning. escapes <enter>.
\' prints '. escapes '.
\" prints ". escapes ".
#i want to write hi\hi'hi"
#first escape the characters hi\\hi\'hi\"
#then put it inside print('...') or print("...") or print('''...''') or print("""...""")
print('hi\\hi\'hi\"')
Besides those, single quotes can be escaped in double quotes and vice versa without using \.
Both can be escaped in triple single/double quotes.
print('escaped"inside') #escaped inside single quotes
print("escaped'inside") #double quotes
print('''escaped'and"inside''') #triple single
print("""escaped'and"inside""") #triple double
Triple single / double quotes automatically convert <enter> to \n
print('''no need to
escape separately''')
x='''no need
to escape'''
x
You may escape <enter> inside triple quotes by \<enter>
print('''\
this is for formatting purposes.\
will be in one line.''')
If we don’t escape, we have two extra new lines.
The last new line is automatically inserted by print. We can change this auto insert at the end behavior by using ,end='…' inside print.
If you have multiple items inside print separated by comma, single space is printed between those, and end= character is printed after all of them.
print('hi',end='') #for no new line
print('hi',end=' ') #for single space
print('hi',end=', ') #for ,<space>
print('hi1', 'hi2', 'hi3', end='---')