0% found this document useful (0 votes)
4 views2 pages

Python Cheatsheet

This cheat sheet covers fundamental concepts of Python programming, including variables, assignment, and input/output operations. It explains lvalues and rvalues, dynamic typing, and how to read user input and print output. Key points highlight the definition of variables, the importance of type compatibility, and the syntax for common operations.

Uploaded by

u5021628
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python Cheatsheet

This cheat sheet covers fundamental concepts of Python programming, including variables, assignment, and input/output operations. It explains lvalues and rvalues, dynamic typing, and how to read user input and print output. Key points highlight the definition of variables, the importance of type compatibility, and the syntax for common operations.

Uploaded by

u5021628
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Fundamentals CHEAT SHEET · CHAPTER 6 CLASS XI · CS

COMPONENTS OF A VARIABLES & 6.2 LVALUES & RVALUES


6.1 6.2
PROGRAM ASSIGNMENT
Lvalue (Left-hand side)
Expression A variable is a named memory location Expression that can appear on the left of an
A legal combination of symbols that that stores a value and can be used & assignment — i.e. a variable name.
represents a value. E.g. 3 + 4 , a * 2 changed during program run. Variables Rvalue (Right-hand side)
are symbolic labels for values. Literals and expressions that evaluate to a
Statement
A programming instruction that Python Creating a Variable value
value; can appear on either side.
can execute. E.g. x = 10 Simply assign a value — Python creates it
a = 20 # a is lvalue
automatically.
Comment
b = 10
Non-executable text for readability. a = b # b is rvalue here
marks = 78
Begins with # # ERROR:
student = "Jacob"
20 = a # literal can't be lvalue
Function age = 16

Named, reusable block of code. E.g.


Multiple Assignment
print() , input()
Note: A variable is defined only when
Block / Indentation a value is assigned to it. Using an a = b = c = 10 # all = 10
Group of statements belonging to undefined variable raises a x, y = 25, 50 # x=25, y=50
another statement, represented by NameError . x, y = y, x # swap!

indentation.
Tip: Python variables are labels
# main code pointing to values in memory — not
def firstMultiples(): fixed storage boxes like C/C++.
...
firstMultiples()

6.3 – VARIABLE DEFINITION & DYNAMIC 6.2 PYTHON VARIABLES IN MEMORY


6.4
TYPING
Python preloads common values into memory. Each memory
Variable Definition Dynamic Typing location has an address. Variables are labels pointing to these
A variable is created when you Python variables are labels
labels, not addresses.
first assign a value to it. It does typed containers. The same
Statement What happens
not exist before that. variable can hold different types
at different times. age = Label age points to location storing 15
print(x) # NameError 15
x = 20 x = 10 # int
print(x) # 20 ✓ x = "Hello" # now str age = A new label age is created pointing to location
x = 10 20 storing 20 (different address)
y = x/2
Note: Running print(x)
y = x/2 # y=0
before x = 20 gives Key difference from C/C++: In C++, reassigning a variable
NameError: name 'x' is not changes the contents of the same memory box. In Python, the
defined. Caution: Dynamic typing label moves to point at a new location — old value stays in
lets strings be re-assigned; memory until garbage-collected.
ensure the right type before
operations (e.g. don't divide Variable Types Created
a string).
trainNo = 181234 # int
type() function balance = 23456.75 # float
rollNo = 105 # int
Use type(<object>) to check the data type of any variable or
student = "Jacob" # str
literal.

>>> a = 10; type(a) # <class 'int'>


>>> a = 28.5; type(a) # <class 'float'>
>>> a = "hi"; type(a) # <class 'str'>

INPUT() — READING INPUT PRINT() — OUTPUT QUICK REFERENCE


6.6 INPUT() — READING INPUT 6.6.2 PRINT() — OUTPUT PROGRAMS QUICK REFERENCE

input() reads user input and always Syntax P6.1 — Welcome message

returns a String value.


print(*objects, sep=' ', end='\n') msg = input("Enter message: ")
Syntax
print("Hello,", msg)

Argument Default Effect


variable = input("prompt text")
P6.2 — Sum of 3 numbers
sep ' ' Character
Reading Numbers
Wrap with int() or float() to get inserted
n1 = int(input("Num 1: "))
numeric types: between
n2 = int(input("Num 2: "))
items n3 = int(input("Num 3: "))
age = int(input("Enter age: "))
print("Sum:", n1+n2+n3)
marks = float(input("Enter marks: ")) end '\n' Character
appended at
P6.4 — BMI Calculator
end of line
TypeError: input() returns a string.
You cannot do age + 1 directly — it w = float(input("Weight (kg): "))

raises TypeError: can only concatenate print("My","name","is","Amit.") h = float(input("Height (m): "))


bmi = w / (h * h)
str to str. # My name is Amit.
print("BMI:", bmi)

print("My","name", sep='...')
Caution: Value entered must be
# My...name P6.8 — Swap two numbers
compatible with the target type.
Entering "17.5" for int(input()) raises n1,n2 = int(input("n1: ")),int(input("n2: "))
a,b = 20,30
a ValueError
ValueError. n1,n2 = n2,n1
print("a =",a, end=' ')
print("b =",b) print("Swapped:", n1, n2)

# a = 20 b = 30 (same line)

Note: All objects passed to


print() must be convertible to
string type.

✦ LET US REVISE — KEY POINTS

A Python program can contain A variable in Python is defined only when Use int(input()) or float(input())
expressions, statements, comments, some value is assigned to it. to read numbers.
functions, blocks and indentation. Python supports dynamic typing — a Output is generated via print()
An expression is a legal combination of variable can hold different types at statement.
symbols representing a value. different times. print() default sep is space, default
A statement is a programming instruction. input() always returns a string type end is \n .

Comments are non-executable; they begin value. Value entered via input() must be
with # . compatible with the conversion type.

Computer Science with Python — XI · Chapter 6: Python Fundamentals · Variables, Simple I/O

You might also like