1- python3 varibles and simple data types
Variables
here “message” is variable
message = "Hello world!"
print(message)
variable rules(Naming & Using)
- Variable names can contain only letters, numbers, and underscore, but not with a number. For instance, you can calla variable message_1 but not 1_message
- Spaces are not allowed in varrible names, but underscores can be used toseparate words in variable nmes. For example greeting_message works , but greeting message will cause errors.
- Avoid using python keywords and function names as variable names; that is, do not usewords that python has reserved for a particular programmatic purpose, such as word print .![[python-keywords.jpg]]
- Variable an,es should be short but desperictive. For example, name is better than n, student_name is better than length_of_person_name.
- Be careful when using lower case letter l and the uppecase O becasuse they could be confused with a numbers.
If u get error check ur traceback and find a solution for error u get. It should be easy because traceback provides a enough info for ur error. For futher studies [[Python traceback]]
Strings
A strings is a series of characters. Anything inside a quotes. like this :
- “This is a string !”
- ‘This is also string !!’
Changing case in a String with Methods
code :
name = "pyThon3 changing case in a String"
print(name.title())
print(name.upper())
print(name.lower())
output :
Python3 Changing Case In A String
PYTHON3 CHANGING CASE IN A STRING
python3 changing case in a string
[Finished in 0.1s]
This methods are particulary useful for storing data. Specially lower().
Using string callled F-string
The F is for format.
code:
first_name = "ada"
second_name = "lovelace"
full_name = f"{first_name} {second_name}"
message = f"Hello, {full_name.title()}!"
print(message)
output:
Hello, Ada Lovelace!
[Finished in 0.1s]
** NOTE :- f strings were first introduced in Python 3.6. If you’re using older version u need to use format() method rather tan this f syntex. **
full_name = "{} {}" .format(first_name, last_name)
Adding whitespace to strings with Tabs or Newlines
whitespaces referes to any non-printing characters, such as spaces, tabsand end-of-line symbols. How we add these characters on our programme.
- \t - add a tab
- \n - add a new line
- \n \t - tab and new line , combined of both
example code :
print("Languages : \n \tPython \n \tC \n \tJavaScript")
output :
Languages :
Python
C
JavaScript
[Finished in 0.1s]
Newlines and tabs will be very useful in next two chapters when you start to produce many lines of output from just a few lines of code.
Stripping Whitespace
extra white spaces can be confusing in your programs. Like ‘python’ and 'python ’ looks same but to program these two different strings. Specially this important to usernames when people’s use extra space after typing their username it can be involed with login credentials. Fortuanately, Python gives a soloution for this matter.
- strip() - Both side of once remove spaces
- rstrip() - Right to end remove spaces
- lstrip() - Left to end remove spaces
code :
favourite_language = ' python '
favourite_language1 = ' python '
favourite_language2 = ' python '
favourite_language = favourite_language.rstrip()
favourite_language1 = favourite_language1.lstrip()
favourite_language2 = favourite_language2.strip()
print(favourite_language)
print(favourite_language1)
print(favourite_language2)
output :
python
python
python
[Finished in 0.1s]
Stripping functions are used most often to clean up user input before it’s stored in a program
Avoiding Syntex Errors wiyth Strings
Check the document about troubleshooting -> [[Syntex errors with strings]]
Numbers
Integers
- Add (+)
- Subtract (-)
- Multiply (*)
- divide (/)
- exponent (**)
example code :
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 2+3
5
>>> 3-2
1
>>> 2*3
6
>>> 3/2
1.5
>>> 3**3
27
>>> 2+3 *4
14
>>> (2+3) * 4
20
Floats
Python calls any number with a decimal point.
Code :
>>> 0.1 + 0.1
0.2
>>> 0.2 - 0.5
-0.3
>>> 2 * 0.1
0.2
>>> 0.2 + 0.1
0.30000000000000004
>>> 4/2
2.0
>>> 1 + 2.0
3.0
>>> 3.0 ** 2
9.0
When you writing long numbers u can group digits using underscores.
universe_age = 14_000_000_000
print(universe_age)
14000000000
Constants
Python dosen’t have built in condtant types, but python programmers use all cap to indicate varible should be treated as a constant and never be changed :
MAX_CONNECTIONS = 5000