0% found this document useful (0 votes)
19 views3 pages

Python Basics: Strings, Variables, Data Types

The document covers various aspects of Python programming, including string representation, variable naming rules, basic data types, logical operations, loops, and arithmetic operations. It provides examples and explanations for each topic, highlighting valid and invalid variable names, the output of code snippets, and the syntax of for loops. Additionally, it explains the precedence of logical operators and the behavior of the range function.

Uploaded by

hs.ghai1980
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)
19 views3 pages

Python Basics: Strings, Variables, Data Types

The document covers various aspects of Python programming, including string representation, variable naming rules, basic data types, logical operations, loops, and arithmetic operations. It provides examples and explanations for each topic, highlighting valid and invalid variable names, the output of code snippets, and the syntax of for loops. Additionally, it explains the precedence of logical operators and the behavior of the range function.

Uploaded by

hs.ghai1980
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

1. What is used to represent Strings in Python?

Strings in Python are represented using quotes — either single quotes (' '), double quotes ("
"), or triple quotes (''' ''' / """ """).

2. Which of the following variable names are invalid? Justify.

Variable Valid / Invalid Reason

(a) try Invalid try is a Python keyword, cannot be used as a variable name.

(b) 123Hello Invalid Variable names cannot start with a digit.

(c) sum Valid Although it shadows a built-in function, it is not illegal.

(d) abc@123 Invalid Variable names cannot contain special characters like @.

3. Name four of Python’s basic data types. Why are they called so?

Examples of basic data types:

• int – integers

• float – decimal numbers

• str – strings

• bool – Boolean values (True/False)

They are called basic (or built-in) data types because Python provides them by default, and
they form the fundamental building blocks for handling data.

4. What gets printed?

x = True

y = False

z = False

if x or y and z:

print("yes")

else:

print("no")
Output: yes

Reason: and has higher precedence than or, so expression becomes:


x or (y and z) → True or (False and False) → True.

5. Which numbers are printed?

for i in range(2):

print(i)

for i in range(4, 6):

print(i)

Output:

Explanation

✔ range(2) prints: 0, 1
✔ range(4, 6) prints: 4, 5

6. Define for loops. Write the syntax.

Definition:
A for loop in Python is used to repeat a block of code for each item in a sequence (like a list,
string, or range).

Syntax:

for variable in sequence:

statements

7. Output of the code

a=0

a += 2
print(a)

Output: 2

8. Output of the code

print(3 + 4)

print(3 - 4)

print(3 * 4)

print(3 / 4)

print(3 % 2)

print(3 ** 4)

print(3 // 4)

Output:

-1

12

0.75

81

You might also like